``````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 : 步幅

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
# (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:
self.W = W
self.b = b
self.stride = stride
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_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)
``````

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)

1个回答

FN,C,FH,FW这里有4个变量，但是self.W.shape的长度只有2。

jianjiu17 谢谢，看半天没看到，多谢大哥
12 个月之前 回复