TensorFlow.NET機器學習入門【8】採用GPU進行學習

seabluescn發表於2021-12-31

 隨著網路越來約複雜,訓練難度越來越大,有條件的可以採用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、除錯

如果環境沒有安裝成功,程式執行會列印下列資訊:

TensorFlow.NET機器學習入門【8】採用GPU進行學習
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

 

如果環境安裝成功,則顯示如下:

TensorFlow.NET機器學習入門【8】採用GPU進行學習
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
View Code

關鍵字:Successfully opened dynamic library

  

【相關資源】

 原始碼:Git: https://gitee.com/seabluescn/tf_not.git

目錄:檢視TensorFlow.NET機器學習入門系列目錄

相關文章