-
语句: result.numpy()
-
报错: AttributeError: ‘Tensor’ object has no attribute ‘numpy’
-
工具: jupyter-notebook
-
现象:
A. 直接再终端执行 python mnist.py 是没有报错的
B. 再终端执行 python 后,直接写代码也是没有问题的 -
原因:
A. 我在一片博客中找到:.numpy方法只用在使用tf.enable_eager_execution()(命令式编程开启)后才有的方法, 否则会有==AttributeError: ‘Tensor’ object has no attribute ‘numpy’==报错
B. 又再另一篇博客中找到:刚更新了tensorflow 2.0的beta版本,发现新版本似乎默认开启了eager_execution -
总结: Tensor.numpy() 只能在终端执行代码或者在终端调用代码才有效(这时候会默认开启eager_execution),而在jupyter-notebook 中没有开启,所以就会报这个错误
-
解决办法: 如何再 jupyter-notebook 中使得 Tensor Object 也能调用 numpy() 方法呢?