v9.1.1
Menu

Max Pooling

easyPROCNNsDeep LearningCV: Deep Learning

Implement a max pooling operation on a 2D feature map. Apply 2×2 max pooling with stride 2 to reduce spatial dimensions. Max pooling is a downsampling technique used in Convolutional Neural Networks (CNNs) to reduce the number of parameters and computations, while retaining important information.

  1. Divide the feature map into 2×2 pooling windows.
  2. Compute the maximum value in each window. The key equation for max pooling can be represented as:
output(i,j)=max⁡x,y∈windowinput(i⋅stride+x,j⋅stride+y)\text{output}(i, j) = \max_{x, y \in \text{window}} \text{input}(i \cdot \text{stride} + x, j \cdot \text{stride} + y)

This technique is widely used in image classification tasks.

Example

Input
max_pool([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
Output
[[6,8],[14,16]]
Reasoning
  • Input: feature_map is a 4×44 \times 4 grid of activations, row rr holding 4r−3,…,4r4r-3, \dots, 4r, so values grow left to right and top to bottom.
  • With a 2×22 \times 2 window and stride 2, the windows tile the grid without overlap, giving 42×42=2×2\frac{4}{2} \times \frac{4}{2} = 2 \times 2 windows:
    • top-left [1256]\begin{bmatrix}1 & 2 \\ 5 & 6\end{bmatrix}, top-right [3478]\begin{bmatrix}3 & 4 \\ 7 & 8\end{bmatrix}
    • bottom-left [9101314]\begin{bmatrix}9 & 10 \\ 13 & 14\end{bmatrix}, bottom-right [11121516]\begin{bmatrix}11 & 12 \\ 15 & 16\end{bmatrix}
  • Each output cell (i,j)(i, j) is the largest of the four inputs at rows 2i,2i+12i, 2i+1 and columns 2j,2j+12j, 2j+1: max⁡(1,2,5,6)=6\max(1,2,5,6)=6, max⁡(3,4,7,8)=8\max(3,4,7,8)=8, max⁡(9,10,13,14)=14\max(9,10,13,14)=14, max⁡(11,12,15,16)=16\max(11,12,15,16)=16.
  • Output: [[6, 8], [14, 16]], a 2×22 \times 2 grid that keeps each window's position, so height and width are both halved (16 values become 4).
  • Notice every maximum sits in its window's bottom-right corner, only because this input increases along both axes; for other inputs the winner can be any of the four cells.

Constraints

  • Input dimensions are even
  • Return pooled feature map

Learn this in

More from CV: Deep Learning

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.