7

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?

3voC
  • 647
  • 7
  • 19

2 Answers2

6

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.

P-Gn
  • 23,115
  • 9
  • 87
  • 104
2

In the modern version of TensorFlow (2.2), you can use the is_tensor function documented here.

assert(tf.is_tensor(elem))
P Shved
  • 96,026
  • 17
  • 121
  • 165