machine learning
tensorflow
padding techniques
periodic padding
deep learning

tensorflow periodic padding

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Periodic padding, also called circular or wrap-around padding, extends a tensor by reusing values from the opposite edge. It is useful when your data represents something naturally cyclic, such as angles, tiled textures, longitude, or simulations with periodic boundary conditions.

TensorFlow’s built-in tf.pad supports constant, reflect, and symmetric padding, but not periodic padding directly. To get periodic behavior, you usually build the padded tensor by gathering wrapped indices or concatenating slices.

Why Periodic Padding Is Different

With zero padding, new cells are filled with a constant such as 0. With reflect padding, values mirror around the border. Periodic padding does neither. Instead, the left border is filled with values from the right edge, and the right border is filled with values from the left edge.

For a one-dimensional tensor such as [1, 2, 3, 4], periodic padding by one element on each side gives [4, 1, 2, 3, 4, 1].

That wrap-around property is what makes periodic padding appropriate for cyclic domains.

A General 1D Implementation

A robust way to implement periodic padding is to build the padded index range and wrap it with modulo arithmetic.

python
1import tensorflow as tf
2
3
4def periodic_pad_1d(x: tf.Tensor, left: int, right: int) -> tf.Tensor:
5    n = tf.shape(x)[-1]
6    indices = tf.range(-left, n + right)
7    wrapped = tf.math.mod(indices, n)
8    return tf.gather(x, wrapped, axis=-1)
9
10
11x = tf.constant([1, 2, 3, 4])
12y = periodic_pad_1d(x, left=2, right=3)
13print(y.numpy())  # [3 4 1 2 3 4 1 2 3]

This handles any padding width, even when the requested padding is larger than the original tensor length.

Extending the Idea to 2D Tensors

For images or matrices, wrap rows and columns separately. The same index-based idea works well and stays easy to reason about.

python
1import tensorflow as tf
2
3
4def periodic_pad_2d(x: tf.Tensor, top: int, bottom: int, left: int, right: int) -> tf.Tensor:
5    height = tf.shape(x)[1]
6    width = tf.shape(x)[2]
7
8    row_indices = tf.math.mod(tf.range(-top, height + bottom), height)
9    col_indices = tf.math.mod(tf.range(-left, width + right), width)
10
11    x = tf.gather(x, row_indices, axis=1)
12    x = tf.gather(x, col_indices, axis=2)
13    return x
14
15
16image = tf.reshape(tf.range(1, 10), (1, 3, 3, 1))
17padded = periodic_pad_2d(image, top=1, bottom=1, left=1, right=1)
18print(tf.squeeze(padded).numpy())

This example assumes an NHWC layout with batch and channel dimensions present. If your tensor layout differs, change the axes accordingly.

Why Index Gathering Is Better Than Ad Hoc Slicing

For small fixed padding, slice-and-concatenate code can be fine:

python
wrapped = tf.concat([x[:, :, -1:], x, x[:, :, :1]], axis=2)

The problem is that this style becomes awkward once:

  • padding widths vary dynamically
  • padding is larger than the dimension size
  • you need a reusable helper for different ranks

Modulo-based index gathering is more general and usually easier to test.

Periodic Padding in Convolution Workflows

A common use case is periodic padding before convolution. If you are modeling a cyclic domain, zero padding introduces artificial boundaries that can distort the convolution result near the edges.

A typical workflow is:

  1. apply periodic padding manually
  2. run tf.nn.conv2d or a Keras convolution layer with padding="valid"

That way, you control the exact boundary behavior rather than relying on standard zero padding.

Common Pitfalls

One common mistake is assuming tf.pad has a periodic mode. It does not, so using the wrong built-in padding mode changes the numerical meaning of the boundary.

Another issue is forgetting tensor layout. In image code, whether height and width live on axes 1 and 2 or somewhere else depends on your data format.

Padding larger than the input size is another place where manual slice code breaks down. A modulo-based index approach handles that case naturally.

Finally, keep gradients in mind when building custom layers. TensorFlow operations such as tf.gather are differentiable in the usual way for gathered values, so the approach works well in training pipelines, but you should still test shapes and gradients in your actual model.

Summary

  • Periodic padding wraps values from the opposite edge instead of using zeros or reflections.
  • TensorFlow does not provide periodic padding directly through tf.pad.
  • Building wrapped indices with tf.range, modulo arithmetic, and tf.gather is a flexible solution.
  • Index-based padding works for both one-dimensional and multi-dimensional tensors.
  • Periodic padding is especially useful for cyclic data and convolution over periodic domains.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.