天天看點

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,tensorflow')
sess= tf.compat.v1.Session()#版本2.0的函數
print(sess.run(hello))
           

後記:(1)對問題的認知,能夠幫助自己快速定位問題。

      開始定位問題時,太懶,直接連英文的意思都沒搞懂,就直接粘貼到百度去搜尋,于是根本無法找到合适的答案。

   (2)明确問題,這個問題是圖是空的,然後我去檢視圖建立的方法,沒錯,就是這樣建立的。然後懷疑可能時無法執行sess.run()。

   (3)遇到問題,一定要深入思考一下,才是問題的根因,直接依據問題的表面去查找答案,根本無法找到。

參考連結:https://blog.csdn.net/sinat_36502563/article/details/102302392

參考連結:https://ask.csdn.net/questions/657580