在Linux上安裝PyTorch的步驟如下:
更新pip:
pip install --upgrade pip
安裝PyTorch: 根據你的CUDA版本選擇合適的命令。你可以在PyTorch官網找到最新的安裝命令。
CPU版本:
pip install torch torchvision torchaudio
GPU版本(CUDA 11.3):
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
GPU版本(CUDA 11.7):
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
GPU版本(CUDA 10.2):
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu102
GPU版本(CUDA 9.0):
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu90
CPU版本(無CUDA):
pip install torch torchvision torchaudio
如果你使用Anaconda或Miniconda,可以通過conda來安裝PyTorch。
更新conda:
conda update conda
創建新的conda環境(可選):
conda create -n pytorch_env python=3.8
conda activate pytorch_env
安裝PyTorch: 根據你的CUDA版本選擇合適的命令。你可以在PyTorch官網找到最新的安裝命令。
CPU版本:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
GPU版本(CUDA 11.3):
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
GPU版本(CUDA 11.7):
conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
GPU版本(CUDA 10.2):
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
GPU版本(CUDA 9.0):
conda install pytorch torchvision torchaudio cudatoolkit=9.0 -c pytorch
安裝完成后,你可以通過以下命令驗證PyTorch是否安裝成功:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # 如果安裝了GPU版本,應該返回True
如果一切正常,你應該能夠看到PyTorch的版本號,并且torch.cuda.is_available()
會返回True
(如果你安裝的是GPU版本)。
希望這些步驟能幫助你在Linux上成功安裝PyTorch!