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

Implement Global Average Pooling

Neural Networks
Medium

Implement global average pooling for channel-first feature maps by averaging every spatial location independently for each channel:

GAP(x)c=1HW∑h=1H∑w=1Wxc,h,w\mathrm{GAP}(x)_c = \frac{1}{HW} \sum_{h=1}^{H} \sum_{w=1}^{W} x_{c,h,w}GAP(x)c​=HW1​h=1∑H​w=1∑W​xc,h,w​

For an input with shape (C,H,W)(C,H,W)(C,H,W), return shape (C,)(C,)(C,). For a batched input with shape (N,C,H,W)(N,C,H,W)(N,C,H,W), return shape (N,C)(N,C)(N,C). The output must be a floating-point NumPy array.

Loading visualization...

Examples

Input: x = [[[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]]]

Output: [1.0, 1.0, 1.0]

Explanation: Each channel contains four ones, so every channel mean is 1.

Input: x = [[[[1, 2], [3, 4]]]]

Output: [[2.5]]

Hint 1

The spatial dimensions are always the final two axes.

Hint 2

np.mean(x, axis=(-2, -1)) preserves batch and channel axes.

Requirements

  • Accept an unbatched or batched channel-first input
  • Average only the final two spatial dimensions
  • Return a floating-point NumPy array
  • Do not modify the input

Constraints

  • Spatial height and width are at least 1
  • Use NumPy only
Try Similar Problems
Max Pooling 2dAverage Pooling 2dSimple Cnn LayerMaxpool ForwardConv2d Image Filtering

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Implement Global Average Pooling

Neural Networks
Medium

Implement global average pooling for channel-first feature maps by averaging every spatial location independently for each channel:

GAP(x)c=1HW∑h=1H∑w=1Wxc,h,w\mathrm{GAP}(x)_c = \frac{1}{HW} \sum_{h=1}^{H} \sum_{w=1}^{W} x_{c,h,w}GAP(x)c​=HW1​h=1∑H​w=1∑W​xc,h,w​

For an input with shape (C,H,W)(C,H,W)(C,H,W), return shape (C,)(C,)(C,). For a batched input with shape (N,C,H,W)(N,C,H,W)(N,C,H,W), return shape (N,C)(N,C)(N,C). The output must be a floating-point NumPy array.

Loading visualization...

Examples

Input: x = [[[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]]]

Output: [1.0, 1.0, 1.0]

Explanation: Each channel contains four ones, so every channel mean is 1.

Input: x = [[[[1, 2], [3, 4]]]]

Output: [[2.5]]

Hint 1

The spatial dimensions are always the final two axes.

Hint 2

np.mean(x, axis=(-2, -1)) preserves batch and channel axes.

Requirements

  • Accept an unbatched or batched channel-first input
  • Average only the final two spatial dimensions
  • Return a floating-point NumPy array
  • Do not modify the input

Constraints

  • Spatial height and width are at least 1
  • Use NumPy only
Try Similar Problems
Max Pooling 2dAverage Pooling 2dSimple Cnn LayerMaxpool ForwardConv2d Image Filtering

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.