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


在解決了錯誤:AttributeError: module 'tensorflow' has no attribute 'Session'之后,又出現了新的錯誤

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

錯誤原因:

最終還是因為安裝的Tensorflow版本問題

解決方法:

重新安裝Tensorflow,選擇1.X的版本進行安裝,如下圖

其他解決方法

在解決上述問題的過程中,起初並沒有采取這么簡單粗暴的方法,也查詢了其他解決辦法,但是都沒有很好的解決自己的問題,現將方法總結如下:

1.添加代碼:tf.compat.v1.disable_eager_execution()

在運行session之前,添加代碼tf.compat.v1.disable_eager_execution()保證run()可以正常運行

import tensorflow as tf
tf.compat.v1.disable_eager_execution() #保證sess.run()能夠正常運行
hello = tf.constant('hello,tensorflow')
sess= tf.compat.v1.Session()#版本2.0的函數
print(sess.run(hello))

2.清理開始的會話session

引入keras來清理考試會話的session

import tensorflow as tf
import keras  #引入keras
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()  
config.gpu_options.allow_growth = True 
sess = tf.Session(config=config)
set_session(sess) keras.backend.clear_session() #此句代碼分量很重

參考博客:

CSDN博主「愛學習的人工智障」:https://blog.csdn.net/weixin_38410551/article/details/103631977

CSDN博主「視覺弘毅」:https://blog.csdn.net/weixin_41041772/article/details/100085170


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM