TensorFlow安裝(2.1.0版)及問題解決方法
主要流程參照這位大佬的:博客如下:
https://blog.csdn.net/weixin_44170512/article/details/103990592(主要參考)
官方給出的,環境搭配:
https://tensorflow.google.cn/install/source_windows
先說下我的環境:
顯卡:gtx 950M
python:3.6(ANACONDA 默認python3.7.4,我新建了python的3.6環境)
CUDA: 10.1
cuDNN :v7.6.5
目標:tensorflow_gpu 2.1.0
步驟:
1.安裝Anaconda
Anaconda默認是pythonn3.7.4,tensorflow2.1版本可以使用python3.7.4
2.確定tensorflow是cpu版還是gpu版
cpu版和gpu版只是運算速度上有區別,如果顯卡支持的話,可以選擇gpu版(我gtx950M的顯卡,選的gpu版)
3.安裝CUDA,cuDNN,並配置環境變量
https://blog.csdn.net/weixin_44170512/article/details/103990592(這位大佬講得很細!)
問題1:ImportError: DLL load failed: 找不到指定的模塊。
上述都安裝成功,也都測試正常。
但是,在我運行import tensorflow的時候,就會報錯:ImportError: DLL load failed: 找不到指定的模塊。
解決方法:
Tensorflow 2.1.0 運行需要安裝 VS 2019 的環境,因爲,單獨安裝了 VS 2015 或 VS 2017 的安裝環境,均提示導入 Dll 錯誤,所以,需要下載同時包含,VS 2015、VS2017以及 VS2019 運行環境的安裝包
下載地址:https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads
參考:https://blog.csdn.net/lingtianyulong/article/details/104207960,感謝大佬,這問題終於解決了。
問題2:CUDA_ERROR_UNKNOWN: unknown error
安裝好tensorflow後,當然要測試下了,運行下面的代碼
import tensorflow as tf
hello = tf.constant('hello,tf')
sess = tf.Session()
print(sess.run(hello))
卻報錯:Attempting to fetch value instead of handling error Internal: failed to get device attribute 13 for device 0: CUDA_ERROR_UNKNOWN: unknown error
解決方法:更新顯卡驅動就可以了
問題3:module 'tensorflow' has no attribute 'Session'
這其實不算問題,tensorflow的版本更新導致的
tensorflow是2.0以上版本(我用的是2.1)
sess = tf.Session()
已經改爲:
sess = tf.compat.v1.Session()
tensorflow2.1的測試方法:
import tensorflow as tf
h = tf.constant('Hello, this is TensorFlow')
print(tf.print(h))
經過一段時間,會得到輸出:
Hello, this is TensorFlow
None