Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.pad

tf.pad adds values before and after each tensor dimension according to [before, after] pairs. It supports CONSTANT, REFLECT, and SYMMETRIC modes and returns a tensor with the input's type.

How it works

tf.pad adds values before and after every tensor dimension according to a two-column padding specification. The selected mode controls whether padding uses a constant, reflected values, or symmetric values; the output keeps the input type while its dimensions grow by the requested amounts.

Syntax

tf.pad(tensor, paddings, mode='CONSTANT', constant_values=0, name=None)

Parameters

NameTypeDescription
tensorTensorInput tensor to pad.
paddingsTensor of int32Integer padding counts with shape [n, 2], where n is the tensor rank.
modestrPadding strategy: CONSTANT, REFLECT, or SYMMETRIC; case-insensitive.
constant_valuesscalarScalar value used for added elements in CONSTANT mode; must match tensor's type.
namestrOptional operation name.

Returns

A Tensor with the same type as tensor.

Key points

  • tf.pad is the TensorFlow operation for enlarging a tensor with padding.
  • Provide one integer [before, after] pair for each tensor dimension.
  • The first value in each pair adds elements before that dimension's existing contents.
  • The second value in each pair adds elements after that dimension's existing contents.
  • Each output dimension equals its original size plus the corresponding before and after padding.
  • The mode selects constant, reflected, or symmetric padding and is not case-sensitive.
  • The returned tensor preserves the input tensor's data type.

Common mistakes

  • constant_values is used only for CONSTANT mode and must have the same type as tensor.
  • An unsupported mode raises ValueError; mode names are case-insensitive.

Example: Pad a matrix

import tensorflow as tf

t = tf.constant([[1, 2, 3], [4, 5, 6]])
padded = tf.pad(t, [[1, 1], [2, 2]], mode="CONSTANT", constant_values=0)
print(padded.numpy())

Adds one row before and after, and two columns before and after, using zero padding.

When to use it

Shape alignment

Prepare tensors with uniform spatial or sequence dimensions before batching or further computation.

Boundary padding

Add a controlled border around arrays, such as zero borders for numerical or image operations.

Reflective boundaries

Extend tensor edges using neighboring values instead of a fixed fill value.

Interview questions

QHow does tf.pad interpret paddings?

tf.pad pads each tensor dimension using a [before, after] pair. For rank n, paddings has shape [n, 2], and the output dimension is the original size plus both requested amounts.

Follow-up: What happens if the padding rank does not match the tensor rank? The padding specification is invalid because one pair is required for every tensor dimension.

QWhat padding modes does tf.pad support?

CONSTANT fills added elements with constant_values, while REFLECT and SYMMETRIC derive padding from tensor values. The mode argument accepts those three names without regard to case.

Follow-up: How would you choose between REFLECT and SYMMETRIC? Compare whether the boundary value itself is repeated when constructing the reflected extension.

QWhat are tf.pad's type and value requirements?

The result is a Tensor with the same type as tensor. In CONSTANT mode, constant_values must also have that type.

Follow-up: What should be checked when padding integer and floating-point tensors? Ensure the supplied constant is representable with, and matches, the tensor's dtype.

  • tf.constant
  • tf.image.resize_with_pad
  • tf.keras.utils.pad_sequences

Source: Pad. Checked 2026-10-15.