QBoard » Artificial Intelligence & ML » AI and ML - Tensorflow » 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?

  • What is the difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow?
    In my opinion, 'VALID' means there will be no zero padding outside the edges when we do max pool.
    According to A guide to convolution arithmetic for deep learning, it says that there will be no padding in pool operator, i.e. just use 'VALID' of tensorflow. But what is 'SAME' padding of max pool in tensorflow?
      September 3, 2021 5:30 PM IST
    0
  • When stride is 1 (more typical with convolution than pooling), we can think of the following distinction:

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

    "VALID": Filter window stays at valid position inside input map, so output size shrinks by filter_size - 1. No padding occurs.
      September 7, 2021 1:28 PM IST
    0
  • Padding is an operation to increase the size of the input data. In case of 1-dimensional data you just append/prepend the array with a constant, in 2-dim you surround matrix with these constants. In n-dim you surround your n-dim hypercube with the constant. In most of the cases this constant is zero and it is called zero-padding.

    Here is an example of zero-padding with p=1 applied to 2-d  tensor: 

    enter image description here


    You can use arbitrary padding for your kernel but some of the padding values are used more frequently than others they are:

    • VALID padding. The easiest case, means no padding at all. Just leave your data the same it was.
    • SAME padding sometimes called HALF padding. It is called SAME because for a convolution with a stride=1, (or for pooling) it should produce output of the same size as the input. It is called HALF because for a kernel of size of k enter image description here
    • FULL padding is the maximum padding which does not result in a convolution over just padded elements. For a kernel of size of k, this padding is equal to k-1.

    To use arbitrary padding in TF, you can use tf.pad()

    This post was edited by Vaibhav Mali at September 9, 2021 12:56 PM IST
      September 9, 2021 12:55 PM IST
    0
  • Complementing YvesgereY's great answer, I found this visualization extremely helpful:

    Padding visualization

    Padding 'valid' is the first figure. The filter window stays inside the image.

    Padding 'same' is the third figure. The output is the same size.


    Found it on this article

    Visualization credits: vdumoulin@GitHub

      September 15, 2021 12:45 PM IST
    0