您的位置:首页 > 移动开发 > Objective-C

代码问题1:出现AttributeError: RefVariable object has no attribute numpy

2020-05-25 20:16 826 查看

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 即可消除错误。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐