• 一、卷积操作
• 二、池化操作
• 三、实战
• 3.1.卷积计算例1
• 3.2.卷积计算例2
• 3.3.池化操作例1

# 一、卷积操作

N = W − F + 2 P S + 1 N=\cfrac {W-F+2P} {S}+1

W = W − F + 2 P S + 1 W=\cfrac {W-F+2P} {S}+1
H = H − F + 2 P S + 1 H=\cfrac {H-F+2P} {S}+1

WxW:
N = W − F S + 1 N=\cfrac {W-F} {S}+1
WxH:
W = W − F S + 1 W=\cfrac {W-F} {S}+1
H = H − F S + 1 H=\cfrac {H-F} {S}+1

# 二、池化操作

WxW:
N = W − F S + 1 N=\cfrac {W-F} {S}+1
WxH:
W = W − F S + 1 W=\cfrac {W-F} {S}+1
H = H − F S + 1 H=\cfrac {H-F} {S}+1

# 三、实战

## 3.1.卷积计算例1

1.计算卷积后输出特征图的大小：

N = W − F + 2 P S + 1 = 3 − 2 + 2 × 0 1 + 1 = 2 N=\cfrac {W-F+2P} {S}+1=\cfrac {3-2+2\times0} {1}+1=2

2.计算矩阵的值。

0 × 0 + 1 × 1 + 3 × 2 + 4 × 3 = 19 1 × 0 + 2 × 1 + 4 × 2 + 5 × 3 = 25 3 × 0 + 4 × 1 + 6 × 2 + 7 × 3 = 37 4 × 0 + 5 × 1 + 7 × 2 + 8 × 3 = 43 0×0+1×1+3×2+4×3=19\\ 1×0+2×1+4×2+5×3=25\\ \\ 3×0+4×1+6×2+7×3=37 \\ 4×0+5×1+7×2+8×3=43

def corr2d(X, K):  #@save
"""计算二维互相关运算。"""
h, w = K.shape

#设置输出矩阵的大小
Y = torch.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))

#卷积核移动求值
for i in range(Y.shape[0]):
for j in range(Y.shape[1]):
Y[i, j] = (X[i:i + h, j:j + w] * K).sum()
return Y


## 3.2.卷积计算例2

1.计算卷积后输出特征图的大小：

W = W − F + 2 P S W + 1 = 3 − 2 + 2 × 1 2 + 1 = 2.5 H = H − F + 2 P S H + 1 = 3 − 2 + 2 × 1 3 + 1 = 2 W=\cfrac {W-F+2P} {S_W}+1=\cfrac {3-2+2\times1} {2}+1=2.5\\ H=\cfrac {H-F+2P} {S_H}+1=\cfrac {3-2+2\times1} {3}+1=2

2.计算矩阵的值。

0 × 0 + 0 × 1 + 0 × 2 + 0 × 3 = 0 0 × 0 + 0 × 1 + 1 × 2 + 2 × 3 = 8 0 × 0 + 6 × 1 + 0 × 2 + 0 × 3 = 6 7 × 0 + 8 × 1 + 0 × 2 + 0 × 3 = 8 0×0+0×1+0×2+0×3=0\\ 0×0+0×1+1×2+2×3=8\\ 0×0+6×1+0×2+0×3=6\\ 7×0+8×1+0×2+0×3=8\\

## 3.3.池化操作例1

1.计算池化后输出特征图的大小：

N = W − F S + 1 = 3 − 2 1 + 1 = 2 N=\cfrac {W-F} {S}+1=\cfrac {3-2} {1}+1=2

2.计算矩阵的值。

m a x ( 0 , 1 , 3 , 4 ) = 4 m a x ( 1 , 2 , 4 , 5 ) = 5 m a x ( 3 , 4 , 6 , 7 ) = 7 m a x ( 4 , 5 , 7 , 8 ) = 8. max(0,1,3,4)=4\\max(1,2,4,5)=5\\max(3,4,6,7)=7\\max(4,5,7,8)=8.