温馨提示×

centos怎么测pytorch

小樊
58
2026-09-30 13:14:34
栏目: 智能运维

在 CentOS 上测试(验证)PyTorch 是否可用,通常分几步来做。下面按最常见场景说明(CPU / GPU 都覆盖)。


一、确认 Python 和 PyTorch 已安装

1. 查看 Python 版本

python3 --version

2. 进入 Python 环境

python3

或(如果你用虚拟环境):

source your_env/bin/activate
python

二、基础测试(是否安装成功)

在 Python 交互环境中输入:

import torch
print(torch.__version__)

✅ 能正常输出版本号(如 2.1.0),说明安装成功。


三、CPU 测试

x = torch.rand(3, 3)
y = torch.rand(3, 3)
print(x + y)

如果没有报错,说明 CPU 版 PyTorch 正常。


四、GPU(CUDA)测试(CentOS 常见)

1. 先确认是否有 NVIDIA 显卡

nvidia-smi

✅ 能看到显卡信息说明驱动已装。


2. PyTorch 是否识别 CUDA

import torch
print(torch.cuda.is_available())
  • True → CUDA 可用
  • False → 要么装的是 CPU 版,要么驱动/CUDA 不匹配

3. GPU 张量测试

if torch.cuda.is_available():
    x = torch.rand(3, 3).cuda()
    y = torch.rand(3, 3).cuda()
    print(x + y)
    print(torch.cuda.get_device_name(0))

五、常见 CentOS 问题排查

❌ 1. libstdc++.so.6: version 'GLIBCXX_3.4.21' not found

CentOS 7 常见,升级 GCC:

sudo yum install centos-release-scl
sudo yum install devtoolset-9
scl enable devtoolset-9 bash

❌ 2. torch.cuda.is_available() 为 False

检查:

  • 是否安装 GPU 版 PyTorch
  • CUDA 版本是否匹配
nvcc --version

❌ 3. 无网环境

用离线 wheel 安装:

pip install torch-xxx.whl

六、一键测试脚本(推荐)

保存为 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))
    x = torch.rand(3, 3).cuda()
    print(x)
else:
    x = torch.rand(3, 3)
    print(x)

运行:

python3 test_torch.py

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

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

我可以给你精确安装 + 测试命令。

0 踩