我正在尝试为 Conv Networks 中的 MaxPooling 层实现 fprop,没有重叠和池化区域 2x2。为此,我需要将输入矩阵拆分为 2x2 大小的矩阵,以便提取最大值。然后我创建了一个掩码,稍后我可以在 bprop 中使用它。为了执行拆分,我首先垂直拆分我的输入矩阵,然后水平拆分,然后分别使用 vsplit、hsplit 和 amax 找到最大值。但是,由于索引越界异常,这一直在崩溃,我不确定错误在哪里。有没有更简单的方法将 24 x 24 输入矩阵拆分为 144 个 2x2 矩阵,以便我可以获得最大值。
我正在执行以下操作:
for i in range(inputs.shape[0]):
for j in range(inputs.shape[1]):
for k in range(inputs.shape[2] // 2):
for h in range(inputs.shape[3] // 2):
outputs[i,j,k,h] = np.amax(np.hsplit(np.vsplit(inputs[i,j], inputs.shape[2] // 2)[k], inputs.shape[1] // 2)[h])
max_ind = np.argmax(np.hsplit(np.vsplit(inputs[i,j], inputs.shape[2] // 2)[k], inputs.shape[1] // 2)[h])
max_ind_y = max_ind // inputs.shape[2]
if (max_ind_y == 0):
max_ind_x = max_ind
else:
max_ind_x = max_ind % inputs.shape[3]
self.mask[i,j,max_ind_y + 2 * k, max_ind_x + 2 * h] = outputs[i,j,k,h]
编辑:
这是 reshape 产生的输出:
我想要的是
[0 1
4 5]
[2 3
6 7]
等等……
请您参考如下方法:
这实现为 view_as_blocks在 skimage.util 中:
blocks = skimage.util.view_as_blocks(a,(2,2))
maxs = blocks.max((2,3))
