目录
分析原因
1.检测TensorFlow 能否找到GPU及CPU
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus, cpus)
输出为
2024-06-11 21:44:13.158154: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-06-11 21:44:13.909295: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
Num GPUs Available: 0
[] [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
意思是,找不到GPU,但找到了CPU
2.检查Pytorch能不能找到GPU
import torch
print(torch.cuda.device_count())
输出为
1
即pytorch能找到GPU
3.分析问题,版本检查
应该是tensorflow版本过高的原因,查看CUDA版本,CUDA版本为12.2
具体安装过程
1.cuda安装
输入
nvidia-smi.exe
显示12.2,意思是最高支持12.2
cuda不同版本可以共存,但在安装新的cuda之前需要删除这个文件,并且在环境变量中将你需要的cuda上移,具体操作如下。(安装过程戳新链接,下面主要讲新cuda如何与旧版本共存)
win11+CUDA12.2安装CUDA、cuDNN和tensorflow - 哔哩哔哩 (bilibili.com)
https://developer.nvidia.com/cuda-downloads
打开链接,下载11.2,下载完成点击安装。
如果你已经安装过cuda,在安装新cuda时会遇到报错you already have a new version of the nvidia frameview. have new,此时打开,设置-安装的应用-搜索nvidia frameview.-卸载。即可
(我已经卸载过了,所以没有)
设置-系统-系统信息-高级系统设置
环境变量-path-将所需新版本的路径移到旧版本前
此处给出我的路径,你需要根据自己电脑的地址填入类似路径
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\libnvvp
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\lib\x64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\include
2.cudann
看教程(和4是同一个链接)
https://developer.nvidia.com/rdp/cudnn-download
3.tensorflow
进入你的虚拟环境
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==2.10.0
4.torch
进入官网下载即可,不过多赘述
(下面一张图看不懂就戳图下链接)
全网最详细的安装pytorch GPU方法,一次安装成功!!包括安装失败后的处理方法!-CSDN博客
5.验证
运行如下代码
import tensorflow as tf
print("tensorflow_Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus, cpus)
import torch
print("torch:",torch.cuda.device_count())
都显示能检测到,即开头那样,就是成功