**What is Zero Padding? and what issues does zero-padding combat against.**

Have you heard about Zero padding while working with the convolutional neural networks? And what issue it can resolve, if not, this article is for you. we are going to go through a detailed overview of zero padding

When a filter convolves a given input it then gives us an output this output is a matrix of pixels with the values that were computed during the convolutions that occurred on our image. When this happens the dimensions of our image are reduced

On the left we have a matrix of pixel values from an image of a 7 from the MNIST dataset this image is of size 28 by 28. Now on the right, we have our output from the filter convolving our input. If we look closely, we can see that the output is not the same size as the original input .so our original input was 28 by 28 and now we have an output that has shrunk in size to 26 after convolving the image. Why is that? With our 28 by 28 image our 3 by 3 filter can only fit into 26 by 26 possible positions not all 28 by 28 of them given this we get the resulting 26 by 26 output this is due to what happens when we convolve the edges of our image

For ease of visualizing this let’s look at smaller scale example

Here we have the input of size 4 by 4 and a filter of size 3 by 3. After applying the filter to our input image, we see that the resulting output is two by two while our input was originally 4 by 4. In general, if our image is of size n x n and we convolve it with f x f filter then the size of the resulting output is this:

**Output size = (n — f + 1) x (n — f + 1)**

This is the issue, we are losing valuable data by throwing away pieces of information around the edges of the input because the filter is not convolving those edges nearly as much as its convolving the inner pieces of the input, so what can we do here?

Well, this is where **Zero padding** comes into play. zero padding is a technique that allows us to preserve the original input size this is something we can specify on a per convolutional layer basis so with each convolutional layer just as we’ve defined how many filters we want to have and the size of filters we can also specify whether to use padding, so now we know what issues zero-padding combats against but what it is? Zero padding occurs when we add a border of pixels all with value zero around the edges of our input this add kind of padding of zeros around the image hence the name zero padding

There are two categories of padding:

- Valid (no padding)
- Same (padding to make output size same as input size)

We can see the comparison of both categories in the code.

We can see in the above picture; the output size decreases as we apply convolutions.

In this case, as padding is the same, our output size is the same as our input size.

Written by:Mohsin Mahmood