🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

tf.concat()

AI & DATA SCIENCE // tf-concat

tf.concat() joins a list of tensors together along an existing dimension, without adding any new dimension.

Syntax

tf.concat(values, axis)

Deep Dive Course

concat() requires every input tensor to have the same shape along every dimension except the one being joined, and the same total number of dimensions overall — joining along axis=0 stacks tensors end-to-end along their first dimension, like adding more rows, while axis=1 joins them side by side along the second dimension instead. Unlike stacking with tf.stack(), which creates a brand-new dimension, concat() only extends an existing one, so the result has the same number of dimensions as the inputs.

1Understanding tf.concat()

concat() requires every input tensor to have the same shape along every dimension except the one being joined, and the same total number of dimensions overall — joining along axis=0 stacks tensors end-to-end along their first dimension, like adding more rows, while axis=1 joins them side by side along the second dimension instead. Unlike stacking with tf.stack(), which creates a brand-new dimension, concat() only extends an existing one, so the result has the same number of dimensions as the inputs.

💡

Use tf.concat() when combining tensors should extend an existing dimension, like adding more rows or more columns — reach for tf.stack() instead if you need to combine tensors along a genuinely new dimension that didn't exist before.

editor.html
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 6]])
result = tf.concat([a, b], axis=0)
print(result)
localhost:3000

2Practical Example

Here is a real-world application of tf.concat() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5], [6]])
result = tf.concat([a, b], axis=1)
print(result)
localhost:3000

3Best Practices

Follow these guidelines when working with tf.concat():

1. Use concat() when joining tensors should extend an existing axis, not create a new one — use tf.stack() instead if you need a genuinely new dimension

2. Verify all input tensors match in every dimension except the one you're concatenating along, since a mismatch there raises a shape error

3. Specify the axis parameter explicitly and deliberately, rather than assuming a default, since concat() requires you to state which dimension to join along

⚠️

Tip: Use tf.concat() when combining tensors should extend an existing dimension, like adding more rows or more columns — reach for tf.stack() instead if you need to combine tensors along a genuinely new dimension that didn't exist before.

editor.html
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 6]])
result = tf.concat([a, b], axis=0)
print(result)
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 6]])
result = tf.concat([a, b], axis=0)
print(result)
Example 02Advanced Example
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5], [6]])
result = tf.concat([a, b], axis=1)
print(result)

Best Practices

  • Use concat() when joining tensors should extend an existing axis, not create a new one — use tf.stack() instead if you need a genuinely new dimension
  • Verify all input tensors match in every dimension except the one you're concatenating along, since a mismatch there raises a shape error
  • Specify the axis parameter explicitly and deliberately, rather than assuming a default, since concat() requires you to state which dimension to join along

Interview Question

What's the fundamental difference between tf.concat() and tf.stack(), given both combine multiple tensors into one?

Hint: Think about whether the result gains an extra dimension compared to the inputs.

concat() joins tensors along an axis that already exists in each of them, so the resulting tensor has the same number of dimensions as the original inputs, just larger along the joined axis. stack() instead creates a brand-new axis that didn't exist in the original tensors, lining the inputs up along it, so the result always has one more dimension than each individual input — concatenating a list of 1D tensors gives a longer 1D tensor, while stacking the same list gives a 2D tensor instead.

Exercises

MediumPractice using tf.concat() in a real scenario.
View Solution
import tensorflow as tf

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

Frequently Asked Questions

What's the fundamental difference between tf.concat() and tf.stack(), given both combine multiple tensors into one?

concat() joins tensors along an axis that already exists in each of them, so the resulting tensor has the same number of dimensions as the original inputs, just larger along the joined axis. stack() instead creates a brand-new axis that didn't exist in the original tensors, lining the inputs up along it, so the result always has one more dimension than each individual input — concatenating a list of 1D tensors gives a longer 1D tensor, while stacking the same list gives a 2D tensor instead.

Related Functions

tf-expand-dimsnp-concatenatenp-stack