How to concatenate two tensors horizontally in TensorFlow?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Concatenating tensors is a fundamental operation in TensorFlow, used frequently when merging feature vectors, combining model outputs, or preparing input batches. Horizontal concatenation joins tensors along columns (axis 1 for 2D tensors), while vertical concatenation joins along rows (axis 0). Understanding how tf.concat works with different axes and tensor shapes prevents common runtime errors.
Horizontal Concatenation with tf.concat
The primary function for combining tensors is tf.concat. To concatenate two 2D tensors side by side (horizontally), set axis=1.
The result has the same number of rows as the inputs, but the columns are combined. Both tensors must have the same number of rows for this to work.
Vertical Concatenation with axis=0
Setting axis=0 stacks tensors vertically, combining rows.
For vertical concatenation, the column count must match across all input tensors.
Concatenating Along Higher Dimensions
With 3D tensors (common in batch processing and sequence models), the axis parameter selects which dimension to join.
Using negative indexing also works. axis=-1 always refers to the last dimension, which is equivalent to horizontal concatenation for 2D tensors.
tf.concat vs tf.stack
While tf.concat joins tensors along an existing axis, tf.stack creates a new axis and stacks the tensors along it. This changes the output rank.
Use tf.stack when you want to create a batch dimension from individual samples. Use tf.concat when you want to extend an existing dimension.
Handling Shape Mismatches
TensorFlow raises an InvalidArgumentError when dimensions other than the concatenation axis do not match. You can pad or reshape tensors before concatenating.
Concatenating Ragged Tensors
When working with sequences of variable length, tf.ragged provides specialized support.
Ragged tensors allow concatenation without requiring uniform shapes on non-concat dimensions, which is useful for NLP and variable-length sequence processing.
Common Pitfalls
- Confusing axis=0 and axis=1 leads to unexpected output shapes; axis=1 is horizontal (column-wise) for 2D tensors, while axis=0 is vertical (row-wise).
- Mixing tf.concat with tf.stack without realizing that
tf.stackadds a new dimension whiletf.concatextends an existing one, resulting in unexpected tensor ranks. - Rank mismatch between inputs (for example, concatenating a 2D tensor with a 3D tensor) causes an immediate error; reshape or expand dimensions first with
tf.expand_dims. - Forgetting that all non-concat dimensions must match exactly is the most common shape error; use
tf.padortf.reshapeto align shapes before concatenation. - Not specifying the axis parameter defaults to
axis=0, which is vertical concatenation; always pass the axis explicitly for clarity.
Summary
- Use
tf.concat([a, b], axis=1)for horizontal concatenation andaxis=0for vertical concatenation of 2D tensors. - Use
axis=-1as a portable shorthand for concatenating along the last dimension regardless of tensor rank. - Choose
tf.stackovertf.concatwhen you need to create a new dimension, such as forming a batch from individual samples. - All dimensions except the concatenation axis must match; use
tf.padortf.reshapeto fix shape mismatches before concatenation. - For variable-length data, use
tf.raggedtensors, which support concatenation without requiring uniform shapes on non-concat dimensions.

