Skip to content

Commit 67e077d

Browse files
committed
add file
1 parent b96efa9 commit 67e077d

2 files changed

Lines changed: 144 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
| <a href="https://www.bilibili.com/video/BV1AK4y1P7vs?spm_id_from=333.999.0.0"> <img src="./imgs/cover/16.png" width="170" /></a> | [16-Pytorch神经网络基础](notes/16-Pytorch神经网络基础.md) | [讲义](https://zh-v2.d2l.ai/chapter_deep-learning-computation/model-construction.html) | [Jupyter 代码](code/16-Pytorch神经网络基础) | <a href="https://github.com/Atream"> <img src="./imgs/profile/BoXin_Zhang.png" width="80" /></a> <a href="https://github.com/benmagnifico"><img src="./imgs/profile/JingGuang_Li.png" width="80" /></a> |
9292
| <a href="https://www.bilibili.com/video/BV1z5411c7C1?spm_id_from=333.999.0.0"> <img src="./imgs/cover/17.png" width="170" /></a> | [17-使用和购买GPU](notes/17-使用和购买GPU.md) | [讲义](https://zh-v2.d2l.ai/chapter_deep-learning-computation/use-gpu.html) | [Jupyter 代码](code/17-使用和购买GPU.ipynb) | <a href="https://github.com/dcy-dhsunabzh"> <img src="./imgs/profile/ChenYang_Ding.png" width="80" /></a> <a href="https://github.com/fghccv"><img src="./imgs/profile/ShiQi_Zhou.png" width="80" /></a> |
9393
| <a href="https://www.bilibili.com/video/BV15Q4y1o7vc?spm_id_from=333.999.0.0"> <img src="./imgs/cover/18.png" width="170" /></a> | [18-预测房价竞赛总结](notes/18-预测房价竞赛总结.md) | [讲义](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_1.pdf) | | <a href="https://github.com/Chigland"> <img src="./imgs/profile/ZhiTao_Wang.png" width="80" /></a> <a href="https://github.com/Khadorstorm"><img src="./imgs/profile/KeCheng_Zhang.png" width="80" /></a> |
94-
| <a href="https://www.bilibili.com/video/BV1L64y1m7Nh?spm_id_from=333.999.0.0"> <img src="./imgs/cover/19.png" width="170" /></a> | [19-卷积层](code/19-卷积层.ipynb) | [讲义1](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_2.pdf) [2](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_3.pdf) | [Jupyter 代码](code/19-卷积层.ipynb) | <a href="https://github.com/120L031815"> <img src="./imgs/profile/RuiChen_Yi.png" width="80" /></a> |
94+
| <a href="https://www.bilibili.com/video/BV1L64y1m7Nh?spm_id_from=333.999.0.0"> <img src="./imgs/cover/19.png" width="170" /></a> | [19-卷积层](notes/19-卷积层.md) | [讲义1](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_2.pdf) [2](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_3.pdf) | [Jupyter 代码](code/19-卷积层.ipynb) | <a href="https://github.com/120L031815"> <img src="./imgs/profile/RuiChen_Yi.png" width="80" /></a> |
9595
| <a href="https://www.bilibili.com/video/BV1Th411U7UN?spm_id_from=333.999.0.0"> <img src="./imgs/cover/20.png" width="170" /></a> | [20-填充和步幅](notes/20-填充和步幅.md) | [讲义](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_4.pdf) | [Jupyter 代码](code/20-填充和步幅.ipynb) | <a href="https://github.com/Aleafy"> <img src="./imgs/profile/Ye_Fang.png" width="80" /></a> |
9696
| <a href="https://www.bilibili.com/video/BV1MB4y1F7of?spm_id_from=333.999.0.0"> <img src="./imgs/cover/21.png" width="170" /></a> | [21-多输入输出通道](notes/21-多输入输出通道.md) | [讲义](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_5.pdf) | [Jupyter 代码](code/21-多输入输出通道.ipynb) | <a href="https://github.com/kinza99"> <img src="./imgs/profile/He_Du.png" width="80" /></a> <a href="https://github.com/chase6666"><img src="./imgs/profile/Kuo_Tian.png" width="80" /></a> |
9797
| <a href="https://www.bilibili.com/video/BV1EV411j7nX?spm_id_from=333.999.0.0"> <img src="./imgs/cover/22.png" width="170" /></a> | [22-池化层](notes/22-池化层.md) | [讲义](https://courses.d2l.ai/zh-v2/assets/pdfs/part-1_6.pdf) | [Jupyter 代码](code/22-池化层.ipynb) | <a href="https://github.com/Atream"> <img src="./imgs/profile/BoXin_Zhang.png" width="80" /></a> <a href="https://github.com/benmagnifico"><img src="./imgs/profile/JingGuang_Li.png" width="80" /></a> |

notes/19-卷积层.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
## 19-卷积层
2+
3+
#### 本讲文字介绍部分请参考沐神在线书籍~https://zh-v2.d2l.ai/chapter_convolutional-neural-networks/why-conv.html
4+
5+
#### 代码
6+
```python
7+
import torch
8+
from torch import nn
9+
10+
def corr2d(X,K): #X为输入,K为核矩阵
11+
h,w=K.shape #h得到K的行数,w得到K的列数
12+
Y=torch.zeros((X.shape[0]-h+1,X.shape[1]-w+1)) #用0初始化输出矩阵Y
13+
for i in range(Y.shape[0]): #卷积运算
14+
for j in range(Y.shape[1]):
15+
Y[i,j]=(X[i:i+h,j:j+w]*K).sum()
16+
return Y
17+
```
18+
19+
20+
```python
21+
#样例点测试
22+
X=torch.tensor([[0,1,2],[3,4,5],[6,7,8]])
23+
K=torch.tensor([[0,1],[2,3]])
24+
corr2d(X,K)
25+
```
26+
27+
28+
29+
30+
tensor([[19., 25.],
31+
[37., 43.]])
32+
33+
34+
35+
36+
```python
37+
#实现二维卷积层
38+
class Conv2d(nn.Module):
39+
def _init_(self,kernel_size):
40+
super()._init_()
41+
self.weight=nn.Parameter(torch.rand(kerner_size))
42+
self.bias=nn.Parameter(torch.zeros(1))
43+
def forward(self,x):
44+
return corr2d(x,self.weight)+self.bias
45+
```
46+
47+
48+
```python
49+
X=torch.ones((6,8))
50+
X[:,2:6]=0
51+
X
52+
```
53+
54+
55+
56+
57+
tensor([[1., 1., 0., 0., 0., 0., 1., 1.],
58+
[1., 1., 0., 0., 0., 0., 1., 1.],
59+
[1., 1., 0., 0., 0., 0., 1., 1.],
60+
[1., 1., 0., 0., 0., 0., 1., 1.],
61+
[1., 1., 0., 0., 0., 0., 1., 1.],
62+
[1., 1., 0., 0., 0., 0., 1., 1.]])
63+
64+
65+
66+
67+
```python
68+
K=torch.tensor([[-1,1]]) #这个K只能检测垂直边缘
69+
Y=corr2d(X,K)
70+
Y
71+
```
72+
73+
74+
75+
76+
tensor([[ 0., -1., 0., 0., 0., 1., 0.],
77+
[ 0., -1., 0., 0., 0., 1., 0.],
78+
[ 0., -1., 0., 0., 0., 1., 0.],
79+
[ 0., -1., 0., 0., 0., 1., 0.],
80+
[ 0., -1., 0., 0., 0., 1., 0.],
81+
[ 0., -1., 0., 0., 0., 1., 0.]])
82+
83+
84+
85+
86+
```python
87+
corr2d(X.t(),K)
88+
```
89+
90+
91+
92+
93+
tensor([[0., 0., 0., 0., 0.],
94+
[0., 0., 0., 0., 0.],
95+
[0., 0., 0., 0., 0.],
96+
[0., 0., 0., 0., 0.],
97+
[0., 0., 0., 0., 0.],
98+
[0., 0., 0., 0., 0.],
99+
[0., 0., 0., 0., 0.],
100+
[0., 0., 0., 0., 0.]])
101+
102+
103+
104+
105+
```python
106+
conv2d = nn.Conv2d(1, 1, kernel_size=(1, 2), bias=False)
107+
108+
X = X.reshape((1, 1, 6, 8))
109+
Y = Y.reshape((1, 1, 6, 7))
110+
111+
for i in range(10):
112+
Y_hat = conv2d(X)
113+
l = (Y_hat - Y)**2
114+
conv2d.zero_grad()
115+
l.sum().backward()
116+
conv2d.weight.data[:] -= 3e-2 * conv2d.weight.grad
117+
if (i + 1) % 2 == 0:
118+
print(f'batch {i+1}, loss {l.sum():.3f}')
119+
```
120+
121+
batch 2, loss 3.852
122+
batch 4, loss 1.126
123+
batch 6, loss 0.386
124+
batch 8, loss 0.145
125+
batch 10, loss 0.057
126+
127+
128+
129+
```python
130+
conv2d.weight.data.reshape((1, 2))
131+
```
132+
133+
134+
135+
136+
tensor([[-1.0173, 0.9685]])
137+
138+
139+
140+
141+
```python
142+
143+
```

0 commit comments

Comments
 (0)