I want to test the type of a Tensor in non-eager mode. In Eager mode, this code works:
tf.enable_eager_execution()
t = tf.random.normal([1, 1])
num_type = t.dtype
print(num_type == tf.float32) # prints `True`
In non-eager mode, the same code does not and the only way I found to test is the ugly str(num_type) == "float32"
:
sess = tf.Session()
t = sess.run(tf.random.normal([1, 1]))
num_type = t.dtype
print(num_type) # prints `float32`
print(str(num_type) == "float32") # prints `True`
print(num_type == float32) # returns `NameError: name 'float32' is not defined`
print(num_type == tf.float32) # returns `TypeError: data type not understood`
and if I try to grab the type of the Tensor inside the session:
t = tf.random.normal([1, 1])
t_type = t.dtype
num_type = sess.run(t_type)
then I get:
TypeError: Fetch argument tf.float32 has invalid type <class 'tensorflow.python.framework.dtypes.DType'>, must be a string or Tensor. (Can not convert a DType into a Tensor or Operation.)
How can I test for a float32
type in non-eager mode?