将矩阵划分为2x2个正方形子矩阵maxpooling fprop

2024-06-26 18:00:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在没有重叠和池区2x2的Conv网络中实现fprop的MaxPooling层。为此,我需要将输入矩阵拆分为2x2大小的矩阵,以便提取最大值。然后我正在创建一个掩码,稍后可以在bprop中使用。为了进行拆分,我首先垂直拆分输入矩阵,然后水平拆分,然后分别使用vsplithsplit和{}找到最大值。但是,这会持续崩溃,但会出现索引越界异常,我不确定错误在哪里。有没有更简单的方法把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]

编辑:

这是通过整形产生的输出:

enter image description here

我想要的是

^{pr2}$

等等。。。在


Tags: in网络fornprange矩阵outputsmax
2条回答

步骤1:获取max_ind_xmax_ind_y

我们需要得到每个块的max元素的行、列索引-

m,n = inputs.shape
a = inputs.reshape(m//2,2,n//2,2).swapaxes(1,2)
row, col = np.unravel_index(a.reshape(a.shape[:-2] + (4,)).argmax(-1), (2,2))

步骤2:使用argmax places从输入设置输出数组

然后,看看你的代码,你似乎在试图创建一个输出数组,其中的argmax个位置是用输入数组中的值设置的。因此,我们可以-

^{pr2}$

最后,我们可以得到输出的2D形状,这将是一个很好的验证步骤来验证原始输入inputs-

out2d = out.reshape(a.shape[:2]+(2,2)).swapaxes(1,2).reshape(m,n)

样本输入,输出-

In [291]: np.random.seed(0)
     ...: inputs = np.random.randint(11,99,(6,4))

In [292]: inputs
Out[292]: 
array([[55, 58, 75, 78],
       [78, 20, 94, 32],
       [47, 98, 81, 23],
       [69, 76, 50, 98],
       [57, 92, 48, 36],
       [88, 83, 20, 31]])

In [286]: out2d
Out[286]: 
array([[ 0,  0,  0,  0],
       [78,  0, 94,  0],
       [ 0, 98,  0,  0],
       [ 0,  0,  0, 98],
       [ 0, 92, 48,  0],
       [ 0,  0,  0,  0]])

这在skimage.util中实现为^{}

blocks = skimage.util.view_as_blocks(a,(2,2))
maxs = blocks.max((2,3))

相关问题 更多 >