本次配置之前确认电脑是否支持gpu,可以在控制面板->硬件和声音->设备管理器->显示适配器中查看,如图所示
一般电脑最低在1060左右也支持gpu,这个不用担心,接下来安装gpu显卡驱动,网址https://www.nvidia.com/download/index.aspx?lang=en-us ,如图所示
下载完后再下载gpu运行库cuda和cudnn,cuda安装链接https://developer.nvidia.com/cuda-toolkit-archive 如图所示
一般选择默认安装,安装后同时按win+r,输入cmd回车,输入nvcc -V,回车
那么显示安装成功,接下来配置环境变量了,
打开设置,里面搜索环境变量,选择系统环境变量,然后双击path,点击新建即可,如图所示
记得新建后保存即可。接下来安装cudnn,下载链接https://developer.nvidia.com/cudnn,选择download cudnn(记得这个要注册登录下载)
选择对应cuda的10版本,如图所示
x下载后得到一个文件如图所示
按照上面的步骤进行操作即可,然后再添加一下路径到环境变量C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\lib\x64和C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\cudnn\bin
,接下来直接终端pip下载tensorflow命令
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==2.0.0
接下来验证gpu是否可用,代码如下
import tensorflow as tf
print(tf.test.is_gpu_available())
结果如下
2022-09-09 15:05:37.152943: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2022-09-09 15:05:40.452044: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2022-09-09 15:05:40.458530: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2022-09-09 15:05:40.491185: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: NVIDIA GeForce RTX 3060 Laptop GPU major: 8 minor: 6 memoryClockRate(GHz): 1.425
pciBusID: 0000:01:00.0
2022-09-09 15:05:40.491329: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2022-09-09 15:05:40.491672: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
True
2022-09-09 15:05:41.705041: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-09-09 15:05:41.705135: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
2022-09-09 15:05:41.705189: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
2022-09-09 15:05:41.706141: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/device:GPU:0 with 4745 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6)
为True即可以使用gpu环境了,恭喜配置完成!
如果上述代码帮助您很多,可以打赏下以减少服务器的开支吗,万分感谢!


点击此处登录后即可评论