初始化:
将滤波器(权重、卷积核)、偏置、步幅、填充作为参数接收。
滤波器是(FN, C, FH, FW)的四维形状。
- Filter Number
- Chanel
- Filter Height
- Filter Width
def __int__(self, W, b, stride=1, pad=0): self.W = W # 权重 self.b = b # 偏置 self.stride = stride # 步幅 self.pad = pad # 填充
卷积:
im2col函数展开输入数据,并用reshape将滤波器展开为2维数组,计算展开后的矩阵的乘积。
col = im2col(x, FH, FW, self.stride, self.pad)
col_W = self.W.reshape(FN, -1).T
out = np.dot(col. col_W) + self.b
卷积层的实现:
from common.util import im2col
import numpy as np
class Convolution:
def __int__(self, W, b, stride=1, pad=0):
self.W = W
self.b = b
self.stride = stride
self.pad = pad
def forward(self, x):
FN, C, FH, FW = self.W.shape
N, C, H, W = x.shape
out_h = int(1 + (H + 2*self.pad - FH) / self.stride)
out_w = int(1 + (W + 2*self.pad - FW) / self.stride)
col = im2col(x, FH, FW, self.stride, self.pad)
col_W = self.W.reshape(FN, -1).T
out = np.dot(col. col_W) + self.b
out = out.reshape(N, out_h, out_w, -1).transpose(0, 3, 1, 2)
return out
Q1: x.shape的意义和作用
Q2: transpose的作用对象
斋藤康毅.深度学习原理入门:基于Python的理论与实现[M].北京:人民邮电出版社,2020.