Tensorflow EagerTensor定义在哪里?

时间:2018-05-06 11:37:11

标签: tensorflow

在我的代码中,我想检查返回的对象类型是否为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

但结果是:

<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'

这里:AttributeError: module 'tensorflow' has no attribute 'python'我发现tensorflow故意删除它对python模块的引用。那么如何检查我的对象是否为EagerTensor实例?

2 个答案:

答案 0 :(得分:1)

我不确定你是否可以,但我认为你可能不需要。您已经拥有以下工具:

  • tf.contrib.framework.is_tensorTrue
  • 返回EagerTensor
  • tf.executing_eagerly如果你是,那就回复True。好吧,急切地执行。

我相信他们应该满足你99%的需求 - 我很想知道你的问题是否属于那个百分比。

答案 1 :(得分:1)

在现代版本的TensorFlow(2.2)中,您可以使用here中记录的is_tensor函数。

assert(tf.is_tensor(elem))