TensorFlow.NET机器学习入门【8】采用GPU进行学习

时间:2024-03-11 18:32:17

 随着网络越来约复杂,训练难度越来越大,有条件的可以采用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-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

关键字:Successfully opened dynamic library

  

【相关资源】

 源码:Git: https://gitee.com/seabluescn/tf_not.git

目录:查看TensorFlow.NET机器学习入门系列目录