Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where Tensorflow EagerTensor is defined?

Tags:

tensorflow

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?

like image 577
3voC Avatar asked May 06 '18 11:05

3voC


People also ask

What is the difference between Eagertensor and tensor?

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.

How do you find the Dtype of a tensor?

We can access the data type of a tensor using the ". dtype" attribute of the tensor. It returns the data type of the tensor.


1 Answers

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.

like image 126
P-Gn Avatar answered Nov 15 '22 10:11

P-Gn