温馨提示×

Ubuntu系统中PyTorch更新与维护指南

小樊
60
2025-07-11 21:50:28
栏目: 智能运维

在Ubuntu系统中更新和维护PyTorch的指南如下:

更新PyTorch

使用pip更新

  1. 卸载旧版本
pip uninstall torch torchvision torchaudio 
  1. 检查当前版本
pip show torch 
  1. 升级pip(如果需要):
pip install --upgrade pip 
  1. 安装最新版本
pip install --upgrade torch torchvision torchaudio 

或者指定版本:

pip install torch==<version> torchvision==<version> torchaudio==<version> 
  1. 验证更新
pip show torch 

使用conda更新

  1. 激活conda环境(如果有的话):
conda activate your_environment_name 
  1. 卸载旧版本
conda remove pytorch torchvision torchaudio cudatoolkit 
  1. 安装最新版本
conda update pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch 

注意:cudatoolkit的版本应与你安装的CUDA版本相匹配。 4. 验证更新

conda list torch 

使用系统包管理器(不推荐)

  1. 卸载旧版本
sudo apt-get remove python3-torch python3-torchvision python3-torchaudio 
  1. 添加PyTorch PPA
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt-get update 
  1. 安装最新版本
sudo apt-get install python3-torch python3-torchvision python3-torchaudio 

维护PyTorch

创建和激活虚拟环境

  • 使用conda
conda create -n pytorch_env python=3.9 conda activate pytorch_env 
  • 使用venv
python3 -m venv pytorch_env source pytorch_env/bin/activate 

验证安装

  • 导入PyTorch并检查CUDA支持
import torch print(torch.__version__) print(torch.cuda.is_available()) 

注意事项

  • 在更新PyTorch之前,建议备份你的项目代码和数据。
  • 确保你的CUDA和cuDNN版本与新的PyTorch版本兼容。
  • 如果你在虚拟环境中工作,确保在更新PyTorch之前激活相应的虚拟环境。

通过以上步骤,你应该能够在Ubuntu系统上成功更新和维护PyTorch。

0