【问题标题】:Why there is AttributeError: module 'tensorflow.contrib.eager' has no attribute 'Variable' in TensorFlow 1.15?为什么在 TensorFlow 1.15 中有 AttributeError: module 'tensorflow.contrib.eager' has no attribute 'Variable'?
【发布时间】:2019-11-11 08:57:29
【问题描述】:

大约一个月前,我在 Google Colab 中成功运行了一个神经风格迁移笔记本教程。但是,本周我无法成功运行完全相同的笔记本,并显示以下错误消息: AttributeError:模块“tensorflow.contrib.eager”没有属性“变量”。

我检查了 Google Colab 中的 Notebook 使用的是 TensorFlow 1.15,当我检查 API 文档时,变量方法存在:https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/contrib/eager/Variable

此错误消息的原因是什么?

【问题讨论】:

  • 请发布一个包含完整错误回溯的最小代码示例。

标签: python tensorflow eager-execution


【解决方案1】:

在 TensorFlow 1.15 中 tensorflow.contrib.eager.Variable 不存在。虽然在文档中显示,但它被标记为已弃用,并且似乎根本不在代码中。

改用常规的旧tf.Variable。以下代码在 1.15 中运行良好:

import tensorflow as tf

tf.compat.v1.enable_eager_execution()

x = tf.Variable(1, dtype=tf.float32)
with tf.GradientTape() as tape:
    f = x * x
print(tape.gradient(f, x))

>> tf.Tensor(2.0, shape=(), dtype=float32)

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 2022-10-21
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 2015-09-26
    • 2020-11-23
    • 2022-08-24
    相关资源
    最近更新 更多