ubuntu

Ubuntu中PyTorch依赖怎么安装

小樊
64
2025-04-24 01:29:50
栏目: 智能运维

在Ubuntu系统中安装PyTorch及其依赖,可以按照以下步骤进行:

方法一:使用pip安装

  1. 更新系统包列表

    sudo apt update
    
  2. 安装必要的依赖项

    sudo apt install python3-pip python3-dev
    
  3. 安装PyTorch PyTorch官方提供了多种安装方式,可以根据你的CUDA版本选择合适的命令。以下是一些常见的安装命令:

    • CPU版本

      pip3 install torch torchvision torchaudio
      
    • CUDA 11.3版本

      pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
      
    • CUDA 11.7版本

      pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
      
    • CUDA 12.0版本

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

    请根据你的GPU和CUDA版本选择合适的命令。

方法二:使用conda安装

如果你更喜欢使用Anaconda或Miniconda来管理Python环境和包,可以按照以下步骤进行:

  1. 安装Anaconda或Miniconda

  2. 创建新的conda环境

    conda create -n pytorch_env python=3.8
    
  3. 激活环境

    conda activate pytorch_env
    
  4. 安装PyTorch 同样,根据你的CUDA版本选择合适的命令:

    • CPU版本

      conda install pytorch torchvision torchaudio cpuonly -c pytorch
      
    • CUDA 11.3版本

      conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
      
    • CUDA 11.7版本

      conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
      
    • CUDA 12.0版本

      conda install pytorch torchvision torchaudio cudatoolkit=12.0 -c pytorch
      

验证安装

安装完成后,可以通过以下命令验证PyTorch是否安装成功:

import torch
print(torch.__version__)
print(torch.cuda.is_available())  # 如果使用GPU版本,应该返回True

如果一切正常,你应该能够看到PyTorch的版本号以及GPU是否可用的信息。

希望这些步骤能帮助你在Ubuntu系统中成功安装PyTorch及其依赖!

0
看了该问题的人还看了