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__)
,若输出版本号则安装成功。