Padding and stride help control how a convolutional layer processes images. Padding adds space around the image edges, and stride controls how the filter moves over the image.
Padding and stride in TensorFlow
tf.keras.layers.Conv2D(filters, kernel_size, strides=(stride_height, stride_width), padding='valid' or 'same')
padding='valid' means no padding; the filter only moves inside the image.
padding='same' means padding is added to keep output size same as input size.
Conv2D(32, (3, 3), strides=(1, 1), padding='valid')
Conv2D(32, (3, 3), strides=(2, 2), padding='same')
Conv2D(64, (5, 5), strides=(1, 1), padding='same')
This code shows how padding changes the output size and values. The kernel weights are all ones to make it easy to see the sum of values under the filter.
import tensorflow as tf import numpy as np # Create a simple 5x5 grayscale image with one channel input_image = np.array([[[[1], [2], [3], [4], [5]], [[6], [7], [8], [9], [10]], [[11], [12], [13], [14], [15]], [[16], [17], [18], [19], [20]], [[21], [22], [23], [24], [25]]]], dtype=np.float32) # Define a Conv2D layer with kernel 3x3, stride 1, padding 'valid' conv_valid = tf.keras.layers.Conv2D(filters=1, kernel_size=(3,3), strides=(1,1), padding='valid', use_bias=False, kernel_initializer=tf.constant_initializer(1.0)) # Define a Conv2D layer with kernel 3x3, stride 1, padding 'same' conv_same = tf.keras.layers.Conv2D(filters=1, kernel_size=(3,3), strides=(1,1), padding='same', use_bias=False, kernel_initializer=tf.constant_initializer(1.0)) # Apply convolution with 'valid' padding output_valid = conv_valid(input_image) # Apply convolution with 'same' padding output_same = conv_same(input_image) print('Output shape with valid padding:', output_valid.shape) print('Output values with valid padding:\n', output_valid.numpy().squeeze()) print('Output shape with same padding:', output_same.shape) print('Output values with same padding:\n', output_same.numpy().squeeze())
Stride larger than 1 reduces output size by skipping positions.
Padding='same' adds zeros around the input to keep output size equal to input size.
Using all ones kernel helps understand how convolution sums input values under the filter.
Padding controls if and how much space is added around input edges.
Stride controls how far the filter moves each step over the input.
Together, they affect output size and what features the model can learn.
Practice
Solution
Step 1: Understand padding role
Padding adds pixels around the input image edges to control output size and preserve edge information.Step 2: Compare options
Only Adds extra space around the input edges correctly describes padding as adding space around input edges.Final Answer:
Adds extra space around the input edges -> Option DQuick Check:
Padding = Adds space around input edges [OK]
- Confusing padding with stride
- Thinking padding changes filter size
- Assuming padding controls learning rate
Solution
Step 1: Recall Conv2D stride syntax
TensorFlow Conv2D expects strides as a tuple for height and width, e.g., (2, 2).Step 2: Check options
Only strides=(2, 2) uses the correct parameter name and tuple format.Final Answer:
strides=(2, 2) -> Option CQuick Check:
Stride syntax = strides=(2, 2) [OK]
- Using 'stride' instead of 'strides'
- Passing a single integer instead of a tuple
- Omitting parentheses around stride values
Solution
Step 1: Calculate output size formula for 'valid' padding
Output size = floor((Input - Filter) / Stride) + 1 = floor((28 - 3) / 2) + 1 = floor(25 / 2) + 1 = 12 + 1 = 13.Step 2: Confirm output dimensions
Output height and width are both 13, so output size is 13x13.Final Answer:
13x13 -> Option AQuick Check:
Output size = 13x13 with valid padding [OK]
- Using 'same' padding formula instead
- Forgetting to floor the division
- Adding padding when 'valid' means no padding
tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=2, padding='samee')
What is the error?
Solution
Step 1: Check padding parameter
Padding accepts only 'valid' or 'same'. 'samee' is a typo and invalid.Step 2: Verify other parameters
Strides can be integer or tuple, kernel_size can be int or tuple, filters is integer. So no error there.Final Answer:
Invalid padding value 'samee' -> Option BQuick Check:
Padding typo causes error [OK]
- Misspelling padding values
- Assuming strides must be tuple always
- Confusing kernel_size type requirements
Solution
Step 1: Understand output size preservation
To keep output size same as input, use 'same' padding and stride 1 so filter slides over every pixel with padding added.Step 2: Check options
Only padding='same', strides=(1, 1) uses 'same' padding and stride (1,1), which preserves size.Final Answer:
padding='same', strides=(1, 1) -> Option AQuick Check:
Same padding + stride 1 keeps size [OK]
- Using 'valid' padding which shrinks output
- Using stride > 1 which reduces output size
- Confusing filter size effect on output
Ready to prove it?
Pass the test and this pattern moves unknown
