We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← CNNs from Scratch step 3 of 15
Implement Max Pooling 1D
Max pooling summarizes each window by its largest value. The result is a value, not the index of that value. For a window containing only negative values, choose the largest negative value; do not replace it with zero.
Worked example: for x = [1, 3, 2, 5, 4], kernel_size = 2
and stride = 2:
| Start | Window | Maximum |
|---|---|---|
| 0 | [1, 3] |
3 |
| 2 | [2, 5] |
5 |
Return [3, 5]. The next start index would be 4, but only one value
remains. This exercise uses no padding and includes only complete windows;
do not add a partial-window result. Stride is the distance between starts,
so smaller strides can create overlapping windows.
Your task
Implement max_pool1d(x, kernel_size, stride) for a nonempty 1D
floating-point tensor x of shape (L,). Both integer parameters are
positive, and kernel_size <= L.
For output index i, take x[i * stride : i * stride + kernel_size]
and return its maximum. The output is a 1D tensor with length
(L - kernel_size) // stride + 1. There is no padding or dilation.
Use basic tensor slicing and reductions rather than a built-in pooling
operation.
When stride is larger than kernel_size, some input values are skipped between windows. Always move the window start by stride and discard any incomplete final window.
Explore the positions: the shared window diagram below shows indices, not data values or the reduction result. Keep padding at 0 and dilation at 1 to match this exercise; use Next position to inspect each complete window.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Apply the same filter at many locations
A convolutional layer slides a learned kernel over its input. At each location, it multiplies the covered values by the kernel weights and sums them. Deep-learning libraries usually implement cross-correlation under the name convolution: the kernel is not reversed.
For input [1, 2, 3] and kernel [1, -1], with no padding and stride one,
the outputs are 1*1 + 2*(-1) = -1 and 2*1 + 3*(-1) = -1.
Work out the output shape
For one spatial axis, the output length is
$$\left\lfloor\frac{L+2P-D(K-1)-1}{S}\right\rfloor+1.$$
L is the input length, K the kernel size, P the padding on each end,
S the stride between windows, and D the spacing between kernel elements
(dilation). For a 2D input, apply the formula separately to height and width.
Check the channel layout too: an ordinary convolution sums over both spatial
positions in the window and input channels.
A depthwise convolution filters each input channel separately. A following pointwise convolution, using a 1-by-1 spatial kernel, mixes channels. This uses a different set of parameters from a full spatial convolution; the two constructions are not generally interchangeable.
Pool nearby features
Max pooling keeps the largest value in each window. Average pooling takes the window's mean. During backpropagation, max pooling sends the gradient to the selected position, while average pooling distributes it across the values that contributed to the mean. Overlapping windows can contribute more than once to the same input position, so add those gradients.
Follow the exercise's rules for tied maxima and whether padding counts in an average. For a small CNN, also save the forward-pass values needed for activation and pooling derivatives. Add each shared kernel's gradient contributions across all locations, then update weights after computing all gradients.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.