Bird
Raised Fist0
TensorFlowml~20 mins

Padding and stride in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Padding and Stride Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output shape with SAME padding and stride 2
Consider a 2D convolution layer in TensorFlow with input shape (1, 28, 28, 3), kernel size 3x3, stride 2, and padding='SAME'. What is the output shape after applying this convolution?
TensorFlow
import tensorflow as tf
input_tensor = tf.random.uniform((1, 28, 28, 3))
conv_layer = tf.keras.layers.Conv2D(filters=5, kernel_size=3, strides=2, padding='SAME')
output = conv_layer(input_tensor)
print(output.shape)
A(1, 14, 14, 5)
B(1, 12, 12, 5)
C(1, 15, 15, 5)
D(1, 13, 13, 5)
Attempts:
2 left
💡 Hint
Padding='SAME' keeps output size roughly input size divided by stride.
🧠 Conceptual
intermediate
1:30remaining
Effect of stride on output size
In a convolutional neural network, if you increase the stride from 1 to 3 while keeping padding='VALID' and kernel size fixed, what happens to the output feature map size?
AOutput size stays the same because kernel size is fixed.
BOutput size increases because stride adds padding.
COutput size becomes zero because stride is too large.
DOutput size decreases because stride skips more input positions.
Attempts:
2 left
💡 Hint
Think about how stride controls the step size when sliding the kernel.
❓ Metrics
advanced
2:00remaining
Calculate output size with padding='VALID' and stride 2
Given an input image of size 32x32, a convolutional layer with kernel size 5x5, stride 2, and padding='VALID', what is the output width and height?
A16 x 16
B15 x 15
C14 x 14
D13 x 13
Attempts:
2 left
💡 Hint
Use formula: output = floor((input - kernel + 1) / stride)
🔧 Debug
advanced
2:00remaining
Identify the error in convolution output shape calculation
A user writes this code to compute output shape after a convolution: input_shape = (1, 64, 64, 3) kernel_size = 7 stride = 2 padding = 'VALID' output_height = (input_shape[1] - kernel_size) // stride output_width = (input_shape[2] - kernel_size) // stride print(f'Output shape: (1, {output_height}, {output_width}, 10)') What is wrong with this calculation?
APadding='VALID' requires adding padding size to input
BThe kernel size should be subtracted with +1: (input - kernel + 1)
COutput channels should be input channels minus kernel size
DThe stride should be added, not divided
Attempts:
2 left
💡 Hint
Recall the formula for output size with VALID padding.
❓ Model Choice
expert
2:30remaining
Choosing padding and stride for preserving spatial dimensions
You want to design a convolutional layer that keeps the input image size exactly the same after convolution. Which combination of padding and stride should you choose?
Apadding='SAME' and stride=1
Bpadding='VALID' and stride=1
Cpadding='SAME' and stride=2
Dpadding='VALID' and stride=2
Attempts:
2 left
💡 Hint
Think about how padding and stride affect output size.

Practice

(1/5)
1. What does padding do in a convolutional layer in TensorFlow?
easy
A. Changes the filter size
B. Adjusts the learning rate
C. Controls the number of filters
D. Adds extra space around the input edges

Solution

  1. Step 1: Understand padding role

    Padding adds pixels around the input image edges to control output size and preserve edge information.
  2. Step 2: Compare options

    Only Adds extra space around the input edges correctly describes padding as adding space around input edges.
  3. Final Answer:

    Adds extra space around the input edges -> Option D
  4. Quick Check:

    Padding = Adds space around input edges [OK]
Hint: Padding adds border space around input images [OK]
Common Mistakes:
  • Confusing padding with stride
  • Thinking padding changes filter size
  • Assuming padding controls learning rate
2. Which of the following is the correct way to specify stride of 2 in TensorFlow's Conv2D layer?
easy
A. strides=(2)
B. stride=(2)
C. strides=(2, 2)
D. stride=2

Solution

  1. Step 1: Recall Conv2D stride syntax

    TensorFlow Conv2D expects strides as a tuple for height and width, e.g., (2, 2).
  2. Step 2: Check options

    Only strides=(2, 2) uses the correct parameter name and tuple format.
  3. Final Answer:

    strides=(2, 2) -> Option C
  4. Quick Check:

    Stride syntax = strides=(2, 2) [OK]
Hint: Use tuple for strides in Conv2D: strides=(height, width) [OK]
Common Mistakes:
  • Using 'stride' instead of 'strides'
  • Passing a single integer instead of a tuple
  • Omitting parentheses around stride values
3. Given an input of size 28x28, a filter size of 3x3, stride of 2, and 'valid' padding, what is the output size after Conv2D?
medium
A. 13x13
B. 14x14
C. 15x15
D. 12x12

Solution

  1. 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.
  2. Step 2: Confirm output dimensions

    Output height and width are both 13, so output size is 13x13.
  3. Final Answer:

    13x13 -> Option A
  4. Quick Check:

    Output size = 13x13 with valid padding [OK]
Hint: Use formula: floor((input-filter)/stride)+1 for 'valid' padding [OK]
Common Mistakes:
  • Using 'same' padding formula instead
  • Forgetting to floor the division
  • Adding padding when 'valid' means no padding
4. You wrote this code but get an error:
tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=2, padding='samee')

What is the error?
medium
A. Strides must be a tuple, not integer
B. Invalid padding value 'samee'
C. Kernel size must be a tuple
D. Filters must be a list

Solution

  1. Step 1: Check padding parameter

    Padding accepts only 'valid' or 'same'. 'samee' is a typo and invalid.
  2. 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.
  3. Final Answer:

    Invalid padding value 'samee' -> Option B
  4. Quick Check:

    Padding typo causes error [OK]
Hint: Check spelling of padding values: 'same' or 'valid' only [OK]
Common Mistakes:
  • Misspelling padding values
  • Assuming strides must be tuple always
  • Confusing kernel_size type requirements
5. You want to keep the output size same as input (28x28) after Conv2D with filter size 5x5. Which padding and stride should you use?
hard
A. padding='same', strides=(1, 1)
B. padding='valid', strides=(1, 1)
C. padding='same', strides=(2, 2)
D. padding='valid', strides=(2, 2)

Solution

  1. 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.
  2. Step 2: Check options

    Only padding='same', strides=(1, 1) uses 'same' padding and stride (1,1), which preserves size.
  3. Final Answer:

    padding='same', strides=(1, 1) -> Option A
  4. Quick Check:

    Same padding + stride 1 keeps size [OK]
Hint: Use 'same' padding and stride 1 to keep output size [OK]
Common Mistakes:
  • Using 'valid' padding which shrinks output
  • Using stride > 1 which reduces output size
  • Confusing filter size effect on output
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard