TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Max Pooling Forward

Neural Networks
Medium

Max pooling is a downsampling operation commonly used in convolutional neural networks to reduce spatial dimensions while retaining the most prominent features. It slides a window over the input and takes the maximum value in each window position.

Given a 2D input matrix X (height H, width W), a pool size p, and a stride s, compute the max pooling output.

Algorithm

  1. Compute the output dimensions:
Hout=⌊H−ps⌋+1H_{out} = \left\lfloor \frac{H-p}{s} \right\rfloor + 1Hout​=⌊sH−p​⌋+1 Wout=⌊W−ps⌋+1W_{out} = \left\lfloor \frac{W-p}{s} \right\rfloor + 1Wout​=⌊sW−p​⌋+1
  1. For each output position (i, j), extract the p x p window and take the maximum:
out[i][j]=max⁡0≤a<p,  0≤b<pX[i⋅s+a][j⋅s+b]\text{out}[i][j] = \max_{0 \le a < p,\; 0 \le b < p} X[i \cdot s + a][j \cdot s + b]out[i][j]=0≤a<p,0≤b<pmax​X[i⋅s+a][j⋅s+b]

Return the pooled two-dimensional list.

Loading visualization...

Examples

Input: X = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], pool_size = 2, stride = 2

Output: [[6, 8], [14, 16]]

Explanation: Each non-overlapping 2 by 2 window contributes its maximum.

Input: X = [[1, 5, 3], [4, 2, 6], [7, 8, 0]], pool_size = 2, stride = 1

Output: [[5, 6], [8, 8]]

Hint 1

Compute output height and width with integer division before scanning windows.

Hint 2

Initialize a window maximum from its top-left value, then compare the remaining entries.

Requirements

  • Apply 2D max pooling with the given pool size and stride
  • Each output element is the maximum value in its pooling window
  • Handle both overlapping (stride < pool_size) and non-overlapping (stride = pool_size) cases
  • Return the pooled output as a list of lists of floats or ints

Constraints

  • X has at least pool_size rows and pool_size columns
  • pool_size >= 1, stride >= 1
  • Return a list of lists
  • Time limit: 300 ms
Try Similar Problems
Max Pooling 2dAverage Pooling 2dGlobal Avg PoolingSimple Cnn LayerConv2d Image Filtering

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Max Pooling Forward

Neural Networks
Medium

Max pooling is a downsampling operation commonly used in convolutional neural networks to reduce spatial dimensions while retaining the most prominent features. It slides a window over the input and takes the maximum value in each window position.

Given a 2D input matrix X (height H, width W), a pool size p, and a stride s, compute the max pooling output.

Algorithm

  1. Compute the output dimensions:
Hout=⌊H−ps⌋+1H_{out} = \left\lfloor \frac{H-p}{s} \right\rfloor + 1Hout​=⌊sH−p​⌋+1 Wout=⌊W−ps⌋+1W_{out} = \left\lfloor \frac{W-p}{s} \right\rfloor + 1Wout​=⌊sW−p​⌋+1
  1. For each output position (i, j), extract the p x p window and take the maximum:
out[i][j]=max⁡0≤a<p,  0≤b<pX[i⋅s+a][j⋅s+b]\text{out}[i][j] = \max_{0 \le a < p,\; 0 \le b < p} X[i \cdot s + a][j \cdot s + b]out[i][j]=0≤a<p,0≤b<pmax​X[i⋅s+a][j⋅s+b]

Return the pooled two-dimensional list.

Loading visualization...

Examples

Input: X = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], pool_size = 2, stride = 2

Output: [[6, 8], [14, 16]]

Explanation: Each non-overlapping 2 by 2 window contributes its maximum.

Input: X = [[1, 5, 3], [4, 2, 6], [7, 8, 0]], pool_size = 2, stride = 1

Output: [[5, 6], [8, 8]]

Hint 1

Compute output height and width with integer division before scanning windows.

Hint 2

Initialize a window maximum from its top-left value, then compare the remaining entries.

Requirements

  • Apply 2D max pooling with the given pool size and stride
  • Each output element is the maximum value in its pooling window
  • Handle both overlapping (stride < pool_size) and non-overlapping (stride = pool_size) cases
  • Return the pooled output as a list of lists of floats or ints

Constraints

  • X has at least pool_size rows and pool_size columns
  • pool_size >= 1, stride >= 1
  • Return a list of lists
  • Time limit: 300 ms
Try Similar Problems
Max Pooling 2dAverage Pooling 2dGlobal Avg PoolingSimple Cnn LayerConv2d Image Filtering

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

Accepts: number

You must run your code first.