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.
Divide the feature map into 2×2 pooling windows.
Compute the maximum value in each window.
The key equation for max pooling can be represented as:
Each output cell (i,j) is the largest of the four inputs at rows 2i,2i+1 and columns 2j,2j+1: max(1,2,5,6)=6, max(3,4,7,8)=8, max(9,10,13,14)=14, max(11,12,15,16)=16.
Output:[[6, 8], [14, 16]], a 2×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.
Max Pooling in CNNs: Background Knowledge & Implementation Guide
Background Knowledge
What is Max Pooling?
Max pooling is a downsampling operation used in convolutional neural networks to reduce the spatial dimensions of feature maps while retaining the most important information. In a 2×2 max pooling operation with stride 2, you divide the input feature map into non-overlapping 2×2 windows and extract the maximum value from each window. This serves two critical purposes: it reduces computational complexity for subsequent layers and helps the network learn invariant features that are robust to small spatial translations.
Why Use Max Pooling?
Max pooling helps CNNs learn local features effectively by preserving the strongest activations in each region. The operation is particularly useful because it captures the most discriminative information—the maximum activation typically represents the presence of important features detected by filters in that region. By reducing spatial dimensions, max pooling also decreases memory requirements and computational cost, making deep networks more practical to train.
Key Concept: Stride and Window Size
The stride parameter determines how far the pooling window moves after each operation. With a 2×2 window and stride 2, the window moves 2 pixels horizontally and vertically, creating non-overlapping regions. This means a 4×4 feature map becomes 2×2 after max pooling, and an 8×8 map becomes 4×4. Understanding the relationship between input size, window size, and stride is essential for calculating output dimensions correctly.
Algorithm/Approach
The general approach to implementing 2×2 max pooling with stride 2 follows this pattern:
Iterate through the feature map using a sliding window that moves in steps of 2 (the stride)
Extract each 2×2 window from the current position
Find the maximum value within that window
Place the maximum in the corresponding position of the output feature map
Continue until the entire feature map is processed
The key insight is that with stride equal to window size, windows don't overlap, making the computation straightforward and efficient.
Step-by-Step Strategy
Step 1: Calculate Output Dimensions
Before processing, determine the output size using the formula:
output_size=strideinput_size−window_size+1
PRO
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.