📌  相关文章
📜  AttributeError: 模块 'tensorflow' 没有属性 'eagerly' (1)

📅  最后修改于: 2023-12-03 15:13:33.059000             🧑  作者: Mango

AttributeError: 模块 'tensorflow' 没有属性 'eagerly'

当在TensorFlow 2.0版本或更高版本中使用"eager execution"时,有时会遇到类似于"AttributeError: 模块 'tensorflow' 没有属性 'eagerly'"的错误。

这个错误通常是由于代码使用了过时的TensorFlow导致的。在TensorFlow 2.0中,'eager execution' 是默认启用的,因此不需要调用'tf.enable_eager_execution()'函数。如果使用了这个函数,就会出现上述错误。

如果在使用TensorFlow 2.0或更高版本时遇到了这个问题,请尝试将'tf.enable_eager_execution()'代码段移除,看看问题是否得到解决。

下面的代码片段演示了如何使用TensorFlow 2.0中的'eager execution':

import tensorflow as tf

# 不需要调用tf.enable_eager_execution()

a = tf.constant([1, 2])
b = tf.constant([3, 4])

# 执行计算
print(tf.add(a, b))

在TensorFlow 2.0中,上述代码会直接输出结果,而不需要显式地调用tf.Session()。这就是'eager execution'的优点之一,因为它让TensorFlow更易于学习和调试。

总之,当在TensorFlow 2.0中使用'eager execution'时,如果收到“AttributeError: 模块 'tensorflow' 没有属性 'eagerly'”错误,请确保没有使用'tf.enable_eager_execution()'函数。