Problems
Loading...
Step 5 of 6
Overlapping Max Pooling
Easy

Implement two-dimensional max pooling for an NHWC tensor. A square window of width KK moves by stride ss, and each output keeps the largest value independently in every batch item and channel.

Yn,i,j,c=max⁡0≤u,v<KXn,is+u,js+v,c.Y_{n,i,j,c}=\max_{0\le u,v<K}X_{n,is+u,js+v,c}.

Here, nn is the batch index, (i,j)(i,j) is the output location, and cc is the channel. AlexNet used windows larger than their stride, so neighboring windows overlap. Return the pooled values as a float64 NumPy array with shape (B,Hout,Wout,C)(B,H_{out},W_{out},C).

Loading visualization...

Examples

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

Output: [[[[11],[12]],[[15],[16]]]]

Explanation: Each three-by-three window contributes its maximum value, and stride one makes adjacent windows overlap.

Input: x = [[[[1,8],[2,7]],[[3,6],[4,5]]]], kernel_size = 2, stride = 1

Output: [[[[4,8]]]]

Input: x = [[[[1],[5],[2],[4]],[[3],[0],[7],[6]],[[8],[2],[9],[1]],[[4],[3],[5],[0]]]], kernel_size = 2, stride = 2

Output: [[[[5],[7]],[[8],[9]]]]

Hint 1

Slice each window with row * stride and column * stride as its top-left corner.

Hint 2

np.max(window, axis=(1, 2)) preserves the batch and channel axes.

Requirements

  • Compute the maximum value in every valid spatial window.
  • Pool each batch item and channel independently.
  • Do not pad the input.
  • Return a new float64 NumPy array.

Constraints

  • x has shape (B,H,W,C)(B,H,W,C).
  • kernel_size and stride are positive integers.
  • kernel_size is no larger than the input height or width.
  • Output sizes are Hout=⌊(H−K)/s⌋+1H_{out}=\lfloor(H-K)/s\rfloor+1 and Wout=⌊(W−K)/s⌋+1W_{out}=\lfloor(W-K)/s\rfloor+1.