温馨提示×

centos如何测试pytorch

小樊
33
2026-10-06 08:43:34
栏目: 智能运维

在 CentOS 上测试 PyTorch 是否安装成功,通常按下面的步骤来操作。


一、确认 Python 和 pip 环境

python3 --version
pip3 --version

如果没有 python3:

sudo yum install python3 python3-pip -y

二、安装 PyTorch(如未安装)

CPU 版本(推荐先测这个)

pip3 install torch torchvision torchaudio

GPU 版本(有 NVIDIA 显卡 + 驱动)

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

(cu118 对应 CUDA 11.8,按需调整)


三、测试 PyTorch 是否可用

新建测试脚本 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)

五、常见问题(CentOS)

1. 缺少依赖库

sudo yum install -y libgomp glibc

2. GLIBC 版本过低

CentOS 7 可能不支持最新 PyTorch,建议:

  • 使用较旧 PyTorch 版本
  • 或升级到 CentOS 8 / Rocky / AlmaLinux

3. 虚拟环境(推荐)

python3 -m venv venv
source venv/bin/activate
pip install torch

如果你告诉我:

  • CentOS 版本(7 / 8)
  • 是否有 GPU
  • 是否已安装 CUDA

我可以给你 更精确的命令。

0 踩