TensorFlow 找不到 GPU而PyTorch 能找到

目录

分析原因

1.检测TensorFlow 能否找到GPU及CPU

2.检查Pytorch能不能找到GPU

3.分析问题,版本检查

 具体安装过程

1.cuda安装

2.cudann

3.tensorflow

4.torch

5.验证



分析原因

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

一番搜索,是cuda版本过高了,在网上找cuda12.2的教程,都说tensorflow安装2.10就能跑,但我就是不行。最终我成功的版本如下:
cuda11.2,cudnn8.9.6,tensorflow2.10,torchtorch2.2.2+cu121
此时在运行最前面的代码,tensorflow和torch都可以检测到了。
所以安装包一定别安装最新的!!!!!!!

 具体安装过程

1.cuda安装

输入

nvidia-smi.exe

显示12.2,意思是最高支持12.2 

cuda不同版本可以共存,但在安装新的cuda之前需要删除这个文件,并且在环境变量中将你需要的cuda上移,具体操作如下。(安装过程戳新链接,下面主要讲新cuda如何与旧版本共存)

win11+CUDA12.2安装CUDA、cuDNN和tensorflow - 哔哩哔哩 (bilibili.com)

CUDA安装及环境配置——最新详细版-CSDN博客

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是同一个链接)

CUDA安装及环境配置——最新详细版-CSDN博客

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

全网最详细的安装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())

 都显示能检测到,即开头那样,就是成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值