Tensorflow EagerTensor 在哪里定义?

3vo*_*voC 5 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
Run Code Online (Sandbox Code Playgroud)

但结果是:

<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'
Run Code Online (Sandbox Code Playgroud)

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

P-G*_*-Gn 6

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

  • tf.is_tensor(以前tf.contrib.framework.is_tensor)将返回True一个EagerTensor
  • tf.executing_eagerlyTrue如果你急切地执行,它就会返回。

我相信它们应该可以满足您 99% 的需求——如果您的问题落在这个百分比之外,我很想知道您的问题。