logo

PyTorch 环境

王哲峰 / 2022-07-11


目录

Windows 安装 PyTorch

CPU version

使用 pip 安装 PyTorch

$ pip3 install torch torchvision torchaudio

使用 conda 安装 PyTorch

$ conda install pytorch torchvision torchaudio cpuonly -c pytorch

使用 Docker 安装 PyTorch

$ TODO

GPU-CUDA version

使用 pip 安装 PyTorch

# CUDA 11.3
$ pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
# CUDA 11.6
$ pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
# CUDA 11.8
$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# CUDA 12.1
$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

使用 conda 安装 PyTorch

# CUDA 11.3
$ conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
# CUDA 11.6
$ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge
# CUDA 11.8
$ conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
# CUDA 12.1
$ conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

使用 Docker 安装 PyTorch

$ TODO

macOS 安装 PyTorch

使用 pip 安装 PyTorch

$ pip3 install torch torchvision torchaudio

使用 conda 安装 PyTorch

$ conda install pytorch::pytorch torchvision torchaudio -c pytorch

使用 Docker 安装 PyTorch

$ TODO

Linux 安装 PyTorch

CPU version

使用 pip 安装 PyTorch

$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

使用 conda 安装 PyTorch

$ conda install pytorch torchvision torchaudio cpuonly -c pytorch

使用 Docker 安装 PyTorch

$ TODO

GPU-CUDA version

使用 pip 安装 PyTorch

# CUDA 10.2
$ pip3 install torch torchvision torchaudio
# CUDA 11.3
$ pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
# CUDA 11.6
$ pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
# ROCm 5.1.1
$ pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/rocm5.1.1

# CUDA 11.8
$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# CUDA 12.1
$ pip3 install torch torchvision torchaudio
# ROCm 5.6
$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.6

使用 conda 安装 PyTorch

# CUDA 10.2
$ conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
# CUDA 11.3
$ conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
# CUDA 11.6
$ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge
# CUDA 11.8
$ conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
# CUDA 12.1
$ conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

使用 Docker 安装 PyTorch

$ TODO

Verification

>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)

tensor([[0.3380, 0.3845, 0.3217],
	[0.8337, 0.9050, 0.2650],
	[0.2979, 0.7141, 0.9069],
	[0.1449, 0.1132, 0.1375],
	[0.4675, 0.3947, 0.1426]])
>>> import torch
>>> torch.cuda.is_available()

官网安装介绍