Tensorflow出现AttributeError: ‘RefVariable’ object has no attribute 'numpy’如下错误信息
代码中使用x.numpy()输出信息时报错
import tensorflow as tf
ignition = tf.Variable(451, tf.int16)
print(ignition.numpy())
AttributeError Traceback (most recent call last)
<ipython-input-10-3e5e7544b714> in <module>
1
----> 2 print(ignition.numpy())
AttributeError: 'RefVariable' object has no attribute 'numpy'
这个错误一般出现在tensorflow1.x版本中,2.0版本不会出现
解决办法:
在程序初始时加入如下代码,报错信息即可消除。
tf.enable_eager_execution(
config=None,
device_policy=None,
execution_mode=None
)
加入上述代码之后,有时会接着出现如下错误:
ValueError: tf.enable_eager_execution must be called at program startup.
这是由于使用jupyter notebook这类编辑器引起的,这个时候只需要重新运行一下 kernel 就可以了,restart 即可消除错误。