在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 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
安裝PyTorch: 同樣根據你的CUDA版本選擇合適的安裝命令。以下是一些常見的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 8.0:
conda install pytorch torchvision torchaudio cudatoolkit=8.0 -c pytorch
你可以訪問PyTorch官網獲取最新的安裝命令和詳細信息。
安裝完成后,可以通過以下代碼驗證PyTorch是否安裝成功:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # 檢查是否有可用的GPU
如果輸出顯示了PyTorch的版本號,并且torch.cuda.is_available()返回True(如果你安裝的是GPU版本),則說明安裝成功。
希望這些步驟能幫助你在Linux環境下成功安裝PyTorch!