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

TensorFlow · Function

tf.squeeze

Removes dimensions of size 1 from a tensor. By default it removes every singleton dimension, or only the selected singleton dimensions when axis is supplied.

How it works

tf.squeeze returns the input data with dimensions whose size is 1 removed. By default it removes every such dimension; supplying axis limits removal to the listed positions, while name optionally labels the operation.

Syntax

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

Parameters

NameTypeDescription
inputTensorA Tensor to squeeze.
axislist of ints, optionalOptional list of integer dimension indices. If provided, only those dimensions are squeezed; it defaults to all singleton dimensions.
namestring, optionalOptional operation name.

Returns

A Tensor with the same type and data as input, with one or more dimensions of size 1 removed.

Key points

  • The operation eliminates singleton dimensions from a tensor's shape.
  • The output retains the input tensor's data type.
  • Use axis to select which singleton dimensions are removed.
  • Dimension positions use zero-based indexing.
  • The optional name argument can label the created operation.

Common mistakes

  • Every dimension named by axis must currently have size 1; otherwise the operation raises an error.
  • Axis indices must fall within the input rank, allowing either nonnegative or valid negative indices.
  • RaggedTensor inputs require an explicit axis instead of relying on the default behavior.

Example: Remove singleton dimensions

import tensorflow as tf

t = tf.ones([1, 2, 1, 3, 1, 1])
print(tf.shape(tf.squeeze(t)).numpy())
print(tf.shape(tf.squeeze(t, axis=[2, 4])).numpy())

Creates a tensor with singleton dimensions, then removes all singleton dimensions and selected singleton dimensions.

When to use it

Normalize tensor rank

Convert a tensor with unnecessary batch or channel singleton dimensions into the expected rank for a downstream operation.

Targeted shape adjustment

Remove only known singleton positions while preserving other dimensions.

Adapt model outputs

Prepare tensor outputs for APIs that expect scalar-like or lower-rank values.

Interview questions

QWhat does tf.squeeze do?

tf.squeeze removes dimensions of size 1 while preserving the tensor's element type and data. With axis omitted, all singleton dimensions are removed; with axis provided, only the listed singleton dimensions are removed.

Follow-up: What happens if an axis points to a non-singleton dimension? The operation raises ValueError because that dimension cannot be squeezed.

QHow does the axis argument affect tf.squeeze?

axis is an optional list of integer dimension indices, and its default is effectively an empty list that requests removal of all singleton dimensions. Indices start at zero and may be negative within the input rank.

Follow-up: Why pass axis explicitly in production code? It constrains the intended shape transformation and avoids accidentally removing an unrelated singleton dimension.

QWhat does tf.squeeze return, and when can it fail?

The result is a Tensor with the same type as input and the same data, but with one or more dimensions of size 1 removed. ValueError can occur when the input cannot be converted to a tensor or the selected axis cannot be squeezed.

Follow-up: How should a RaggedTensor be handled? Provide axis explicitly, because the documentation requires it for RaggedTensor input.

  • tf.expand_dims
  • tf.reshape

Source: tf.squeeze | TensorFlow. Checked 2026-10-15.