Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform sum pooling in PyTorch

How to perform sum pooling in PyTorch. Specifically, if we have input (N, C, W_in, H_in) and want output (N, C, W_out, H_out) using a particular kernel_size and stride just like nn.Maxpool2d ?

like image 769
adeelz92 Avatar asked Sep 01 '25 01:09

adeelz92


2 Answers

You could use torch.nn.AvgPool1d (or torch.nn.AvgPool2d, torch.nn.AvgPool3d) which are performing mean pooling - proportional to sum pooling. If you really want the summed values, you could multiply the averaged output by the pooling surface.

like image 129
benjaminplanche Avatar answered Sep 02 '25 16:09

benjaminplanche


https://pytorch.org/docs/stable/generated/torch.nn.AvgPool2d.html#torch.nn.AvgPool2d find divisor_override.
set divisor_override=1
you'll get a sumpool

import torch
input = torch.tensor([[[1,2,3],[3,2,1],[3,4,5]]])
sumpool = torch.nn.AvgPool2d(2, stride=1, divisor_override=1)
sumpool(input)

you'll get

tensor([[[ 8,  8],
         [12, 12]]])
like image 31
coke Avatar answered Sep 02 '25 18:09

coke