Skip to content

Commit ca63376

Browse files
authored
Merge pull request MLNLP-World#3 from Aleafy/main
update file
2 parents 2aa4bda + 5070c12 commit ca63376

3 files changed

Lines changed: 371 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-卷积层] | [讲义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) | | <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> |

code/19-卷积层.ipynb

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 20,
6+
"id": "a673b451",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import torch\n",
11+
"from torch import nn\n",
12+
"\n",
13+
"def corr2d(X,K): #X为输入,K为核矩阵\n",
14+
" h,w=K.shape #h得到K的行数,w得到K的列数\n",
15+
" Y=torch.zeros((X.shape[0]-h+1,X.shape[1]-w+1)) #用0初始化输出矩阵Y\n",
16+
" for i in range(Y.shape[0]): #卷积运算\n",
17+
" for j in range(Y.shape[1]):\n",
18+
" Y[i,j]=(X[i:i+h,j:j+w]*K).sum()\n",
19+
" return Y"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 21,
25+
"id": "85e0f332",
26+
"metadata": {},
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": [
31+
"tensor([[19., 25.],\n",
32+
" [37., 43.]])"
33+
]
34+
},
35+
"execution_count": 21,
36+
"metadata": {},
37+
"output_type": "execute_result"
38+
}
39+
],
40+
"source": [
41+
"#样例点测试\n",
42+
"X=torch.tensor([[0,1,2],[3,4,5],[6,7,8]])\n",
43+
"K=torch.tensor([[0,1],[2,3]])\n",
44+
"corr2d(X,K)"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 22,
50+
"id": "3b9a8de1",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"#实现二维卷积层\n",
55+
"class Conv2d(nn.Module):\n",
56+
" def _init_(self,kernel_size):\n",
57+
" super()._init_()\n",
58+
" self.weight=nn.Parameter(torch.rand(kerner_size))\n",
59+
" self.bias=nn.Parameter(torch.zeros(1))\n",
60+
" def forward(self,x):\n",
61+
" return corr2d(x,self.weight)+self.bias "
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 23,
67+
"id": "555f2489",
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"data": {
72+
"text/plain": [
73+
"tensor([[1., 1., 0., 0., 0., 0., 1., 1.],\n",
74+
" [1., 1., 0., 0., 0., 0., 1., 1.],\n",
75+
" [1., 1., 0., 0., 0., 0., 1., 1.],\n",
76+
" [1., 1., 0., 0., 0., 0., 1., 1.],\n",
77+
" [1., 1., 0., 0., 0., 0., 1., 1.],\n",
78+
" [1., 1., 0., 0., 0., 0., 1., 1.]])"
79+
]
80+
},
81+
"execution_count": 23,
82+
"metadata": {},
83+
"output_type": "execute_result"
84+
}
85+
],
86+
"source": [
87+
"X=torch.ones((6,8))\n",
88+
"X[:,2:6]=0\n",
89+
"X"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 24,
95+
"id": "859d2935",
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"data": {
100+
"text/plain": [
101+
"tensor([[ 0., -1., 0., 0., 0., 1., 0.],\n",
102+
" [ 0., -1., 0., 0., 0., 1., 0.],\n",
103+
" [ 0., -1., 0., 0., 0., 1., 0.],\n",
104+
" [ 0., -1., 0., 0., 0., 1., 0.],\n",
105+
" [ 0., -1., 0., 0., 0., 1., 0.],\n",
106+
" [ 0., -1., 0., 0., 0., 1., 0.]])"
107+
]
108+
},
109+
"execution_count": 24,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
114+
"source": [
115+
"K=torch.tensor([[-1,1]]) #这个K只能检测垂直边缘\n",
116+
"Y=corr2d(X,K)\n",
117+
"Y"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 25,
123+
"id": "926cb124",
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"data": {
128+
"text/plain": [
129+
"tensor([[0., 0., 0., 0., 0.],\n",
130+
" [0., 0., 0., 0., 0.],\n",
131+
" [0., 0., 0., 0., 0.],\n",
132+
" [0., 0., 0., 0., 0.],\n",
133+
" [0., 0., 0., 0., 0.],\n",
134+
" [0., 0., 0., 0., 0.],\n",
135+
" [0., 0., 0., 0., 0.],\n",
136+
" [0., 0., 0., 0., 0.]])"
137+
]
138+
},
139+
"execution_count": 25,
140+
"metadata": {},
141+
"output_type": "execute_result"
142+
}
143+
],
144+
"source": [
145+
"corr2d(X.t(),K)"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 40,
151+
"id": "2eb05498",
152+
"metadata": {},
153+
"outputs": [
154+
{
155+
"name": "stdout",
156+
"output_type": "stream",
157+
"text": [
158+
"batch 2, loss 3.852\n",
159+
"batch 4, loss 1.126\n",
160+
"batch 6, loss 0.386\n",
161+
"batch 8, loss 0.145\n",
162+
"batch 10, loss 0.057\n"
163+
]
164+
}
165+
],
166+
"source": [
167+
"conv2d = nn.Conv2d(1, 1, kernel_size=(1, 2), bias=False)\n",
168+
"\n",
169+
"X = X.reshape((1, 1, 6, 8))\n",
170+
"Y = Y.reshape((1, 1, 6, 7))\n",
171+
"\n",
172+
"for i in range(10):\n",
173+
" Y_hat = conv2d(X)\n",
174+
" l = (Y_hat - Y)**2\n",
175+
" conv2d.zero_grad()\n",
176+
" l.sum().backward()\n",
177+
" conv2d.weight.data[:] -= 3e-2 * conv2d.weight.grad\n",
178+
" if (i + 1) % 2 == 0:\n",
179+
" print(f'batch {i+1}, loss {l.sum():.3f}')"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 41,
185+
"id": "a626a495",
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"data": {
190+
"text/plain": [
191+
"tensor([[-1.0173, 0.9685]])"
192+
]
193+
},
194+
"execution_count": 41,
195+
"metadata": {},
196+
"output_type": "execute_result"
197+
}
198+
],
199+
"source": [
200+
"conv2d.weight.data.reshape((1, 2))"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"id": "5722b641",
207+
"metadata": {},
208+
"outputs": [],
209+
"source": []
210+
}
211+
],
212+
"metadata": {
213+
"kernelspec": {
214+
"display_name": "Python [conda env:py36torch040]",
215+
"language": "python",
216+
"name": "conda-env-py36torch040-py"
217+
},
218+
"language_info": {
219+
"codemirror_mode": {
220+
"name": "ipython",
221+
"version": 3
222+
},
223+
"file_extension": ".py",
224+
"mimetype": "text/x-python",
225+
"name": "python",
226+
"nbconvert_exporter": "python",
227+
"pygments_lexer": "ipython3",
228+
"version": "3.6.13"
229+
}
230+
},
231+
"nbformat": 4,
232+
"nbformat_minor": 5
233+
}

notes/19-卷积层.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+

0 commit comments

Comments
 (0)