【问题标题】:Where Tensorflow EagerTensor is defined?Tensorflow EagerTensor 是在哪里定义的?
【发布时间】:2018-05-06 11:37:11
【问题描述】:

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

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

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

    • tf.is_tensor(以前的 tf.contrib.framework.is_tensor)将返回 True 以获得 EagerTensor
    • tf.executing_eagerly 返回 True,如果您正在急切地执行。

    我相信它们应该能满足您 99% 的需求——如果它落在遗漏的那个百分比内,我很想知道您的问题。

    【讨论】:

    • 我不知道第一个函数。谢谢。
    【解决方案2】:

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

    assert(tf.is_tensor(elem))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多