Ubuntu中安裝PyTorch有使用pip和conda兩種常用方法,以下是具體步驟:
sudo apt update
。sudo apt install python3 python3-pip
。pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
。pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu版本號
,如pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
(CUDA 11.7版本)。import torch
,再輸入print(torch.__version__)
,若輸出版本號則安裝成功。conda create -n pytorch_env python=3.9
(版本號可自定)。conda activate pytorch_env
。conda install pytorch torchvision torchaudio cpuonly -c pytorch
。conda install pytorch torchvision torchaudio cudatoolkit=版本號 -c pytorch
,如conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
。import torch
,再輸入print(torch.__version__)
,若輸出版本號則安裝成功。