1. 程式人生 > 實用技巧 >Tensorflow安裝以及RuntimeError: The Session graph is empty. Add operations to the graph before calling run().解決方法

Tensorflow安裝以及RuntimeError: The Session graph is empty. Add operations to the graph before calling run().解決方法

Tensorflow安裝

之前裝過pytorch,但是很多老的機器學習程式碼都是tensorflow,所以沒辦法,還要裝個tensorflow。

在安裝之前還要安裝nvidia驅動還有cudn之類的,這些我已經在之前的篇章介紹過,就不在這細說了,可以直接傳送過去看。
那麼前面這些搞完,直接執行下面的命令:
pip install --upgrade tensorflow-gpu

上面這行命令,會自動下載你的對應版本或者最新版本,現在下載應該是2.0版本的。

sess.run()無法執行

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

具體的測試tensorflow是否裝好程式碼:

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))

參考連結:
https://blog.csdn.net/weixin_38410551/article/details/103631977