1.问题

使用命令安装torch:

pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio===0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html -i http://mirrors.aliyun.com/pypi/simple/  --trusted-host mirrors.aliyun.com

这时候报错:

pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='download.pytorch.org', port=443): Read timed out.

2.分析问题产生原因

(1)大概率是网络问题,也就是阿里云的镜像源提供的网速不足以支撑2个G的torch安装
(2)也许可能版本不匹配,但这一问题我在官方网站复制的命令:torch安装官网提供的命令,应该是问题不大的。

3.解决办法

切换镜像源:
豆瓣:http://pypi.douban.com/simple/
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
山东理工大学:http://pypi.sdutlinux.org/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:http://pypi.hustunique.com/
因此命令可以改为:

pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio===0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html -i https://pypi.tuna.tsinghua.edu.cn/simple

或者一个一个镜像试过去,总会有效果的!

4.大牛提供的方法

如果你的网速一直很拉闸,上面的方法依然解决不了,那这时可以使用如下的解决方案:

pip --default-timeout=1688 install 库名称 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 

那在这里可以分别用该方法安装torch和torchvision
也就是使用命令:

pip3 --default-timeout=1688 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio===0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html -i http://mirrors.aliyun.com/pypi/simple/  --trusted-host mirrors.aliyun.com

这里的--default-timeout=1688其实意思就是让其检测延迟的时间变长,以防止因为你的网络问题而直接报错。以上有可能也会报错,因为你所选用的镜像源是不存在这样的包的,这时候可以切换镜像源。如下所示:
在这里插入图片描述

还没有使用这个技巧之前,前面好多次都是安装在了600多M就因为网络问题停止安装了,有一次甚至都2000多M了以为要成功的时候GG了,真的气哭了。

5.安装成果展示

在这里插入图片描述

虽然安装成功了,但新的风暴又产生了:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
projectq 0.7.0 requires matplotlib>=2.2.3, which is not installed.
projectq 0.7.0 requires networkx>=2, which is not installed.
projectq 0.7.0 requires requests, which is not installed.
projectq 0.7.0 requires scipy, which is not installed.

不过这个风暴,直接使用安装matplotlib包,其他几个包的命令也如下所示:

pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install networkx -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple

依次输入上面的命令将报错的几个包全部安装成功就🆗了!
最后,在命令行下输入python:

(torchlearning) C:\WINDOWS\system32>python

然后进入python操作行输入以下代码:

import torch
x = torch.rand(5, 3)
print(x)

在这里插入图片描述
出现以上结果就表示torch终于TM的安装成功了!!!

6. 如何使用GPU版本的torch

详见:基于torch的GPU版本快速搭建CNN,实现分类MINST数据集

7. 验证torch的cuda版本是否成功

在操作界面输入以下代码:

import torch
print(torch.__version__)
print(torch.cuda.is_available())

如果出现以下界面,则表示cuda对应上了torch版本:
在这里插入图片描述
关于如何安装cuda:cuda保姆安装教程

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐