前言
pytorch的cpu的包可以在国内镜像上下载,但是gpu版的包只能通过国外镜像下载,网上查了很多教程,基本都是手动从先将gpu版whl包下载下来,然后再手动安装,如何最快的通过pip的命令安装呢?下面我细细讲下。
解决办法
注意:此方法只能支持torch<=2.3.1的版本,更高的gpu版本阿里云上没有,只能从pytorch官网下载。
目前国内有pytorch的gpu版的whl包只有阿里云上的:https://mirrors.aliyun.com/pytorch-wheels/
这里以11.8为例:
官网中,通过pip命令安装的torch包命令如下:Previous PyTorch Versions | PyTorch
# CUDA 11.8
pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 --index-url https://download.pytorch.org/whl/cu118
然后,网上的各种教程,都是说,直接将https://download.pytorch.org/whl/cu118,改成https://mirrors.aliyun.com/pytorch-wheels/cu118,会直接报如下错误:
哈哈,看到这里,恭喜你被忽悠了。
正确的命令是这样的:
pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 -f https://mirrors.aliyun.com/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
完美解决!!!