# Pooling Layer

## Introduction

![](https://2109831662-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvMRntv-nKvtl7WOpCz%2F-LvMRp9FltcwEeVxPYFs%2F-LvMRrqp4R7Uh6vYzF5d%2FMaxPoolingLayer.png?generation=1575572710612029\&alt=media)

The pooling layer, is used to reduce the spatial dimensions, but not depth, on a convolution neural network, model, basically this is what you gain:

1. By having less spatial information you gain computation performance
2. Less spatial information also means less parameters, so less chance to over-fit
3. You get some translation invariance

Some projects don't use pooling, specially when they want to "learn" some object specific position. Learn how to play atari games.

On the diagram bellow we show the most common type of pooling the max-pooling layer, which slides a window, like a normal convolution, and get the biggest value on the window as the output.

![](https://2109831662-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvMRntv-nKvtl7WOpCz%2F-LvMRp9FltcwEeVxPYFs%2F-LvMRrqrlhiBpH5LD-_p%2FPooling_Simple_max.png?generation=1575572710691611\&alt=media)

The most important parameters to play:

* Input: H1 x W1 x Depth\_In x N
* Stride: Scalar that control the amount of pixels that the window slide.
* K: Kernel size

Regarding it's Output H2 x W2 x Depth\_Out x N:

$$W_2 = (W_1 - K)/S + 1\\ H_2 = (H_1 - K)/S + 1 \\ Depth_{out} = Depth_{In}$$

It's also valid to point out that there is no learnable parameters on the pooling layer. So it's backpropagation is simpler.

## Forward Propagation

The window movement mechanism on pooling layers is the same as convolution layer, the only change is that we will select the biggest value on the window.

### Python Forward propagation

![](https://2109831662-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvMRntv-nKvtl7WOpCz%2F-LvMRp9FltcwEeVxPYFs%2F-LvMRrqtqnkKTcnM_t-R%2FPython_maxpool_forward.png?generation=1575572710743461\&alt=media)

### Matlab Forward propagation

## Backward Propagation

From the [backpropagation chapter](../supervised_learning/backpropagation) we learn that the max node simply act as a router, giving the input gradient "dout" to the input that has value bigger than zero.

![](https://2109831662-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvMRntv-nKvtl7WOpCz%2F-LvMRp9FltcwEeVxPYFs%2F-LvMRqq-8yeIbRSmKJ-E%2FMaxGate.png?generation=1575572707296576\&alt=media) You can consider that the max pooling use a series of max nodes, on it's computation graph. So consider the backward propagation of the max pooling layer as a product between a mask containing all elements that were selected during the forward propagation and dout.

![](https://2109831662-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvMRntv-nKvtl7WOpCz%2F-LvMRp9FltcwEeVxPYFs%2F-LvMRrqxU_wXXzCY06YX%2FBackPropagation_MaxPool.png?generation=1575572710672313\&alt=media)

In other words the gradient with respect to the input of the max pooling layer will be a tensor make of zeros except on the places that was selected during the forward propagation.

### Python Backward propagation

![](https://2109831662-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvMRntv-nKvtl7WOpCz%2F-LvMRp9FltcwEeVxPYFs%2F-LvMRrqzjOM_Hkgg3z0o%2FPython_maxpool_backward.png?generation=1575572711238039\&alt=media)

## Improving performance

On future chapter we will learn a technique that improves the convolution performance, until them we will stick with the naive implementation.

## Next Chapter

Next chapter we will learn about Batch Norm layer
