- 写老师布置的神经网络作业中,要求安装tensorflow,并且运行示例代码(使用with tf.Session()创建上下文)
- 我作为新手,本来想在Ubuntu上弄的,奈何电脑有点带不动虚拟机各种报bug。最后还是老老实实的在我的win10上安装了anaconda。官网:https://www.anaconda.com/。
- 最后在anaconda神器的图形界面上安装tensorflow,这里注意是environment-root-uninstall-旁边的搜索框(tensorflow)
- 安装成功之后,开始运行老师给的示例代码:
import tensorflow as tf
hello=tf.constant('hello world!')
with tf.Session() as sess:
-
`print(sess.run(hello).decode())`
- 结果报错:AttributeError: module ‘tensorflow’ has no attribute ‘Session’
10.查了蛮久,一开始看的是英文的回答,半天没看懂,后来终于翻到国内网友的解答才明白。意思就是说更新的tensorflow已经删除了Session ,tf.Session()已过时,需要使用tf.compat.v1.Session()代替。即把上面第三行替换为
:with tf.compat.v1.Session() as sess:
- 现在运行代码就对了
- 如果这样还是报错的话,估计你是还没有装库。参考以下链接https://blog.csdn.net/xiakejiang/article/details/82720815
根据我同学的反馈,基本到这就差不多解决问题了。哈哈哈要相信自己可以搞定的!!我当初不知道搞了多少个下午了(捂脸)