Keras
zero padding
convolutional layers
deep learning
neural networks

How to do zero padding in keras conv layer?

Master System Design with Codemia

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

Introduction

In Keras, zero padding for convolution layers is usually done in one of two ways: set padding="same" on the convolution layer, or add an explicit ZeroPadding2D layer before the convolution. The first option is the common default when you want shape preservation. The second is useful when you need precise control over how much padding goes on each side.

Using padding="same" in Conv2D

The simplest way to request zero padding is to set the convolution layer's padding argument to "same".

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(28, 28, 1)),
5    tf.keras.layers.Conv2D(
6        filters=16,
7        kernel_size=3,
8        padding="same",
9        activation="relu"
10    )
11])
12
13model.summary()

With a 3 x 3 kernel and stride 1, "same" padding adds zeros around the border so the output height and width stay the same as the input.

If you switch to padding="valid", no zeros are added and the spatial dimensions shrink.

Comparing same and valid

Here is a quick shape comparison:

python
1import tensorflow as tf
2
3x = tf.random.normal((1, 28, 28, 1))
4
5same_layer = tf.keras.layers.Conv2D(8, 3, padding="same")
6valid_layer = tf.keras.layers.Conv2D(8, 3, padding="valid")
7
8print(same_layer(x).shape)
9print(valid_layer(x).shape)

Typical output:

text
(1, 28, 28, 8)
(1, 26, 26, 8)

This is why "same" is so common in CNN models where preserving feature-map size is desirable.

Using ZeroPadding2D Explicitly

If you need exact padding sizes, use ZeroPadding2D. This is more flexible than relying on the convolution layer's automatic "same" behavior.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(28, 28, 1)),
5    tf.keras.layers.ZeroPadding2D(padding=((1, 2), (3, 4))),
6    tf.keras.layers.Conv2D(8, 3, padding="valid", activation="relu")
7])
8
9model.summary()

In this example:

  • Height gets 1 row on top and 2 rows on bottom
  • Width gets 3 columns on the left and 4 columns on the right

That level of control is useful when porting architectures from papers or matching another framework's exact shape behavior.

When same Does Not Mean "Exactly the Same"

There is one subtle point that often surprises people. With stride 1, padding="same" preserves spatial dimensions. With larger strides, the output is not necessarily identical in size to the input. Instead, Keras pads in a way that follows the framework's "same" convolution rule, which often produces a size based on the stride.

python
1import tensorflow as tf
2
3x = tf.random.normal((1, 28, 28, 1))
4layer = tf.keras.layers.Conv2D(8, 3, strides=2, padding="same")
5print(layer(x).shape)

So if exact output size matters, always inspect the resulting shape rather than assuming "same" literally means unchanged dimensions under every configuration.

Choosing Between the Two Approaches

Use padding="same" when:

  • You want the typical zero-padding behavior
  • The convolution configuration is standard
  • You care more about readability than manual shape control

Use ZeroPadding2D when:

  • You need asymmetric padding
  • You want padding separated from convolution in the model graph
  • You are reproducing an architecture with specific border rules

Both approaches use zeros. The difference is how much control you need.

Common Pitfalls

One common mistake is assuming padding="same" means the output always has the exact same width and height. That is true for stride 1, but not for every other setting.

Another issue is stacking ZeroPadding2D and padding="same" without realizing that both add padding. That can silently change the receptive field and output shape.

Developers also sometimes forget that padding interacts with data format and input shape. In Keras, the default is usually channels_last, so image input shapes are typically written as (height, width, channels).

Finally, if you are debugging shape mismatches, print actual tensor shapes during model construction instead of relying on mental arithmetic alone. Convolution output dimensions are easy to miscalculate in deep networks.

Summary

  • Use padding="same" for the most common zero-padding behavior in Keras convolutions.
  • Use ZeroPadding2D when you need explicit or asymmetric padding.
  • 'padding="valid" means no zero padding at all.'
  • With stride 1, "same" usually preserves spatial dimensions.
  • Check actual output shapes when stride or kernel configuration becomes more complex.

Course illustration
Course illustration

All Rights Reserved.