在國內,由于網絡限制,直接從 PyTorch 官方源下載可能會遇到速度慢或無法訪問的問題。為了解決這一問題,可以使用國內鏡像源來加速下載和安裝 PyTorch。
文章目錄
- 安裝指定版本的 PyTorch(以 CUDA 11.8 為例)
- 安裝 CPU 版本
- 驗證是否安裝成功
安裝指定版本的 PyTorch(以 CUDA 11.8 為例)
pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 -f https://mirrors.aliyun.com/pytorch-wheels/cu118/
此命令中的 -f 參數用于指定包含包文件的頁面,而不是使用 --index-url。
上海交通大學鏡像
pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 -f https://mirror.sjtu.edu.cn/pytorch-wheels/cu118
pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 -f https://mirror.sjtu.edu.cn/pytorch-wheels/torch_stable.html
清華大學鏡像
pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
-i 參數用于指定 PyPI 鏡像源地址。
安裝 CPU 版本
conda install pytorch torchvision torchaudio cpuonly -c pytorch
驗證是否安裝成功
import torch
print(torch.cuda.is_available())
print("CUDA Version:", torch.version.cuda)
注意事項
- 鏡像源選擇:根據網絡環境選擇最適合的鏡像源,例如阿里云、清華大學或上海交大。
- 版本兼容性:確保安裝的 PyTorch 版本與系統的 CUDA 版本和 Python 版本兼容。
- 安裝驗證:安裝完成后,運行簡單的 PyTorch 代碼檢查是否正常工作。