Ubuntu上PyTorch如何进行版本更新
导读:Ubuntu上PyTorch版本更新指南 一 更新前准备 明确包管理器与虚拟环境:确认使用的是 pip 还是 conda,并在需要更新的虚拟环境中操作(如 conda 需先激活环境)。 检查当前版本与 CUDA 信息: 查看 PyTor...
Ubuntu上PyTorch版本更新指南
一 更新前准备
- 明确包管理器与虚拟环境:确认使用的是 pip 还是 conda,并在需要更新的虚拟环境中操作(如 conda 需先激活环境)。
- 检查当前版本与 CUDA 信息:
- 查看 PyTorch 版本:
python -c "import torch; print(torch.__version__)" - 查看 CUDA 运行时版本:
python -c "import torch; print(torch.version.cuda)" - 查看驱动/CUDA 驱动版本:
nvidia-smi(右上角显示 Supported/Runtime CUDA)
- 查看 PyTorch 版本:
- 选择目标版本:优先前往 PyTorch 官网 Start Locally 选择与你系统、Python 版本、CUDA 版本匹配的安装命令,避免不兼容升级。
二 使用 pip 更新
- 升级到最新可用版本(同一大版本线内):
pip install --upgrade torch torchvision torchaudio
- 跨版本或需要指定 CUDA 时,直接安装对应构建(推荐从官网复制命令):
- CPU 版本:
pip install torch torchvision torchaudio - GPU 版本(示例为 CUDA 11.8):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
- CPU 版本:
- 遇到冲突或残留导致的安装失败时,可先卸载再装:
pip uninstall torch torchvision torchaudio- 然后按上面的安装命令重装
三 使用 Conda 更新
- 激活环境:
conda activate < your_env> - 升级到最新可用版本:
conda update pytorch torchvision torchaudio -c pytorch
- 指定 CUDA 工具链(示例为 11.8):
conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch -c nvidia
- 如需全新安装,也可先
conda remove pytorch torchvision torchaudio后按目标命令重装。
四 验证与常见问题
- 验证安装结果:
- 版本号:
python -c "import torch; print(torch.__version__)" - CUDA 可用性:
python -c "import torch; print(torch.cuda.is_available())"
- 版本号:
- 常见问题处理:
- 网络慢或超时:使用国内镜像(如清华源)加速 pip 安装,例如
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple(镜像仅加速 PyPI 包,GPU 版本仍需从 PyTorch 官方通道获取)
- 构建失败(如 “Could not build wheels for pytorch”):
- 升级 pip:
pip install --upgrade pip - 安装系统构建依赖:
sudo apt update & & sudo apt install -y build-essential - 清理缓存并重试:
pip install --no-cache-dir torch torchvision torchaudio
- 升级 pip:
- CUDA 不匹配:用
nvidia-smi与torch.version.cuda核对驱动/运行时与 PyTorch 构建是否一致,必要时选择对应的 cu118/cu121 等构建重装。
- 网络慢或超时:使用国内镜像(如清华源)加速 pip 安装,例如
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu上PyTorch如何进行版本更新
本文地址: https://pptw.com/jishu/782497.html
