In short: I am looking for a simple numpy
(maybe oneliner) implementation of Maxpool
- maximum on a window on numpy.narray
for all location of the window across dimensions.
In more details: I am implementing a convolutional neural network ("CNN"), one of the typical layers in such a network is MaxPool
layer (look for example here). Writing
y = MaxPool(x, S)
, x
is an input narray
and S
is a parameter, using pseudocode, the output of the MaxPool
is given by:
y[b,h,w,c] = max(x[b, s*h + i, s*w + j, c]) over i = 0,..., S-1; j = 0,...,S-1.
That is, y
is narray
where the value at indexes b,h,w,c
equals the maximum taken over the window of size S x S
along the second and the third dimension of the input x
, the window "corner" is placed at the indexes b,h,w,c
.
Some additional details: The network is implemented using numpy
. CNN has many "layers" where output of one layer is the input to the next layer. The input to a layers are numpy.narray
s called "tensors". In my case tensors are 4-dimensional numpy.narray
's, x
. That is x.shape
is a tuple (B,H,W,C)
. Each size of dimensions changes after the tensor is process by a layer, for example the input to layer i= 4
can have size B = 10, H = 24, W = 24, C = 3
, while the output, aka input to i+1
layer has B = 10, H = 12, W = 12, C = 5
. As indicated in the comments the size after application of MaxPool
is (B, H - S + 1, W - S + 1, C)
.
For a concreteness: if I use
import numpy as np
y = np.amax(x, axis = (1,2))
where x.shape
is say (2,3,3,4)
this will give me what I want but for a degenerate case where the window I am maximizing over is of the size 3 x 3
, the size of the second and third dimension of x
, which is not exactly what I want.