如何对 maxpool1d 使用 'same' 填充

How to use 'same' padding for maxpool1d

Tensorflow tf.keras.layers.MaxPool1D has the option to set padding='same' to make the input shape the same as the output shape. Is there something equivalent for torch.nn.MaxPool1d ? I see that torch.nn.Conv1d 有设置 padding='same' 的选项,但是 maxpool 好像没有这个选项。当前的解决方法是什么?

请注意,keras 版本的输出仅在您将步幅和扩张设置为 1 时使用时与输入的形状真正相同,因此我将在此答案中假设相同的参数。

对于任何不均匀的内核大小,通过将填充设置为 (kernel_size - 1)/2,这在 PyTorch 中很容易实现。

对于均匀的内核大小,输入的两边都需要填充不同的量,这在当前的 MaxPool1d 实现中似乎是不可能的。