随着網絡越來約複雜,訓練難度越來越大,有條件的可以采用GPU進行學習。本文介紹如何在GPU環境下使用TensorFlow.NET。
随着網絡越來約複雜,訓練難度越來越大,有條件的可以采用GPU進行學習。本文介紹如何在GPU環境下使用TensorFlow.NET。
TensorFlow.NET使用GPU非常的簡單,代碼不用做任何修改,更換一個依賴庫即可,程式是否能運作成功主要看環境是否安裝正确,這篇文章重點介紹的也就是環境的安裝了。
CUDA和cuDNN的安裝都比較容易,重點是要裝對版本。
1、确認安裝版本
首先電腦得有一塊NVIDIA的顯示卡!
在桌面右鍵選擇NVIDIA控制台,在程式左下角點選 :系統資訊。
首先要确認顯示卡CUDA的版本,我的是10.1,實際安裝時要根據自己的環境來。
根據顯示卡CUDA版本,需要安裝對應的CUDA Toolkit和cuDNN的包,同時也要引用對應版本的TensorFlow_gpu運作庫。
這是TensorFlow官方網站提供的一個對應關系。
2、安裝CUDA Toolkit
安裝包下載下傳位址:CUDA Toolkit 11.5 Update 1 Downloads | NVIDIA Developer
由于不可描述的原因,網站打開很慢,需要有一定的耐心。
我下載下傳的程式為:cuda_10.1.243_win10_network.exe,要保證前兩個版本号和本機顯示卡的CUDA版本一緻,最後一位影響不大。
隻要選對版本,安裝就沒有問題了,基本一路下一步就可以了。
3、安裝cuDNN
在NVIDIA網站搜尋cuDNN即可找到下載下傳位址,同樣要注意版本。
我下載下傳的檔案為:cudnn-10.1-windows10-x64-v8.0.5.39.zip
從檔案名可以看出,該軟體版本為8.0,對應的CUDA版本為10.1,這點和TensonFlow網站上說的不完全一緻,但實測可以使用。
注意cuDNN軟體不需要安裝,隻要解壓到指定目錄即可。
假設CUDA安裝目錄為:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\V10.1
解壓後的檔案包括:bin、inclue、lib三個檔案夾,和CUDA安裝目錄下的三個同名檔案夾合并即可。
【我懷疑随便解壓到什麼位置,隻要把路徑加入到path裡即可,但我沒有測試過。】
4、更換依賴庫
本系列第一篇就介紹過一個TensorFlow,NET項目的依賴庫如下:
如果需要采用GPU學習,更改其中的一個庫如下即可。
需要注意運作庫的版本。我的GPU最高支援2.3
如果此時你的運作環境沒有具備,程式運作時會彈出警告,并自動切換到CPU進行計算。
5、調試
如果環境沒有安裝成功,程式運作會列印下列資訊:
2021-08-29 09:46:26.631756: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-08-29 09:46:26.633225: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-08-29 09:46:27.323608: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-08-29 09:46:27.993386: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-08-29 09:46:27.994493: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublas64_11.dll'; dlerror: cublas64_11.dll not found
2021-08-29 09:46:27.997410: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2021-08-29 09:46:27.998498: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2021-08-29 09:46:27.999403: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusolver64_11.dll'; dlerror: cusolver64_11.dll not found
2021-08-29 09:46:28.000402: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusparse64_11.dll'; dlerror: cusparse64_11.dll not found
2021-08-29 09:46:28.001807: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2021-08-29 09:46:28.001917: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1835] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
View Code
關鍵字:Could not load dynamic library
如果環境安裝成功,則顯示如下:
2021-08-30 15:10:25.201050: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2021-08-30 15:10:25.471127: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-08-30 15:10:25.481201: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1fa714fedc0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-08-30 15:10:25.481288: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2021-08-30 15:10:25.485142: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2021-08-30 15:10:26.377044: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1060 computeCapability: 6.1
coreClock: 1.6705GHz coreCount: 10 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 178.99GiB/s
2021-08-30 15:10:26.377230: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2021-08-30 15:10:26.385642: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2021-08-30 15:10:26.390730: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2021-08-30 15:10:26.392476: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2021-08-30 15:10:26.402477: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2021-08-30 15:10:26.413454: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2021-08-30 15:10:26.414923: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2021-08-30 15:10:27.287767: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-08-30 15:10:27.287889: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263] 0
2021-08-30 15:10:27.289201: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1276] 0: N
2021-08-30 15:10:27.290348: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4826 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)
2021-08-30 15:10:27.297029: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1fa3d8c6890 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2021-08-30 15:10:27.297143: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce GTX 1060, Compute Capability 6.1
2021-08-30 15:10:27.890649: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
關鍵字:Successfully opened dynamic library
【相關資源】
源碼:Git: https://gitee.com/seabluescn/tf_not.git
目錄:檢視TensorFlow.NET機器學習入門系列目錄
簽名區:
如果您覺得這篇部落格對您有幫助或啟發,請點選右側【推薦】支援,謝謝!