本文基于 PyTorch训练框架
电脑装了CUDA 但是python训练的时候显示的还是用了cpu进行训练
def print_device_info():"""打印设备信息"""print("PyTorch版本:", torch.__version__)print("CUDA是否可用:", torch.cuda.is_available())if torch.cuda.is_available():print("GPU数量:", torch.cuda.device_count())print("当前GPU:", torch.cuda.get_device_name(0))print("CUDA版本:", torch.version.cuda)else:print("使用CPU进行预测")
打印的 使用CPU
解决方式
1、确认确实安装了CUDA
输入 nvidia-smi 看下有没有安装CUDA版本 没有的话 进行安装 参考:https://www.cnblogs.com/pxblog/p/19164452

如果装了还是显示使用CPU,那么有可能是安装了CPU版本的PyTorch版本 ,所以不会调用gpu 默认使用清华源 安装的是 CPU的版本
先卸载CPU版本的torch
pip uninstall torch torchvision torchaudio
如果电脑用了conda进行python管理的话 那么可以执行以下命令 (亲测可用,我用的就是这种)
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
还有以下这种 但是我没用过
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
这样安装好了之后 就可以了调用gpu了
