在Linux環境下安裝PyTorch,可以按照以下步驟進行:
更新pip:
pip install --upgrade pip
安裝PyTorch: 根據你的CUDA版本選擇合適的命令。以下是一些常見的CUDA版本對應的安裝命令:
無GPU(CPU版本):
pip install torch torchvision torchaudio
CUDA 11.3:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
CUDA 11.2:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu112
CUDA 11.1:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu111
CUDA 10.2:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu102
CUDA 10.1:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu101
CUDA 9.2:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu92
CUDA 9.1:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu91
CUDA 8.0:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu80
你可以訪問PyTorch官網獲取最新的安裝命令。
如果你使用的是Anaconda或Miniconda,可以使用conda來安裝PyTorch:
更新conda:
conda update conda
創建新的conda環境(可選):
conda create -n pytorch_env python=3.8
conda activate pytorch_env
安裝PyTorch: 根據你的CUDA版本選擇合適的命令:
無GPU(CPU版本):
conda install pytorch torchvision torchaudio cpuonly -c pytorch
CUDA 11.3:
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
CUDA 11.2:
conda install pytorch torchvision torchaudio cudatoolkit=11.2 -c pytorch
CUDA 11.1:
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch
CUDA 10.2:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
CUDA 10.1:
conda install pytorch torchvision torchaudio cudatoolkit=10.1 -c pytorch
CUDA 9.2:
conda install pytorch torchvision torchaudio cudatoolkit=9.2 -c pytorch
CUDA 9.1:
conda install pytorch torchvision torchaudio cudatoolkit=9.1 -c pytorch
CUDA 8.0:
conda install pytorch torchvision torchaudio cudatoolkit=8.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!