AttributeError: module 'tensorflow' has no attribute 'Session'

  1. 写老师布置的神经网络作业中,要求安装tensorflow,并且运行示例代码(使用with tf.Session()创建上下文)在这里插入图片描述
  2. 我作为新手,本来想在Ubuntu上弄的,奈何电脑有点带不动虚拟机各种报bug。最后还是老老实实的在我的win10上安装了anaconda。官网:https://www.anaconda.com/。
  3. 最后在anaconda神器的图形界面上安装tensorflow在这里插入图片描述
  4. 安装成功之后,开始运行老师给的示例代码:
  5. import tensorflow as tf
  6. hello=tf.constant('hello world!')
  7. with tf.Session() as sess:
  8. 	`print(sess.run(hello).decode())`
    
  9. 结果报错:AttributeError: module ‘tensorflow’ has no attribute ‘Session’
    10.查了蛮久,一开始看的是英文的回答,半天没看懂,后来终于翻到国内网友的解答才明白。意思就是说更新的tensorflow已经删除了Session ,tf.Session()已过时,需要使用tf.compat.v1.Session()代替。即把上面第三行替换为
    with tf.compat.v1.Session() as sess:
  10. 现在运行代码就对了成功图
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章