In my code I want to check whether returned object type is EagerTensor
:
import tensorflow as tf
import inspect
if __name__ == '__main__':
tf.enable_eager_execution()
iterator = tf.data.Dataset.from_tensor_slices([[1, 2], [3, 4]]).__iter__()
elem = iterator.next()
print(type(elem))
print(inspect.getmodule(elem))
assert type(elem) == tf.python.framework.ops.EagerTensor
But the result is:
<class 'EagerTensor'>
<module 'tensorflow.python.framework.ops' from '/home/antek/anaconda3/envs/mnist_identification/lib/python3.6/site-packages/tensorflow/python/framework/ops.py'>
Traceback (most recent call last):
File "/home/antek/.PyCharm2018.1/config/scratches/scratch_4.py", line 11, in <module>
assert type(elem) == tf.python.framework.ops.EagerTensor
AttributeError: module 'tensorflow' has no attribute 'python'
Here: AttributeError: module 'tensorflow' has no attribute 'python' I found out that tensorflow purposely deletes its reference to the python module. So how can I check that my object is an EagerTensor instance?
EagreTensor represents a tensor who's value has been calculated in eager mode whereas Tensor represents a tensor node in a graph that may not yet have been calculated.
We can access the data type of a tensor using the ". dtype" attribute of the tensor. It returns the data type of the tensor.
I am not sure if you can, but I think you probably don't need to. You already have the following tools:
tf.is_tensor
(previously tf.contrib.framework.is_tensor
) that will return True
for an EagerTensor
tf.executing_eagerly
that returns True
if you are, well, executing eagerly.I believe they should cover 99% of your needs -- and I would be curious to hear about your problem if it falls in that percentage left out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With