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

TensorFlow · Function

tf.concat

Concatenates tensors along an existing dimension. Inputs must match in rank and in every dimension except the selected axis.

How it works

tf.concat joins a list of tensors along the dimension selected by axis. Inputs must have the same rank and identical sizes everywhere except that dimension; the selected dimension grows by the sum of its input sizes.

Syntax

tf.concat(values, axis, name='concat')

Parameters

NameTypeDescription
valuesTensor or list of TensorA list of Tensor objects or a single Tensor.
axis0-D int32 TensorThe dimension along which to concatenate; a 0-D int32 Tensor in the range [-rank(values), rank(values)).
namestringOptional name for the operation.

Returns

A Tensor resulting from concatenation of the input tensors.

Key points

  • Use tf.concat to join tensors without introducing a new dimension.
  • values accepts either a tensor list or one tensor.
  • The output's selected dimension equals the sum of the corresponding input dimensions.
  • Negative axis values count dimensions from the end.
  • Dimension indices use zero-based numbering.
  • name optionally assigns an operation name.

Common mistakes

  • Inputs cannot differ in rank or in any dimension other than the concatenation dimension.
  • axis must be a valid dimension index, including the documented negative-index range.

Example: Concatenate rows

import tensorflow as tf

left = tf.constant([[1, 2], [3, 4]])
right = tf.constant([[5, 6]])
result = tf.concat([left, right], axis=0)
print(result.numpy())

Joins the two rank-2 tensors by adding the second tensor's rows.

When to use it

Join along the first dimension

Combine batches or rows with matching feature widths.

Join along a feature dimension

Append feature columns while preserving the batch dimension.

Use a runtime axis

Concatenate tensors when the dimension is computed dynamically.

Interview questions

QWhat does tf.concat do, and what shape constraints apply?

tf.concat joins tensors along an existing dimension, so the rank stays unchanged. The inputs must have matching ranks and equal sizes in every non-concatenated dimension.

Follow-up: How does tf.stack differ? tf.stack creates a new dimension, whereas tf.concat extends an existing one.

QHow is axis interpreted?

axis selects the dimension to extend. It is zero-based, and negative values count from the end of the tensor rank.

Follow-up: For rank 3, which dimension does axis=-1 select? It selects dimension 2, the final dimension.

QHow is the output shape determined?

The output size along the selected dimension is the sum of the corresponding sizes from all inputs. The other dimensions retain their common sizes.

Follow-up: What happens if those other dimensions do not match? The operation cannot satisfy the required shape constraints and is invalid.

  • tf.stack
  • tf.expand_dims
  • tf.sparse.concat

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