convolutional neural networks
SAME padding
stride in CNNs
deep learning
machine learning

what is the behavior of SAME padding when stride is greater than 1?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

SAME padding is often misinterpreted as “output size stays the same,” but that is only true in limited cases such as stride one with compatible kernels. When stride is greater than one, output still shrinks due to downsampling. SAME mainly controls how padding is chosen so coverage remains centered and edge loss is controlled.

Core Output Rule

For one spatial dimension in TensorFlow-style semantics:

text
output = ceil(input / stride)

So with input size 10 and stride 2, output is ceil(10/2)=5. That is smaller than input, even though padding mode is SAME.

This is the first key point: SAME does not cancel downsampling introduced by stride.

How Padding Is Computed

Padding is computed to ensure convolution windows can cover input consistently for the target output size.

Conceptual formula:

text
pad_total = max((output - 1) * stride + kernel - input, 0)

That padding is split across both sides. If pad_total is odd, one side gets one extra pad element.

This asymmetry is normal and can affect alignment when merging branches.

Numeric Example

Suppose input length 10, kernel 3, stride 2:

  1. output = ceil(10/2) = 5
  2. pad_total = (5 - 1) * 2 + 3 - 10 = 1

So one pad element is needed in total. Framework places it around the boundaries according to implementation rules.

This allows all five output positions while keeping effective sampling centered.

TensorFlow Verification

python
1import tensorflow as tf
2
3x = tf.random.normal([1, 10, 10, 1])
4conv_same = tf.keras.layers.Conv2D(4, kernel_size=3, strides=2, padding="same")
5conv_valid = tf.keras.layers.Conv2D(4, kernel_size=3, strides=2, padding="valid")
6
7y_same = conv_same(x)
8y_valid = conv_valid(x)
9
10print("input:", x.shape)
11print("same :", y_same.shape)
12print("valid:", y_valid.shape)

SAME usually gives 5x5 spatial output in this case, while VALID often gives smaller dimensions because it adds no padding.

Why This Matters in Model Design

Misunderstanding SAME can cause shape mismatches in:

  • Skip connections.
  • U-Net style concatenation paths.
  • Residual projections.
  • Multi-branch feature fusion.

Always compute expected tensor sizes explicitly when mixing stride and different padding modes.

Odd Dimensions and Alignment Issues

When input dimensions are odd and stride exceeds one, asymmetric padding becomes more common. This can create off-by-one differences between branches that otherwise seem equivalent.

Practical handling:

  • Log intermediate shapes.
  • Use explicit crop or pad alignment layers when needed.
  • Keep architecture diagrams synchronized with actual shape math.

These steps save significant debugging time.

Cross-Framework Caution

Different frameworks may use similar names but differ in boundary details. When converting models between ecosystems, validate layer outputs numerically and dimensionally rather than assuming label equivalence.

For conversion workflows:

  1. Compare each layer output shape.
  2. Compare a few deterministic sample outputs.
  3. Adjust padding or crop operations explicitly if needed.

Practical Rule of Thumb

Remember this compact rule:

  • Stride controls downsampling rate.
  • SAME controls padding policy, not output-preservation guarantee for stride above one.

If you need same-size output with stride above one, you likely need additional upsampling or architectural changes.

Keep this rule visible in model design docs to avoid repeated shape-debug cycles during architecture reviews and framework migrations.

Common Pitfalls

  • Assuming SAME means identity-sized output for every stride value.
  • Ignoring asymmetric padding when combining multi-branch tensors.
  • Mixing SAME and VALID in residual paths without shape checks.
  • Porting models across frameworks without validating boundary behavior.
  • Debugging training metrics before confirming tensor shape expectations.

Summary

  • With stride above one, SAME still reduces spatial dimensions.
  • Output follows a ceil-based stride rule.
  • Padding is computed to preserve coverage, not to prevent downsampling.
  • Asymmetric edge padding can appear, especially with odd dimensions.
  • Explicit shape verification is essential in complex CNN architectures.

Course illustration
Course illustration

All Rights Reserved.