安装cuda,按照https://blog.csdn.net/wangbowj123/article/details/89381562
按照如图配置环境变量
然后试运行
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
print(gpus)
两段代码,尝试执行检测到的话即可
假如遇到大段的
Could not load dynamic library 'cudart64_110.dll';
Could not load dynamic library 'cudart64_110.dll';
一般是TF版本与cuda版本适配不好,还需要重新按照
https://tensorflow.google.cn/install/source
页面下部的
来匹配TF版本与CUDA的版本
安装过程中关闭安全卫士等,否则可能报毒然后误删文件导致import 的时候报错
尝试下载tensorflow-gpu==2.3.0,使用
pip install tensorflow-gpu==2.3.0 -i https://pypi.douban.com/simple/
找到旧版的TF-GPU尝试
但是此时可能还是找不到“dynamic library cudart64_101.dll”
然后我们将文件夹中对应10.2版本的末尾为102的文件复制一份,文件末尾改为101即可修正,而且利用GPU进行训练
配置好之后消除所有报错以后
# 检测GPU
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
print(gpus)
的输出为
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
开始训练前控制台会显示
StreamExecutor device (0): NVIDIA GeForce GTX 1050 Ti, Compute Capability 6.1
训练的过程中GPU的利用率
配置成功后,可见训练时的GPU利用率显著上升
如果报错
Could not load dynamic library 'cupti64_101.dll'; dlerror: cupti64_101.dll not found
需要将
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\extras\CUPTI\lib64
放入环境变量path中
指定GPU:
tf.device('/gpu:2')
#或者采用如下代码
gpus = tf.config.list_physical_devices("GPU")
print(gpus)
if gpus:
gpu0 = gpus[0] # 如果有多个GPU,仅使用第0个GPU
tf.config.experimental.set_memory_growth(gpu0, True) # 设置GPU显存用量按需使用
tf.config.set_visible_devices([gpu0], "GPU")