Is there any difference between AdaptiveAvgPool and mean()?

x = torch.rand(2, 3, 128, 128)
F.adaptive_avg_pool2d(x, output_size=1).flatten(start_dim=1)
x = torch.rand(2, 3, 128, 128)
x.mean(dim=(2, 3))

Is there any difference between two above?

I think both are same and the second one is more simple and understandable.
But I assume there is any reason why most people are using first one.

There is no difference in the posted code snippets and the former one will also use the latter approach internally for an output_size of 1.
The former allows you to use other output sizes and is thus more flexible. Also, you could use it as an nn.Module without creating a custom module for the mean operation.

1 Like