TensorFlow 2.0 問題彙總

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

新的Tensorflow 2.0版本中已經移除了Session這一模塊,改換運行代碼

tf.compat.v1.Session()

就可以獲得與原先相同的輸出信息。如果覺得不方便,也可以改換低版本的Tensorflow,直接用pip即可安裝

pip install tensorflow==1.14

RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

無法執行sess.run()的原因是tensorflow版本不同導致的,tensorflow版本2.0無法兼容版本1.0.
解決辦法:
tf.compat.v1.disable_eager_execution()

import tensorflow as tf
tf.compat.v1.disable_eager_execution() # 保證sess.run()能夠正常運行
hello = tf.constant('hello tf')
sess = tf.compat.v1.Session()
print(sess.run(hello))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章