Join along the first dimension
Combine batches or rows with matching feature widths.
TensorFlow · Function
Concatenates tensors along an existing dimension. Inputs must match in rank and in every dimension except the selected axis.
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.
tf.concat(values, axis, name='concat')| Name | Type | Description |
|---|---|---|
values | Tensor or list of Tensor | A list of Tensor objects or a single Tensor. |
axis | 0-D int32 Tensor | The dimension along which to concatenate; a 0-D int32 Tensor in the range [-rank(values), rank(values)). |
name | string | Optional name for the operation. |
A Tensor resulting from concatenation of the input tensors.
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.
Combine batches or rows with matching feature widths.
Append feature columns while preserving the batch dimension.
Concatenate tensors when the dimension is computed dynamically.
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.
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.
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.stacktf.expand_dimstf.sparse.concat