在 CentOS 上测试 PyTorch 是否安装成功、能否正常使用(尤其是 GPU 是否可用),可以按下面步骤来。
先确认你用的是哪个 Python(建议用虚拟环境):
which python
python --version
如果是 conda 环境:
conda activate your_env
python
在 Python 交互环境中输入:
import torch
print(torch.__version__)
✅ 能正常输出版本号说明安装成功。
x = torch.randn(3, 3)
print(x)
print(torch.cuda.is_available())
True → CUDA 可用(GPU 正常)False → 可能是:
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.randn(1000, 1000).to(device)
y = torch.randn(1000, 1000).to(device)
z = x @ y
print(z.device)
nvidia-smi
nvcc --version
PyTorch 版本要和 CUDA 匹配(如 CUDA 11.8 / 12.1)
pip list | grep torch
GPU 版一般包含 cu11x
新建 test_torch.py:
import torch
print("PyTorch version:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())
if torch.cuda.is_available():
print("GPU:", torch.cuda.get_device_name(0))
运行:
python test_torch.py
如果你愿意,可以告诉我:
我可以直接给你对应的安装 + 测试命令。