Max pooling is an operation that reduces the dimensionality of an input. The output is computed by taking the maximum input values from intersecting input patches and a sliding filter window. At each step, the position of the filter window is updated according to the strides argument.
Why padding is needed
When the filter is applied to the border pixels, some of the filter elements may not overlap any input elements. To compute the values of those border regions, the input can be extended by padding it with zero values. In some cases we may instead want to discard the border regions, so no padding is required.
tf.nn.max_pool supports two types of padding, "VALID" and "SAME". With "VALID" padding, tf.nn.max_pool returns output whose value can be computed without using any padding, and the option may discard the border elements of the input. With "SAME" padding, tf.nn.max_pool returns output whose value can be computed by applying the filter to all input elements, with border elements computed using zero padding. The output may be the same size or smaller than the input, depending on the stride option.
VALID padding: output dimensions
With the "VALID" option there is no zero padding. The output dimensions are computed as:
out_height = ceil((in_height - filter_height + 1) / strides[1])
out_width = ceil((in_width - filter_width + 1) / strides[2])
SAME padding: output and padding dimensions
With the "SAME" option the output dimensions and padding pixels are computed as:
out_height = ceil(in_height / strides[1])
out_width = ceil(in_width / strides[2])
pad_along_height = max((out_height - 1) * strides[1] + filter_height - in_height, 0)
pad_along_width = max((out_width - 1) * strides[2] + filter_width - in_width, 0)
pad_top = pad_along_height // 2; pad_bottom = pad_along_height - pad_top; pad_left = pad_along_width // 2; pad_right = pad_along_width - pad_left
Padding is achieved by adding additional rows and columns at the top, bottom, left and right of the input matrix, according to the formulas above. Padding values are always zero. The formulas for computing output size and padding pixels for the "VALID" and "SAME" options are given on the tensorflow website.
Worked example: VALID padding
For a 2D input of size 4x3 with a 2D filter of size 2x2, strides [2, 2] and "VALID" pooling, tf.nn.max_pool returns an output of size 2x1. There is no padding with the VALID option. Max pooling starts by placing the 2x2 filter over the input at (0,0) and selecting the maximum input value from the overlapping region.
For the next step, moving the filter in the X direction by 2 (the stride in the X dimension) is not possible, because the last column of the filter would fall outside the image. Max pooling therefore continues by moving the filter in the Y direction by 2. From this new position, no further move in either the X or Y direction is possible, so the operation finishes with an output of size 2x1.
![Figure 1: max pooling with the VALID option over a 4x3 input using a 2x2 filter and strides [2, 2], producing a 2x1 output with no padding. Input and output matrices are shown on the left and right, with each pooling step highlighted in yellow.](/assets/kb/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-tensorflow/Figure1.gif)
Worked example: SAME padding
For the same input, filter and strides but with the "SAME" pooling option, tf.nn.max_pool returns an output of size 2x2. The output and padding dimensions are computed using the given formulas. The value of pad_right is 1, so a column is added on the right with zero padding values. The max pooling operation then proceeds as described above, producing an output of size 2x2.
![Figure 2: max pooling with the SAME option over a 4x3 input using a 2x2 filter and strides [2, 2], producing a 2x2 output after a zero-padded column is added on the right. Input and output matrices are shown on the left and right, with each pooling step highlighted in yellow.](/assets/kb/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-tensorflow/Figure2.gif)
Max pooling and padding are building blocks of the convolutional neural networks used in modern deep learning. For another neural-network fundamental, see the role of bias in neural networks.