Linux上升级 PyTorch 的简明步骤
一 准备与环境隔离
python3 -m venv ~/venvs/pt-upgradesource ~/venvs/pt-upgrade/bin/activatepython - <<'PY' import torch print("PyTorch:", torch.__version__) print("CUDA available:", torch.cuda.is_available()) if torch.cuda.is_available(): print("CUDA runtime:", torch.version.cuda) PYnvidia-smi。二 使用 pip 升级
pip install --upgrade torch torchvision torchaudiopip install --upgrade torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118pip install --upgrade torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121pip uninstall torch torchvision torchaudio -y三 使用 conda 升级
conda install pytorch torchvision torchaudio -c pytorchconda install pytorch=2.3.1 torchvision=0.18.1 torchaudio=2.3.1 -c pytorch四 验证与常见问题
python - <<'PY' import torch print("PyTorch:", torch.__version__) print("CUDA available:", torch.cuda.is_available()) if torch.cuda.is_available(): print("CUDA runtime:", torch.version.cuda) PYpython - <<'PY' import torch x = torch.randn(2,3).cuda() print("GPU tensor:", x.device, x.shape) PYtorch.cuda.is_available() 为 False:多半是安装了不含 CUDA 的构建。请使用带 --index-url https://download.pytorch.org/whl/cu118 或 cu121 的命令重新安装,确保与驱动支持匹配(见上文 nvidia-smi 输出)。pip uninstall 相关包,或在干净虚拟环境重装;必要时升级冲突依赖(如 numpy)后再装 PyTorch。pip install --upgrade torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple(注意镜像可能滞后,GPU 构建建议优先官方源)。