PIXELBANKv9.1.0
Menu

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:
  • The 4Γ—4 input is split into non-overlapping 2Γ—2 blocks with stride 2:

    • 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}
  • For each 2Γ—2 block, take the maximum value:

    • 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
  • Arrange these maxima in the same spatial layout to form the output: [[6,8],[14,16]][[6, 8], [14, 16]].

Constraints:

  • Input dimensions are even
  • Return pooled feature map
πŸ”’

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

0/0
Run code to see test results.
Max Pooling - Easy | PixelBank