v9.1.1
Menu

Max Pooling 1D

easyFREECNNs & Sequence ModelsMachine Learning 2

Implement 1D max pooling with configurable kernel size and stride.

Slide a window of size pool_size across the input with step stride, starting at index 0. At each position, output the maximum value in the window. Only windows that fit entirely inside the input are used (no padding, no partial window at the end).

Return the pooled output.

Example

Input
x = [1, 3, 2, 5, 4, 6]
pool_size = 2
stride = 2
Output
[3, 5, 6]
Reasoning
  • x is a sequence of 66 numbers, pool_size = 2 is how many neighbouring values each window covers, and stride = 2 is how far the window moves between outputs.
  • A window may start at position 0,2,40, 2, 4 (counting from 00). Starting at 66 would run past the end, so the number of windows is ⌊(6−2)/2⌋+1=3\lfloor (6 - 2) / 2 \rfloor + 1 = 3.
  • Window at 00 holds 1,31, 3, whose maximum is 33. Window at 22 holds 2,52, 5, maximum 55. Window at 44 holds 4,64, 6, maximum 66.
  • Collecting the maxima in order gives [3, 5, 6]: three values from six inputs, because with stride equal to the window size the windows tile the input without overlap.
  • Notice that if the stride were 11 the windows would overlap, giving ⌊(6−2)/1⌋+1=5\lfloor (6 - 2) / 1 \rfloor + 1 = 5 outputs, and a value like 55 could win two windows in a row.

Constraints

  • x: 1D list of numbers
  • pool_size: window size (integer >= 1)
  • stride: step size (integer >= 1)
  • Return 1D list of max-pooled values

Learn this in

More from Machine Learning 2

solution.py

Test results

Sign in to run your code against the test cases.