【问题标题】:Unable to Enable Tensorflows Eager execution无法启用 TensorFlow 急切执行
【发布时间】:2019-07-26 10:51:51
【问题描述】:

我有一个安装了 Tensorflow 2.0.0-beta1 的 conda 环境。但是,每当我导入 tensorflow 并尝试启用急切执行时,我都会收到错误消息:

AttributeError: module 'tensorflow' has no attribute 'enable_eager_execution'

我为此运行的唯一代码是:

import tensorflow as tf
print(tf.__version__)
tf.enable_eager_execution()

这是 tensorflow 2.0 beta 模块的错误还是我的安装问题?

【问题讨论】:

    标签: tensorflow keras eager-execution


    【解决方案1】:

    在 ternsorflow 2.0 中,enable_eager_execution 方法移至 tf.compat.v1 模块。以下适用于 tensorflow-2.0.0-beta1

    tf.compat.v1.enable_eager_execution()
    

    在 tensorflow 2.0 中,Eager Execution 由 default 启用。您无需在程序中启用它。

    例如

    import tensorflow as tf
    
    t = tf.constant([5.0])
    

    现在可以不用session对象直接查看张量的值了。

    print(t)
    # tf.Tensor([5.], shape=(1,), dtype=float32)
    
    

    也可以将张量值改为numpy数组

    numpy_array = t.numpy()
    print(numpy_array)
    # [5.]
    

    您还可以在 tensorflow-2 中禁用急切执行(在 tensorflow-2.0.0-beta1 上测试。这可能不适用于未来的版本。)

    tf.compat.v1.disable_eager_execution()
    t2 = tf.constant([5.0])
    print(t2)
    # Tensor("Const:0", shape=(1,), dtype=float32)
    

    在急切执行被禁用后在张量上调用 numpy() 方法会引发错误

    AttributeError: 'Tensor' object has no attribute 'numpy'
    

    禁用急切执行时应考虑的一个问题是,一旦禁用急切执行,就无法在同一程序中启用它,因为应在程序启动时调用tf.enable_eager_execution,并在禁用急切执行后调用此方法会引发错误:

    ValueError: tf.enable_eager_execution must be called at program startup.
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-28
      • 2019-06-23
      • 2019-04-25
      • 2019-01-12
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多