前言:
为了解决部分公共服务器上不让用梯子,如果网络不好,只能手动安装pytorch的方式
环境:
Linux CUDA:11.3 (其它版本同样适配,只要找相应的版本就行)
方法
-
创建conda环境(xxx自己替换)
conda create -n <name> python=3.xx
-
进入环境
conda activate <name>
-
由于网络问题,所以要去官网用电脑手动下载wheel用pip来安装
-
可以先试试直接安装:Previous PyTorch Versions | PyTorch
不过多半会失败
-
在资源地址中找到whl安装包来手动安装,torch,torchvision,torchaudio要找到和自己CUDA版本一致的资源安装包,全部下载下来
资源地址:download.pytorch.org/whl/torch_stable.htmltorch,torchvision,torchaudio版本匹配地址(版本要一致!):pytorch/vision: Datasets, Transforms and Models specific to Computer Vision (github.com)
-
下载3个资源torch, torchvision,torchaudio之后将文件复制到服务器上自己创建的whl文件夹下,并在whl文件夹下执行以下命令
pip install torch-1.12.1+cu113-cp310-cp310-linux_x86_64.whl torchaudio-0.12.1+cu113-cp310-cp310-linux_x86_64.whl torchvision-0.13.1+cu113-cp310-cp310-linux_x86_64.whl
如果下载过慢的话可以换个源(会生成一个~/.config/pip/pip.conf配置文件并写入)
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple pip config set install.trusted-host mirrors.aliyun.com
-
测试
import torch def main(): if torch.cuda.is_available(): print(torch.tensor([1,2,3]).to('cuda')) if __name__ == '__main__': main()