Explore Courses Blog Tutorials Interview Questions
+1 vote
in AI and Deep Learning by (3.9k points)
edited by

In tf.nn.max_pool of tensorflow what is the difference between 'SAME' and 'VALID'?

I read in here that there's no padding in pool operator means we just have to use 'VALID' of tensorflow. Then what does 'SAME' padding of max pool means in tensorflow?

3 Answers

0 votes
by (10.9k points)
edited by

VALID Padding: it means no padding and it assumes that all the dimensions are valid so that the input image gets fully covered by a filter and the stride specified by you.

 SAME Padding: it applies padding to the input image so that the input image gets fully covered by the filter and specified stride.It is called SAME because, for stride 1 , the output will be the same as the input.

 Let’s see an example:


a is the input image of shape [2, 3], 1 channel

validPad refers to max pool having 2x2 kernel, stride=2 and VALID padding.

samePad refers to max pool having 2x2 kernel, stride=2 and SAME padding.

a = tf.constant([[1., 2., 3.],

                         [4., 5., 6.]])

 a = tf.reshape(x, [1, 2, 3, 1])  

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

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

 validPad.get_shape() == [1, 1, 1, 1]  

samePad.get_shape() == [1, 1, 2, 1]   

Output shapes are-

validPad : output shape is [1,1]

samePad: output shape is [1,2]

0 votes
by (108k points)

“Valid”= without padding:


“Same”= with zero-padding:


In the above example we have:

  • Input width = 13

  • Filter width = 6

  • Stride = 5

With the SAME padding, if you use a stride of 1, the layer's outputs will have the same spatial dimensions as its inputs and it tries to pad evenly left and right, but if the amount of columns to be added is odd, it will add an extra column to the right, as is the case in this example (the same logic applies vertically: there may be an extra row of zeros at the bottom).

With VALID padding, if we do not use any stride then there will be no padding inputs. The layer only uses valid input data and only drops the right-most columns (or bottom-most rows).

0 votes
by (106k points)

The difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow is as follows:

  • "SAME": Here the output size is the same as input size. This requires the filter window to slip outside input map, hence the need to pad.

  • "VALID": When you use valid the filter window stays at valid position inside input map, so output size shrinks by filter_size - 1. No padding occurs.

Browse Categories