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. 現在運行代碼就對了成功圖
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章