ubuntu

Ubuntu中PyTorch的安装步骤是什么

小樊
40
2025-08-26 00:25:37
栏目: 智能运维

Ubuntu中安装PyTorch有使用pip和conda两种常用方法,以下是具体步骤:

使用pip安装

  1. 更新系统包列表sudo apt update
  2. 安装Python和pipsudo apt install python3 python3-pip
  3. 安装PyTorch
    • CPU版本pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
    • GPU版本:先安装对应CUDA版本,再运行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版本)。
  4. 验证安装:在Python解释器中输入import torch,再输入print(torch.__version__),若输出版本号则安装成功。

使用conda安装

  1. 安装Miniconda或Anaconda:从官网下载安装包,按提示操作。
  2. 创建新环境conda create -n pytorch_env python=3.9(版本号可自定)。
  3. 激活环境conda activate pytorch_env
  4. 安装PyTorch
    • CPU版本conda install pytorch torchvision torchaudio cpuonly -c pytorch
    • GPU版本conda install pytorch torchvision torchaudio cudatoolkit=版本号 -c pytorch,如conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
  5. 验证安装:在Python解释器中输入import torch,再输入print(torch.__version__),若输出版本号则安装成功。

0
看了该问题的人还看了