在Ubuntu上进行PyTorch版本兼容性测试,需重点关注PyTorch与CUDA的版本匹配,以下是具体步骤和方法:
查看Ubuntu版本
lsb_release -a # 确认系统版本(如20.04/22.04 LTS)
参考:PyTorch对Ubuntu LTS版本支持更完善。
检查CUDA版本
nvcc --version # 查看系统安装的CUDA Toolkit版本
nvidia-smi # 查看GPU驱动支持的CUDA版本
参考:确保CUDA版本与PyTorch兼容。
根据CUDA版本选择安装命令:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
或使用Conda:conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
参考:检查PyTorch与CUDA是否匹配
import torch
print("PyTorch版本:", torch.__version__)
print("CUDA版本:", torch.version.cuda if torch.cuda.is_available() else "无CUDA")
print("GPU是否可用:", torch.cuda.is_available())
torch.cuda.is_available()
返回True
,且torch.version.cuda
与安装的CUDA版本一致,则兼容性正常。False
,可能是CUDA版本不匹配或驱动问题,需调整PyTorch或CUDA版本。测试基础功能
运行一个简单的PyTorch脚本(如神经网络前向传播),确保无报错:
import torch
x = torch.randn(2, 3).cuda() if torch.cuda.is_available() else torch.randn(2, 3)
y = torch.nn.functional.relu(x)
print(y)
参考:
CUDA版本不匹配
torch.cuda.is_available()
返回False
,但系统安装了CUDA。驱动问题
nvidia-smi
查看推荐驱动)。通过以上步骤,可系统测试Ubuntu上PyTorch与CUDA的版本兼容性,确保深度学习环境稳定运行。