46

I recently came across a method in Pytorch when I try to implement AlexNet. I don't understand how it works. Please explain the idea behind it with some examples. And how it is different from Maxpooling or Average poling in terms of Neural Network functionality

nn.AdaptiveAvgPool2d((6, 6))

harsath
  • 699
  • 1
  • 7
  • 8

1 Answers1

77

In average-pooling or max-pooling, you essentially set the stride and kernel-size by your own, setting them as hyper-parameters. You will have to re-configure them if you happen to change your input size.

In Adaptive Pooling on the other hand, we specify the output size instead. And the stride and kernel-size are automatically selected to adapt to the needs. The following equations are used to calculate the value in the source code.

Stride = (input_size//output_size)  
Kernel size = input_size - (output_size-1)*stride  
Padding = 0
Anant Mittal
  • 1,923
  • 9
  • 15
  • How come `Stride = (input_size//output_size)`? – Eric Wiener Oct 11 '20 at 00:03
  • 7
    Found another [answer](https://stackoverflow.com/a/63603993/6942666) that explains this formula is only the case when the input size is an integer multiple of the second dimension. Otherwise, the flooring that occurs when calculating the output dimensions will cause this formula to be invalid. – Eric Wiener Oct 11 '20 at 00:31
  • Thank you very much. Where did you get these formulas from please? – Avv Feb 24 '23 at 22:56
  • I'm not familiar with how Adaptive Pooling is implemented in specific libraries, but wanted to note that in the case of `input_size` being a multiple of `output_size`, if `input_size//output_size` is substituted for `stride` in the expression for `Kernel size` above, then we obtain `Kernel size = stride`. Actually, it seems to me that the formula for `Kernel size` above is equivalent to `Kernel size = stride + (input_size % output_size)`. Maybe this helps in understanding the intent for `Kernel size`'s expression in the answer? – Ruben Ramirez Padron Aug 26 '23 at 16:27