在 CentOS 上测试 PyTorch 是否安装成功、能否正常使用(尤其是 GPU),可以按下面步骤来。
cat /etc/centos-release
python3 --version
建议使用 Python 3.8 / 3.9 / 3.10(PyTorch 官方支持较好)
python3
import torch
print(torch.__version__)
✅ 能正常输出版本号说明安装成功。
x = torch.rand(3, 3)
y = torch.rand(3, 3)
print(x + y)
如果没有报错,说明 CPU 版本可用。
torch.cuda.is_available()
True → GPU 可用False → 未使用 GPU(可能是驱动/CUDA/PyTorch 版本不匹配)print(torch.cuda.device_count())
print(torch.cuda.get_device_name(0))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.rand(1000, 1000).to(device)
y = torch.rand(1000, 1000).to(device)
z = x @ y
print(z.device)
libstdc++.so.6 版本过低报错类似:
GLIBCXX_3.4.29 not found
解决:
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
通常需升级 gcc 或使用 conda 环境。
✅ 强烈建议使用 conda:
conda create -n torch python=3.9
conda activate torch
pip install torch torchvision
nvidia-smi
python3 -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
如果你愿意,可以告诉我:
我可以给你 一条最适合你的安装+测试命令。