溫馨提示×

如何在Linux上安裝PyTorch

小樊
62
2025-04-25 20:05:38
欄目: 智能運維

在Linux上安裝PyTorch的步驟如下:

方法一:使用pip安裝

  1. 更新pip

    pip install --upgrade pip
    
  2. 安裝PyTorch: 根據你的CUDA版本選擇合適的命令。你可以在PyTorch官網找到最新的安裝命令。

    • CPU版本

      pip install torch torchvision torchaudio
      
    • GPU版本(CUDA 11.3)

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

      pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
      
    • GPU版本(CUDA 10.2)

      pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu102
      
    • GPU版本(CUDA 9.0)

      pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu90
      
    • CPU版本(無CUDA)

      pip install torch torchvision torchaudio
      

方法二:使用conda安裝

如果你使用Anaconda或Miniconda,可以通過conda來安裝PyTorch。

  1. 更新conda

    conda update conda
    
  2. 創建新的conda環境(可選)

    conda create -n pytorch_env python=3.8
    conda activate pytorch_env
    
  3. 安裝PyTorch: 根據你的CUDA版本選擇合適的命令。你可以在PyTorch官網找到最新的安裝命令。

    • CPU版本

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

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

      conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
      
    • GPU版本(CUDA 10.2)

      conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
      
    • GPU版本(CUDA 9.0)

      conda install pytorch torchvision torchaudio cudatoolkit=9.0 -c pytorch
      

驗證安裝

安裝完成后,你可以通過以下命令驗證PyTorch是否安裝成功:

import torch
print(torch.__version__)
print(torch.cuda.is_available())  # 如果安裝了GPU版本,應該返回True

如果一切正常,你應該能夠看到PyTorch的版本號,并且torch.cuda.is_available()會返回True(如果你安裝的是GPU版本)。

希望這些步驟能幫助你在Linux上成功安裝PyTorch!

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女