在CentOS上安装PyTorch,你可以按照以下步骤进行:
更新系统包:
sudo yum update -y
安装依赖项:
sudo yum install -y python3 python3-pip python3-devel
安装PyTorch: PyTorch官方提供了多种安装方式,你可以根据你的CUDA版本选择合适的命令。以下是一些常见的安装命令:
CPU版本:
pip3 install torch torchvision torchaudio
CUDA 11.3版本:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
CUDA 11.6版本:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
CUDA 11.7版本:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
CUDA 12.0版本:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu120
你可以根据你的GPU和CUDA版本选择合适的命令。
如果你已经安装了Anaconda或Miniconda,可以使用conda来安装PyTorch,这样可以更方便地管理依赖项。
更新conda:
conda update conda
创建一个新的conda环境(可选):
conda create -n pytorch_env python=3.9
conda activate pytorch_env
安装PyTorch: 同样,根据你的CUDA版本选择合适的命令:
CPU版本:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
CUDA 11.3版本:
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
CUDA 11.6版本:
conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch
CUDA 11.7版本:
conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
CUDA 12.0版本:
conda install pytorch torchvision torchaudio cudatoolkit=12.0 -c pytorch
安装完成后,你可以通过以下命令验证PyTorch是否安装成功:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # 如果使用GPU版本,应该返回True
如果一切正常,你应该能够看到PyTorch的版本号,并且在使用GPU版本时,torch.cuda.is_available()
应该返回True
。
希望这些步骤能帮助你在CentOS上成功安装PyTorch!