1

大约一个月前,我在 Google Colab 中成功运行了一个神经风格迁移笔记本教程。但是,本周我无法成功运行完全相同的笔记本,并显示以下错误消息:AttributeError: module 'tensorflow.contrib.eager' has no attribute 'Variable'。

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

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

4

1 回答 1

0

在 TensorFlow 1.15tensorflow.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)

于 2020-03-16T22:17:42.290 回答