在Ubuntu上安裝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官網獲取最新的安裝命令。
驗證安裝: 打開Python解釋器并嘗試導入PyTorch:
import torch
print(torch.__version__)
如果你使用Anaconda或Miniconda,可以使用conda來安裝PyTorch:
更新conda:
conda update conda
創建新的conda環境(可選):
conda create -n pytorch_env python=3.8
conda activate pytorch_env
安裝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 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官網獲取最新的安裝命令。
驗證安裝: 打開Python解釋器并嘗試導入PyTorch:
import torch
print(torch.__version__)
通過以上步驟,你應該能夠在Ubuntu上成功安裝PyTorch。如果有任何問題,請參考PyTorch官方文檔或社區支持。