在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官方文档或社区支持。