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

Average Pooling 2D

Computer Vision
Medium

Average pooling is a downsampling operation that reduces the spatial dimensions of a feature map by computing the mean value within non-overlapping rectangular regions. Unlike max pooling which selects the strongest activation, average pooling captures the overall presence of features in each region.

Given a 2D matrix and a pool size, apply average pooling with non-overlapping windows (stride equal to pool size).

Algorithm

  1. Compute the output dimensions by dividing the input dimensions by the pool size (integer division):
Hout=⌊Hp⌋H_{out} = \left\lfloor \frac{H}{p} \right\rfloorHout​=⌊pH​⌋ Wout=⌊Wp⌋W_{out} = \left\lfloor \frac{W}{p} \right\rfloorWout​=⌊pW​⌋
  1. For each output position (i, j), compute the mean of all values in the corresponding p × p window:
out[i][j]=1p2∑a=0p−1∑b=0p−1X[i⋅p+a][j⋅p+b]\text{out}[i][j] = \frac{1}{p^2} \sum_{a=0}^{p-1} \sum_{b=0}^{p-1} X[i \cdot p + a][j \cdot p + b]out[i][j]=p21​a=0∑p−1​b=0∑p−1​X[i⋅p+a][j⋅p+b]

Return the pooled two-dimensional list of floats.

Loading visualization...

Examples

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

Output: [[3.5, 5.5], [11.5, 13.5]]

Explanation: Each non-overlapping 2 by 2 block contributes its arithmetic mean.

Input: X = [[10, 20], [30, 40]], pool_size = 2

Output: [[25.0]]

Hint 1

Use integer division to count complete pooling windows along each dimension.

Hint 2

Sum one window at a time and divide by pool_size squared.

Requirements

  • Apply non-overlapping average pooling with stride equal to pool_size
  • Compute the arithmetic mean of all values in each pooling window
  • Handle rectangular inputs where dimensions may not be square
  • Discard any remaining rows or columns that don't form a complete pool
  • Return the pooled 2D matrix as a list of lists of floats

Constraints

  • X is a non-empty 2D matrix of numbers
  • pool_size >= 1
  • Input dimensions are at least pool_size in both directions
  • Return a 2D list of floats
  • Time limit: 300 ms
Try Similar Problems
Max Pooling 2dGlobal Avg PoolingConv2d Image FilteringRoi PoolingSimple Cnn Layer

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Average Pooling 2D

Computer Vision
Medium

Average pooling is a downsampling operation that reduces the spatial dimensions of a feature map by computing the mean value within non-overlapping rectangular regions. Unlike max pooling which selects the strongest activation, average pooling captures the overall presence of features in each region.

Given a 2D matrix and a pool size, apply average pooling with non-overlapping windows (stride equal to pool size).

Algorithm

  1. Compute the output dimensions by dividing the input dimensions by the pool size (integer division):
Hout=⌊Hp⌋H_{out} = \left\lfloor \frac{H}{p} \right\rfloorHout​=⌊pH​⌋ Wout=⌊Wp⌋W_{out} = \left\lfloor \frac{W}{p} \right\rfloorWout​=⌊pW​⌋
  1. For each output position (i, j), compute the mean of all values in the corresponding p × p window:
out[i][j]=1p2∑a=0p−1∑b=0p−1X[i⋅p+a][j⋅p+b]\text{out}[i][j] = \frac{1}{p^2} \sum_{a=0}^{p-1} \sum_{b=0}^{p-1} X[i \cdot p + a][j \cdot p + b]out[i][j]=p21​a=0∑p−1​b=0∑p−1​X[i⋅p+a][j⋅p+b]

Return the pooled two-dimensional list of floats.

Loading visualization...

Examples

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

Output: [[3.5, 5.5], [11.5, 13.5]]

Explanation: Each non-overlapping 2 by 2 block contributes its arithmetic mean.

Input: X = [[10, 20], [30, 40]], pool_size = 2

Output: [[25.0]]

Hint 1

Use integer division to count complete pooling windows along each dimension.

Hint 2

Sum one window at a time and divide by pool_size squared.

Requirements

  • Apply non-overlapping average pooling with stride equal to pool_size
  • Compute the arithmetic mean of all values in each pooling window
  • Handle rectangular inputs where dimensions may not be square
  • Discard any remaining rows or columns that don't form a complete pool
  • Return the pooled 2D matrix as a list of lists of floats

Constraints

  • X is a non-empty 2D matrix of numbers
  • pool_size >= 1
  • Input dimensions are at least pool_size in both directions
  • Return a 2D list of floats
  • Time limit: 300 ms
Try Similar Problems
Max Pooling 2dGlobal Avg PoolingConv2d Image FilteringRoi PoolingSimple Cnn Layer

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.