Skip to content

Commit b3d76db

Browse files
committed
Python实战-pytorch安装慢
1 parent 7716024 commit b3d76db

3 files changed

Lines changed: 142 additions & 5 deletions

File tree

README.md

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ Out[15]: [0, 9, 7, 5]
16231623

16241624
频繁使用同一切片的操作可使用slice对象抽出来,复用的同时还能提高代码可读性。
16251625

1626-
#### 87 **判断str1是否为str2的permutation**
1626+
#### 87 str1是否为str2的permutation
16271627

16281628
排序词(permutation):两个字符串含有相同字符,但字符顺序不同。
16291629

@@ -1667,7 +1667,34 @@ print(r) # False
16671667
```
16681668
以上就是使用defaultdict的小例子,希望对读者朋友理解此类型有帮助。
16691669

1670+
#### 88 str1是否由str2旋转而来
16701671

1672+
`stringbook`旋转后得到`bookstring`,写一段代码验证`str1`是否为`str2`旋转得到。
1673+
1674+
**思路**
1675+
1676+
转化为判断:`str1`是否为`str2+str2`的子串
1677+
1678+
```python
1679+
def is_rotation(s1: str, s2: str) -> bool:
1680+
if s1 is None or s2 is None:
1681+
return False
1682+
if len(s1) != len(s2):
1683+
return False
1684+
1685+
def is_substring(s1: str, s2: str) -> bool:
1686+
return s1 in s2
1687+
return is_substring(s1, s2 + s2)
1688+
```
1689+
1690+
**测试**
1691+
```python
1692+
r = is_rotation('stringbook', 'bookstring')
1693+
print(r) # True
1694+
1695+
r = is_rotation('greatman', 'maneatgr')
1696+
print(r) # False
1697+
```
16711698

16721699
### 二、Python字符串和正则
16731700

@@ -5717,7 +5744,98 @@ channels:
57175744
57185745
Done~
57195746
5720-
#### 2 自动群发邮件
5747+
#### 2 pytorch慢到无法安装,怎么办?
5748+
5749+
**1 安装慢到装不上**
5750+
5751+
最近几天,后台几个小伙伴问我,无论pip还是conda安装`pytorch`都太慢了,都是安装官方文档去做的,就是超时装不上,无法开展下一步,卡脖子的感觉太不好受。
5752+
5753+
这些小伙伴按照pytorch官档提示,选择好后,完整复制上面命令`conda install pytorch torchvision cudatoolkit=10.1 -c pytorch`到cmd中,系统是windows.
5754+
5755+
![image-20200128182456390](./img/image-20200128182456390.png)
5756+
5757+
接下来提示,conda需要安装的包,他们操作选择`y`,继续安装,但是在安装时,发现进度条几乎一动不动。
5758+
5759+
反复尝试,就是这样,有些无奈,还感叹怎么深度学习的路一开始就TMD的这么难!
5760+
5761+
5762+
**2 这样能正常安装**
5763+
5764+
无论是安装`cpu`版还是`cuda`版,网上关于这些的参考资料太多了,无非就是cuda硬件和cuda开发包的版本要对应,python版本要对应等,这些bee君觉得都不是事。
5765+
5766+
就像几位读者朋友遇到的问题,关键还是如何解决`慢到无法装`的问题。
5767+
5768+
最有效方法是添加镜像源,常见的清华或中科大。
5769+
5770+
先查看是否已经安装相关镜像源,windows系统在`cmd`窗口中执行命令:
5771+
5772+
```python
5773+
conda config --show
5774+
```
5775+
5776+
bee君这里显示:
5777+
```python
5778+
channels:
5779+
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
5780+
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
5781+
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
5782+
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
5783+
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
5784+
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
5785+
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
5786+
```
5787+
说明已经安装好清华的镜像源。如果没有安装,请参考下面命令安装源:
5788+
```python
5789+
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
5790+
```
5791+
依次安装上面所有的源。
5792+
5793+
并设置搜索时显示通道地址,执行下面命令:
5794+
5795+
```python
5796+
conda config --set show_channel_urls yes
5797+
```
5798+
5799+
**3 最关键一步**
5800+
5801+
有的读者问我,他们已经都安装好镜像源,但是为什么安装还是龟速?问他们,是用哪个命令,他们回复:`conda install pytorch torchvision cudatoolkit=10.1 -c pytorch`
5802+
5803+
好吧,执行上面命令,因为命令最后是`-c pytorch`,所以默认还是从conda源下载,新安装的清华等源没有用上。
5804+
5805+
正确命令:`conda install pytorch torchvision cudatoolkit=10.1`,也就是去掉`-c pytorch`
5806+
5807+
并且在安装时,也能看到使用了清华源。并且安装速度直线提升,顺利done
5808+
5809+
**4 测试是否安装成功**
5810+
5811+
结合官档,执行下面代码,`torch.cuda.is_available()`返回`True`,说明安装cuda成功。
5812+
5813+
```python
5814+
In [1]: import torch
5815+
5816+
In [2]: torch.cuda
5817+
Out[2]: <module 'torch.cuda' from 'D:\\Programs\\anaconda\\lib\\site-packages\\torch\\cuda\\__init__.py'>
5818+
5819+
In [3]: torch.cuda.is_available()
5820+
Out[3]: True
5821+
5822+
In [4]: from __future__ import print_function
5823+
5824+
In [5]: x = torch.rand(5,3)
5825+
5826+
In [6]: print(x)
5827+
tensor([[0.0604, 0.1135, 0.2656],
5828+
[0.5353, 0.9246, 0.3004],
5829+
[0.4872, 0.9592, 0.2215],
5830+
[0.2598, 0.5031, 0.6093],
5831+
[0.2986, 0.1599, 0.5862]])
5832+
```
5833+
5834+
这篇文章主要讨论安装`pytorch`慢到不能装的问题及方案,希望对读者朋友们有帮助。
5835+
5836+
5837+
5838+
#### 3 自动群发邮件
57215839
57225840
Python自动群发邮件
57235841
@@ -5769,7 +5887,7 @@ sender_mail()
57695887
57705888
57715889
5772-
#### 3 二分搜索
5890+
#### 4 二分搜索
57735891
57745892
二分搜索是程序员必备的算法,无论什么场合,都要非常熟练地写出来。
57755893
@@ -5830,7 +5948,7 @@ found 100 at 6
58305948
Out[11]: 6
58315949
```
58325950
5833-
#### 4 爬取天气数据并解析温度值
5951+
#### 5 爬取天气数据并解析温度值
58345952
58355953
爬取天气数据并解析温度值
58365954
@@ -5943,7 +6061,7 @@ Name: temperature, dtype: object
59436061
59446062
59456063
5946-
#### 5 制作小而美的计算器
6064+
#### 6 制作小而美的计算器
59476065
59486066
1) ui设计
59496067

img/image-20200128182456390.png

186 KB
Loading

src/str_rotation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
def is_rotation(s1: str, s2: str) -> bool:
4+
if s1 is None or s2 is None:
5+
return False
6+
if len(s1) != len(s2):
7+
return False
8+
9+
def is_substring(s1: str, s2: str) -> bool:
10+
return s1 in s2
11+
return is_substring(s1, s2 + s2)
12+
13+
14+
# 测试
15+
r = is_rotation('stringbook', 'bookstring')
16+
print(r) # True
17+
18+
r = is_rotation('greatman', 'maneatgr')
19+
print(r) # False

0 commit comments

Comments
 (0)