Monday, 4 November 2019

Function, Tensor, and Operation in TensorFlow 2

TensorFlow 1 runs by default in graph mode in which TF functions return op-tensors, not value-tensors (eager tensors). Operations are used to build graph and graph is fed by a session.

Since TensorFlow 2, the library runs by default in Eager mode (function mode) that every function returns value-tensor (eager tensor), unless: 
  • TF function is inside a function annotated with @tf.function
  • Or, inside a 'with' tf.Graph.as_default() block.
Example code:
%tensorflow_version 2.x
%reset -f

#libs
import tensorflow as tf;

def f1(Inp): #no @tf.function, no autograph
  print("\nEager tensor with values:");
  print(Inp);
  M = tf.multiply(Inp,2); #tf.multiply returns value-tensor here
  return M;

#@tf.function makes a graph of operations
@tf.function
def f2(Inp): #autograph needs param
  print("\nGraph tensor without values:");
  print(Inp);
  M = tf.multiply(Inp,2); #tf.multiply returns op-tensor here
  return M;

#another way to make a graph of operations
G = tf.Graph();
with G.as_default():
  Inp = tf.compat.v1.placeholder(tf.float32, [3]); #manual graph needs placeholder
  Op  = tf.multiply(Inp,2); #tf.multiply returns op-tensor here

T = tf.convert_to_tensor([1,2,3], tf.float32);
print("Eager tensor with values:");
print(T);
f1(T);

R = f2(T);
print("\nOutput from graph is eager tensor:");
print(R);

#tf.multiply in @tf.function returns op-tensor,
#but returns value-tensor (eager tensor) out here:
print("\nUse operation as function in Eager mode:");
print(tf.multiply(T,2)); #tf.multiply returns eager tensor here

print("\nRun operation in graph with feed:");
S = tf.compat.v1.Session(graph=G);
V = S.run(Op,{Inp:T.numpy()});
print(V);
#eof

No comments:

Post a Comment