之前都是使用tensorflow的cpu版本,沒注意自己這臺老人機居然有GPU。便決定試一試看看自己電腦能不能跑起GPU。(各種百度谷歌)記錄如下:
首先:檢查自己的GPU是否可以使用:
CUDA:https://developer.nvidia.com/cuda-gpus
居然找到自己的GPU..哈哈哈(意外)
接下來:需要安裝CUDA + cuDNN。
剛開始無腦:使用CUDA10.0 +cuDNN 7 .4.2+tensorflow1.12發現報錯如下(沒截圖):
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "C:\Anaconda3\envs\tensorflow\lib\imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "C:\Anaconda3\envs\tensorflow\lib\imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: DLL load failed: 找不到指定的模塊。
反正我是沒解決 (我猜是版本問題)....
1.首先確定你需要安裝的版本(重點):版本貌似會出現很多我問題,建議多看看準備安裝的版本問題。
參考:https://blog.csdn.net/lifuxian1994/article/details/81103530
確定好版本:CUDA9.0+cuDNN7.4.2
2.安裝過程(看了很多,都不知道自己安裝是否成功,這篇有檢查環境確定你這一步沒有錯!!):https://blog.csdn.net/u010618587/article/details/82940528
打開命令提示符,輸入:nvcc -V
出現如下類似信息:
3.安裝tensorflow-gpu(巨煩...)因爲我之前使用的是tensorflow-cpu版,已經安裝過了環境,造成很多問題...
我之前有兩個環境,直接pip install tensorflow-gpu之後用的是pycharm 去跑測試環境發現numpy報錯???什麼鬼,剛開始以爲是版本問題各種重裝裝tensorflow-gpu1.5-1.9版本,然並卵(剛裝好直接用cmd去測試吧....)。而且還出現pip緩存的問題(參考:https://blog.csdn.net/kangkanglou/article/details/78955298) 。
過程中用的是Anaconda Prompt,建議安裝成功之後直接在裏面敲:
沒有報錯肯定沒問題。(生氣的我,刪除了之前的環境.....用基礎的環境pip –no-cache-dir install tensorflow-gpu給我裝了1.12的版本。)
報錯圖如下:
3.最後
import os
from tensorflow.python.client import device_lib
import tensorflow as tf
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
if __name__ == "__main__":
print(device_lib.list_local_devices())
# 使用CPU進行計算
with tf.device("/gpu:0"): #使用GPU 如果是cpu改爲cpu
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
c = tf.matmul(a, b)
# 查看計算時硬件的使用情況
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))
運行結果如下:
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 3087808467687446459
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 1470421401
locality {
bus_id: 1
links {
}
}
incarnation: 9473933570797557043
physical_device_desc: "device: 0, name: GeForce 840M, pci bus id: 0000:04:00.0, compute capability: 5.0"
]
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce 840M, pci bus id: 0000:04:00.0, compute capability: 5.0
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
Const: (Const): /job:localhost/replica:0/task:0/device:GPU:0
Const_1: (Const): /job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
[49. 64.]]
Process finished with exit code 0
出現這些說明沒問題的 。終於ok....
如有建議和意見聯繫我,不勝感激。