CentOS 中 PyTorch 库更新攻略
一 更新前准备
pip freeze > requirements.txt 或 conda env export > env.yml。pip install -U pip),并准备编译依赖(如 gcc、openssl-devel、bzip2-devel、libffi-devel)。二 标准更新流程
pip show torchpip install --upgrade torch torchvision torchaudiopython - <<'PY' import torch print("torch:", torch.__version__) print("cuda:", torch.cuda.is_available()) PYpip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0conda activate <your_env>conda list torchconda update pytorch torchvision torchaudio cudatoolkit=<your_cuda_version> -c pytorchconda install pytorch torchvision torchaudio cpuonly -c pytorchpython -c "import torch; print(torch.__version__, torch.cuda.is_available())"pip uninstall torch torchvision torchaudio 或 conda remove pytorch torchvision torchaudio cudatoolkit三 命令模板与版本选择
| 场景 | 包管理 | 命令示例 |
|---|---|---|
| CPU | pip | pip install torch torchvision torchaudio |
| CPU | conda | conda install pytorch torchvision torchaudio cpuonly -c pytorch |
| GPU | pip | pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118 |
| GPU | conda | conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch |
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ && conda config --set show_channel_urls yes-i https://pypi.tuna.tsinghua.edu.cn/simple。四 验证与回滚
python - <<'PY' import torch print("torch:", torch.__version__) print("cuda:", torch.cuda.is_available()) print("cudnn:", torch.backends.cudnn.version() if torch.cuda.is_available() else "N/A") PYpython - <<'PY' import torch x = torch.randn(2,3).cuda() if torch.cuda.is_available() else torch.randn(2,3) print(x @ x.T) PYtorch==2.1.0)。pip install "numpy<2.0" 再复测。pip install torch==<old_version> torchvision==<old_version> torchaudio==<old_version>conda install pytorch=<old_version> torchvision=<old_version> torchaudio=<old_version> cudatoolkit=<old_version> -c pytorch,或使用已导出的 env.yml 重建环境:conda env create -f env.yml。