Tuesday, 12 November 2019

Returning Values of TensorFlow Functions in Different Modes

TensorFlow functions, eg. tf.abs, tf.add, etc. all of them behave differently in the 2 modes: Eager mode, and graph mode.

In Eager mode (default mode in TensorFlow 2), a function returns a tensor with values inside.

In graph mode (default mode in TensorFlow 1, or inside tf.Graph().as_default() block, a function returns a tensor with op inside.

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

#libs
import tensorflow as tf;
from tensorflow.keras.layers import *;

#code
#tf function returns value-based tensor in eager mode
T1 = tf.abs(-1);
print("Value-tensor (aka. Eager tensor):");
print(T1);

#tf function returns op-based tensor in graph mode
G = tf.Graph();
with G.as_default():
  T2 = tf.abs(-1);
  print("\nOp-tensor (aka. graph tensor):");
  print(T2);
#eof

No comments:

Post a Comment