温馨提示×

centos pytorch怎样测试

小樊
33
2026-10-26 21:46:16
栏目: 智能运维

在 CentOS 上测试 PyTorch 是否安装成功、能否正常跑(CPU / GPU),可以按下面步骤来。


一、基础环境确认

1. 查看 CentOS 版本

cat /etc/centos-release

2. 查看 Python 版本

python3 --version

(CentOS 7 默认可能是 python2,建议使用 python3)


二、确认 PyTorch 是否已安装

python3 -c "import torch; print(torch.__version__)"
  • 能输出版本号 → 安装成功
  • 报错 No module named torch → 未安装或未激活环境

三、CPU 版本测试(最基础)

python3 -c "import torch; print(torch.rand(3,3))"

或写个测试脚本 test_torch.py:

import torch

x = torch.rand(3, 3)
print(x)
print("PyTorch version:", torch.__version__)

运行:

python3 test_torch.py

四、GPU(CUDA)测试(如果你装的是 GPU 版)

1. 查看 CUDA 是否可用

python3 -c "import torch; print(torch.cuda.is_available())"
  • True → GPU 可用
  • False → 驱动/CUDA/PyTorch 版本不匹配

2. 查看 GPU 数量和设备名

python3 -c "import torch; print(torch.cuda.device_count()); print(torch.cuda.get_device_name(0))"

3. GPU 张量测试

import torch

x = torch.rand(3, 3).cuda()
print(x)
print(x.device)

五、常见问题(CentOS 特有)

1. GLIBC / libgomp 报错

CentOS 7 常见:

/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found

解决:

sudo yum install -y devtoolset-9
scl enable devtoolset-9 bash

2. 显卡驱动未装

nvidia-smi
  • 无输出 → 驱动未安装
  • 有输出 → 驱动正常

六、推荐安装方式(避免踩坑)

CPU 版

pip3 install torch torchvision torchaudio

GPU 版(示例 CUDA 11.8)

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

如果你愿意,可以告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • CPU 还是 GPU
  • 是否使用 conda / venv

我可以直接给你一套最适合你环境的测试命令。

0 踩