Tuesday, 5 November 2019

Concepts in TensorFlow 1 and 2

These are fundamental concepts in TensorFlow 1 and 2:
  • Session: The environment including devices and graph to run calculation. Session is no longer necessary to be created in TensorFlow 2, TF2 auto-creates sessions to run graph.
  • Graph: The structure containing tensors (nodes) and ops (edges).
    Graph is created in TensorFlow 1 by a chain of functions.
    Graph is created in TensorFlow 2 by a function annotated with @tf.function with the chain of functions inside.
  • Tensor (or Op-Tensor) is the tensor containing an op.
  • EagerTensor (or Value-Tensor) is the tensor containing values.
  • Placeholder (no longer in TF2) is a tensor with empty op.
TensorFlow functions in Eager mode return value-tensors, TensorFlow functions in Graph mode or inside tf.Graph().as_default() block return op-tensors.

Session can run ops and op-tensors. No value returned from session-run an op. Value-tensor is returned from session-run an op-tensor.

Example code:
%tensorflow_version 2.x
%reset -f

#libs
import tensorflow as tf;

#code
G = tf.Graph();
with G.as_default():
  T1 = tf.compat.v1.placeholder(tf.float32,[1]);
  T2 = tf.compat.v1.placeholder(tf.float32,[1]);
  print("Placeholders are tensors with empty ops:");
  print(T1);
  print(T2);

  print("\nA TensorFlow function:");
  print(tf.add);

  #create a Tensor
  Add = tf.add(T1,T2);
  print("\nA tensor with op, or op-tensor:");
  print(Add);
  print("\nThe op is:");
  print(Add.op);

#create an op
Op = tf.Operation(tf.compat.v1.NodeDef(name="Op",op="Add"),G,inputs=[T1,T2]);
T  = tf.Tensor(op=Op,value_index=0,dtype=tf.float32); #op-tensor

#run to the op-tensor
S  = tf.compat.v1.Session(graph=G);
V1 = tf.convert_to_tensor([1], tf.float32);
V2 = tf.convert_to_tensor([2], tf.float32);
print("Value-tensors (or eager tensors):");
print(V1);
print(V2);

R  = S.run(T, feed_dict={T1:V1.numpy(), T2:V2.numpy()});
print("\nOp-Tensor after run:");
print(R);
#eof

No comments:

Post a Comment