这是实现代码,是深度学习入门书上的代码
import numpy as np
def im2col(input_data, filter_h, filter_w, stride=1, pad=0):
"""
Parameters
----------
input_data : 由(数据量, 通道, 高, 长)的4维数组构成的输入数据
filter_h : 卷积核的高
filter_w : 卷积核的长
stride : 步幅
pad : 填充
Returns
-------
col : 2维数组
"""
# 输入数据的形状
# N:批数目,C:通道数,H:输入数据高,W:输入数据长
N, C, H, W = input_data.shape
out_h = (H + 2 * pad - filter_h) // stride + 1 # 输出数据的高
out_w = (W + 2 * pad - filter_w) // stride + 1 # 输出数据的长
# 填充 H,W
img = np.pad(input_data, [(0, 0), (0, 0), (pad, pad), (pad, pad)], 'constant')
# (N, C, filter_h, filter_w, out_h, out_w)的0矩阵
col = np.zeros((N, C, filter_h, filter_w, out_h, out_w))
for y in range(filter_h):
y_max = y + stride * out_h
for x in range(filter_w):
x_max = x + stride * out_w
col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]
# 按(0, 4, 5, 1, 2, 3)顺序,交换col的列,然后改变形状
col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N * out_h * out_w, -1)
return col
class Convolution:
def __init__(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
print(self.W)
out = out.reshape(N,out_h,out_w,-1).transpose(0,3,1,2)
return out
e = np.array([[2,0,1],[0,1,2],[1,0,2]])
x = np.array([[1,2,3,0],[0,1,2,3],[3,0,1,2],[2,3,0,1]])
gdd = Convolution(e,3,1,0)
gdd.forward(x)
在gdd.forward(x)出现错误:
Traceback (most recent call last):
File "E:/PycharmProjects/untitled2/kk.py", line 61, in
gdd.forward(x)
File "E:/PycharmProjects/untitled2/kk.py", line 46, in forward
FN,C,FH,FW=self.W.shape
ValueError: not enough values to unpack (expected 4, got 2)