在 CentOS 上测试 PyTorch 是否安装成功,通常按下面的步骤来操作。
python3 --version
pip3 --version
如果没有 python3:
sudo yum install python3 python3-pip -y
pip3 install torch torchvision torchaudio
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
(cu118 对应 CUDA 11.8,按需调整)
新建测试脚本 test_pytorch.py:
import torch
print("PyTorch version:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())
if torch.cuda.is_available():
print("CUDA version:", torch.version.cuda)
print("GPU device:", torch.cuda.get_device_name(0))
else:
print("Running on CPU")
运行:
python3 test_pytorch.py
import torch
x = torch.rand(3, 3)
y = torch.rand(3, 3)
print(x + y)
sudo yum install -y libgomp glibc
CentOS 7 可能不支持最新 PyTorch,建议:
python3 -m venv venv
source venv/bin/activate
pip install torch
如果你告诉我:
我可以给你 更精确的命令。