I am trying to implement a very simple pooling function. The input is a 3x4x4 matrix (3 dimensions, 4 rows, 4 columns) and I want my output to be a 3x2x2 matrix
def pooling_layers(image):
pooling_layer = np.zeros((3, 2, 2))
for i in range(3):
a = image[i][:][:]
result = skimage.measure.block_reduce(a, (2, 2), np.mean)
# now I have my result, I want to add it to the 2x2 block of `pooling_layer`
pooling_layer = pooling_layers[i][:][:] + result
print(pooling_layer)
return pooling_layer
Above I manage to get the mean 2D array but I want to add it to the correct dimension of my pooling_layers
matrix, how do I do this?
Ex. I have input matrix C
array([[[ 37, 41, 46, 50],
[ 64, 68, 73, 78],
[ 91, 96, 100, 105],
[118, 123, 127, 132]],
[[ 26, 30, 35, 39],
[ 52, 56, 61, 65],
[ 78, 83, 87, 91],
[104, 109, 113, 117]],
[[ 28, 31, 35, 38],
[ 47, 50, 54, 57],
[ 66, 70, 73, 76],
[ 85, 89, 92, 95]]])
And my output, pooling_layer
would be:
array([[[ 52.5, 61.75],
[ 107., 116. ]],
[[ 41., 50. ],
[ 93.5, 102.]],
[[ 39. , 46. ],
[ 77.5, 84. ]]])
Instead of using a for loop, you can directly use the following line of code to get the result.
skimage.measure.block_reduce(image, (1, 2, 2), np.mean)
On the other hand, if you want to use the for loop approach, you can assign the value directly instead of addition.
def pooling_layers(image):
pooling_layer = np.zeros((3, 2, 2))
for i in range(3):
a = image[i][:][:]
result = skimage.measure.block_reduce(a, (2, 2), np.mean)
pooling_layer[i] = result
return pooling_layer