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 ?

Asked on November 17, 2018 in Tensorflow.
Add Comment


  • 3 Answer(s)

    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.

    Output shapes for valid_pad and same_pad are

    • 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')
     
    valid_pad.get_shape() == [1, 1, 1, 1]   # valid_pad is [5.]
    same_pad.get_shape() == [1, 1, 2, 1]   # same_pad is [5., 6.]
    
    Answered on November 17, 2018.
    Add Comment

    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.
    Answered on November 17, 2018.
    Add Comment

    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[1]))
    • out_width = ceil(float(in_width) / float(strides[2]))

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

    • out_height = ceil(float(in_height – filter_height + 1) / float(strides[1]))
    • out_width = ceil(float(in_width – filter_width + 1) / float(strides[2]))
    Answered on November 17, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.