TorchedUp
ProblemsPremium
TorchedUp
MaxPool2D Forward + BackwardMedium
ProblemsPremium

MaxPool2D Forward + Backward

Implement max pooling — the standard downsampling operation in CNNs. A sliding window takes the maximum value in each non-overlapping region, reducing spatial dimensions while retaining the strongest activations.

Forward pass: For each pooling window of size pool_size × pool_size, output the maximum value across all spatial positions (per channel). Also track which position held the max (the mask) — needed for the backward pass.

Backward pass: Gradients flow only to the position that was the max in the forward pass; all other positions get 0. The mask encodes this routing.

Signature: def maxpool2d(x, pool_size=2, stride=2)

  • x: (H, W, C) — input feature map, channels-last
  • pool_size: int — pooling window size (default 2)
  • stride: int — step between windows (default 2)
  • Returns: (output, mask) where:
    • output: (H_out, W_out, C) — max values, where H_out = (H - pool_size) // stride + 1
    • mask: (H, W, C) boolean — True at the position that was max in each window

The test harness checks the pooled output values. Your mask must correctly mark exactly one True per window per channel.

Math

Asked at

Python (numpy)0/3 runs today

Test Results

○4x4x1 arange, pool=2 stride=2: picks max of each 2x2 window
○pool=1 stride=1 is identity
○6x6x2 random (seed=1), pool=2 stride=2
○known values: max is always bottom-right of each 2x2 window🔒 Premium
Advertisement