# What is the difference between ‘SAME’ and ‘VALID’ padding in tf.nn.max_pool of tensorflow ?

What is the difference between ‘SAME’ and ‘VALID’ padding in tf.nn.max_pool of tensorflow ?

Here is an simple example to make it clearly:

• x: input image of shape [2, 3], 1 channel
• valid_pad: max pool with 2×2 kernel, stride 2 and VALID padding.
• same_pad: max pool with 2×2 kernel, stride 2 and SAME padding.

• valid_pad: there, no padding so the output shape is [1, 1]
• same_pad: there, we pad the image to the shape [2, 4] (with -inf then apply max pool), so the output shape is [1, 2]
```x = tf.constant([[1., 2., 3.],
[4., 5., 6.]])

x = tf.reshape(x, [1, 2, 3, 1])  # give a shape accepted by tf.nn.max_pool

valid_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
same_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

```

If the stride is 1, you had to think of the bellow distinction:

• SAME‘: The output size is the same as input size. Which needs the filter window to slip outside input map, so need to pad.
• VALID‘: Here, Filter window stays at valid position inside input map, Hence output size shrinks by filter_size – 1. No need to pad.

The difference between ‘SAME‘ and ‘VALID‘ are,

In SAME padding, output of height and width are determine as:

• out_height = ceil(float(in_height) / float(strides))
• out_width = ceil(float(in_width) / float(strides))

Then, In VALID padding, output of height and width are determine as:

• out_height = ceil(float(in_height – filter_height + 1) / float(strides))
• out_width = ceil(float(in_width – filter_width + 1) / float(strides))