Choose a pooling layer

A pooling layer reduces a feature map by replacing every window with one value: its maximum or its mean. It has no weights.

The snippets on this page are compiled and executed in CI from skainet-docs-samples (PoolingSample.kt).

DSL entry Shape Pick it when

maxPool2d(kernelSize, stride, padding)

[N, C, H, W] to [N, C, H', W']

Downsampling between convolution blocks; keeps the strongest response of a window.

avgPool2d(kernelSize, stride, padding, countIncludePad)

[N, C, H, W] to [N, C, H', W']

Downsampling that keeps the average level of a window.

maxPool1d(kernelSize, stride, padding)

[N, C, L] to [N, C, L']

The same for sequences and signals, after conv1d.

avgPool1d(kernelSize, stride, padding, countIncludePad)

[N, C, L] to [N, C, L']

The same for sequences and signals.

globalAvgPool2d()

[N, C, H, W] to [N, C]

Before the classifier of a convolutional network, instead of flatten.

globalMaxPool2d()

[N, C, H, W] to [N, C]

As above, when the presence of a feature anywhere in the image matters more than how much of it there is.

H' = (H + 2 ยท padding โˆ’ kernelSize) / stride + 1, rounded down, and the same for W' and L'. The stride defaults to the kernel size.

Global pooling instead of flatten

flatten before a dense layer gives the classifier one weight per position, so the network is bound to one image size. Global pooling gives one value per channel whatever the size of the map:

        val model = sequential<FP32, Float>(ctx) {
            input(intArrayOf(3, 32, 32))
            conv2d(outChannels = 16, kernelSize = 3 to 3)
            activation { it.relu() }
            maxPool2d(kernelSize = 2 to 2)
            conv2d(outChannels = 32, kernelSize = 3 to 3)
            activation { it.relu() }
            globalAvgPool2d()          // [N, 32, 13, 13] -> [N, 32]
            dense(10)                  // 32 inputs, inferred
        }

globalAvgPool2d(keepDims = true) keeps the result as [N, C, 1, 1], for a 1 x 1 convolution as classifier.

Outside the DSL

Each layer is a module (GlobalAvgPool2d, GlobalMaxPool2d, MaxPool1d, AvgPool1d, MaxPool2d, AvgPool2d in sk.ainet.lang.nn). Global and 1D pooling are also functions on a tensor, in sk.ainet.lang.tensor:

        val perChannel = featureMap.globalAvgPool2d()          // [N, C]
        val halved = signal.maxPool1d(kernelSize = 2)          // [N, C, L / 2]

How they are built and checked

Global pooling is 2D pooling with a window of the whole map, and 1D pooling is 2D pooling on the view [N, C, 1, L]. They need no kernel of their own: every backend, the gradient tape and the graph export handle them as the operations they are made of.

Their values and gradients are held to PyTorch by suite TS-015 of skainet-ground-truth (adaptive_avg_pool2d(x, 1), adaptive_max_pool2d(x, 1), max_pool1d, avg_pool1d).

  • Max pooling never picks a padded position. Average pooling pads with zero, and countIncludePad = false leaves the padding out of the divisor.

  • 3D pooling is not available.