在国内,由于网络限制,直接从 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 代码检查是否正常工作。