Padding: Same and Valid Convolution
Understand how padding handles image boundaries, compare same and valid convolution, and calculate output sizes with a clear seven-by-seven example.
Padding: Same and Valid Convolution explained visually
Follow a package-inspection example to see why boundaries matter, how zero padding changes output dimensions, and why same padding preserves size only when stride is one.
What you will learn
Follow filters, pooling, flattening, and classification through complete CNN examples.
Understand what happens at an image boundary
A camera can capture a defect near the edge of a package photograph. Convolution needs a rule for positioning its filter near that boundary. Padding adds a border of values around the input so the filter can extend beyond the original image.
Stride and padding do different jobs: stride controls the distance between filter positions; padding controls the added boundary around the input. Both affect the number of output positions.
Calculate a valid convolution
Valid convolution adds no padding. Every filter position must fit completely inside the input. Edge pixels can still participate, but there are fewer valid positions than with a padded input.
For a 7 × 7 input, a 3 × 3 filter, stride 1, and no dilation, each output dimension is (7 − 3) ÷ 1 + 1 = 5. The resulting feature map is 5 × 5.
Use zero padding to preserve size at stride one
Add one row of zeros above and below the input, and one column on each side. The original 7 × 7 input now has a 9 × 9 padded extent. Its original pixels have not been replaced.
With the same 3 × 3 filter and stride 1, each output dimension becomes (9 − 3) ÷ 1 + 1 = 7. This is same padding at stride 1: the output width and height match the original input.
Recognize what padding can and cannot do
Zero padding supplies artificial border values; it does not reconstruct anything outside the photograph. These zeros can affect responses near the boundary, so padding is a modeling choice.
Same padding does not guarantee unchanged dimensions for every stride. Under the common SAME convention, each output dimension is the input dimension divided by the stride, rounded up. For input size 7 and stride 2, that is 4. The network can therefore reduce spatial size even while using same padding.
Remember the distinction: valid uses no added border; same chooses padding to produce the specified output-size behavior. Filter size, stride, and padding must be considered together.
Quick check: test your understanding
What is the output size for a 7 × 7 input, 3 × 3 filter, stride 1, and valid convolution?
5 × 5. Each dimension is 7 − 3 + 1 = 5.
What changes when one layer of zeros is added on every side?
The padded input is 9 × 9. With the same filter and stride 1, the output is 7 × 7. The original image pixels remain unchanged.
Does same padding always preserve the original width and height?
No. It preserves them at stride 1. With a larger stride, the output can be smaller; for example, input size 7 and stride 2 give output size 4 under the common SAME convention.
Accessible referenceLesson 49 transcript273 words · Matches the published narration and captionsOpen transcript
Imagine a camera inspecting a package on a conveyor belt. A small tear appears near the edge of the photograph. We want the network to examine that boundary, along with the center of the image. In Lesson 48, we learned that stride controls how far a filter moves. Another setting, called padding, controls what happens around the input boundary.
Without padding, the filter must fit completely inside the image. This is commonly called valid convolution. Consider a seven-by-seven input and a three-by-three filter with a stride of one. Seven minus three, plus one, equals five. The output is five by five. Now add one row of zeros above and below the input, and one column on each side. The padded input becomes nine by nine.
Applying the same filter gives nine minus three, plus one, which equals seven. The output is now seven by seven. This is an example of same padding with a stride of one. It preserves the input's width and height. Padding also allows the filter to cover boundary locations while part of its window extends into the added border. The original edge pixels remain available to the calculation.
However, adding zeros does not reveal what was outside the photograph. The border contains artificial values, and those values can influence the response. Also, same padding does not always mean an unchanged output size. With a larger stride, the output can still become smaller. The key idea is that padding controls how convolution handles boundaries and helps determine the output dimensions. In the next lesson, we will examine how filter size changes the amount of visual context a network can process.