TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

51. PyTorch: MaxPool2D Forward

Medium

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

For each pooling window of size pool_size × pool_size, output the maximum value across all spatial positions (per channel).

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 of shape (H_out, W_out, C) where H_out = (H - pool_size) // stride + 1.

The test harness checks the pooled output values.

Math

out[i,j,c]=0≤δh​,δw​<Pmax​x[i⋅s+δh​,j⋅s+δw​,c]

Asked at

NumPy

import numpy as np

 

def maxpool2d(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?