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

TensorFlow · Function

tf.expand_dims

Inserts a length-1 axis into a tensor at a chosen position. It uses zero-based Python indexing, with negative axes counted from the end.

How it works

tf.expand_dims creates a new axis of length 1 at the requested position while preserving the input values. The position uses zero-based Python indexing, and negative positions count backward from the end.

Syntax

tf.expand_dims(input, axis, name=None)

Parameters

NameTypeDescription
inputTensorA Tensor.
axisintInteger specifying the dimension index at which to expand the shape of input. For an input of D dimensions, it must be in [-(D+1), D], inclusive.
namestringOptional string naming the output Tensor.

Returns

A tensor with the same data as input, with an additional dimension inserted at the index specified by axis.

Key points

  • The result gains exactly one new axis, and that axis has length 1.
  • The new singleton dimension is inserted into the input shape at the chosen position.
  • Axis numbering starts at zero; negative axes select positions relative to the end.
  • Typical applications include adding a batch axis, preparing shapes for broadcasting, and adding an inner vector-length axis.
  • Passing zero places the new singleton dimension before all existing dimensions.
  • Using a negative axis places the singleton dimension relative to the tensor's trailing dimensions.

Common mistakes

  • axis must be within the input-rank-dependent valid range; an out-of-range value raises an error.
  • For a rank-D input, valid axes run from -(D+1) through D, inclusive.

Example: Add a batch axis

import tensorflow as tf

image = tf.zeros([10, 10, 3])
batched = tf.expand_dims(image, axis=0)
print(batched.shape)  # (1, 10, 10, 3)

Adds a leading batch dimension to a single image tensor.

When to use it

Add a batch axis

Prepare a single example for a model that expects a batch dimension.

Align broadcasting axes

Make tensor ranks or axis positions compatible for elementwise broadcasting.

Add an inner singleton axis

Represent scalar values with an explicit final vector-length dimension.

Interview questions

QWhat does tf.expand_dims do?

It inserts a dimension whose length is 1 at the specified axis and returns a tensor with the same data as the input. The axis follows zero-based Python indexing, including negative indices.

Follow-up: How would you add a batch dimension to an image shaped [10, 10, 3]? Use tf.expand_dims(image, axis=0), producing shape [1, 10, 10, 3].

QWhich axis values are valid?

For an input with rank D, axis may be any integer from -(D+1) to D, inclusive. Negative axes count backward from the end, so axis=-1 inserts the innermost dimension.

Follow-up: What shape results from expanding a [10, 10, 3] tensor with axis=-1? The result is [10, 10, 3, 1].

QWhat are common uses for this function?

The operation is useful for adding an outer batch dimension, aligning axes for broadcasting, or adding an inner vector-length axis to scalar tensors. It changes shape but does not alter the tensor's data.

Follow-up: When would an inner axis be useful? It can make tensor shapes compatible with an operation that expects a final singleton feature or vector-length dimension.

  • tf.squeeze
  • tf.reshape
  • tf.sparse.expand_dims

Source: tf.expand_dims | TensorFlow v2.16.1. Checked 2026-10-15.