Open In App

Tensor Concatenations in Tensorflow With Example

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Tensor concatenation is a fundamental operation in TensorFlow, essential for combining tensors along specified dimensions. In this article, we will learn about concatenation in TensorFlow and demonstrate the concatenations in python.

tf.concat

In TensorFlow, the tf.concat function combines tensors along a specified axis.

Syntax:

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

Parameters:

values: A list of tensors to concatenate.

axis: The dimension along which to concatenate the tensors.

name: An optional name for the operation.

Concatenating tensors along a specific dimension

Here, we are creating Two example tensors t1 and t2 using TensorFlow’s tf.constant function. These tensors are 2-dimensional, with each containing two rows and three columns.

Along Axis0:

Here, we use the tf.concat() function to concatenate t1 and t2 along axis 0, i.e., along rows. This means the rows of t2 will be appended after the rows of t1.

Python3




import tensorflow as tf
 
# Example tensors
t1 = tf.constant([[1, 2, 3], [4, 5, 6]])
t2 = tf.constant([[7, 8, 9], [10, 11, 12]])
print('Tensor 1:\n', t1)
print('\nTensor 2:\n', t2)
 
# Concatenate along axis 0 (rows)
result_0 = tf.concat([t1, t2], axis=0)
 
print("\nConcatenated along axis 0:\n", result_0)


Output:

Tensor 1:
tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
Tensor 2:
tf.Tensor(
[[ 7 8 9]
[10 11 12]], shape=(2, 3), dtype=int32)
Concatenated along axis 0:
tf.Tensor(
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]], shape=(4, 3), dtype=int32)

Along Axis1:

We use the tf.concat() function to concatenate t1 and t2 along axis 1, i.e., along columns. This means the columns of t2 will be appended after the columns of t1.

Python3




import tensorflow as tf
 
# Example tensors
t1 = tf.constant([[1, 2, 3], [4, 5, 6]])
t2 = tf.constant([[7, 8, 9], [10, 11, 12]])
 
print('Tensor 1:\n', t1)
print('\nTensor 2:\n', t2)
# Concatenate along axis 1 (columns)
result_1 = tf.concat([t1, t2], axis=1)
 
print("\nConcatenated along axis 0:\n", result_1)


Output:

Tensor 1:
tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
Tensor 2:
tf.Tensor(
[[ 7 8 9]
[10 11 12]], shape=(2, 3), dtype=int32)
Concatenated along axis 0:
tf.Tensor(
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]], shape=(2, 6), dtype=int32)

Handling Negative axis

Here, we create Two example tensors t1 and t2 using TensorFlow’s tf.constant function. These tensors are 3-dimensional, with each containing two matrices of size 2×2.

Then, we use the tf.concat() function to concatenate t1 and t2 along the last dimension using a negative axis value (-1). Negative axis values count from the end of the tensor’s shape. In this case, -1 refers to the last dimension.

Python3




import tensorflow as tf
 
# Example tensors
t1 = tf.constant([[[1, 2], [2, 3]], [[4, 4], [5, 3]]])
t2 = tf.constant([[[7, 4], [8, 4]], [[2, 10], [15, 11]]])
 
print('Tensor 1:\n', t1)
print('\nTensor 2:\n', t2)
 
# Concatenate along the last dimension using negative axis
result = tf.concat([t1, t2], axis=-1)
 
print("\nConcatenated along last dimension:\n", result)


Output:

Tensor 1:
tf.Tensor(
[[[1 2]
[2 3]]
[[4 4]
[5 3]]], shape=(2, 2, 2), dtype=int32)
Tensor 2:
tf.Tensor(
[[[ 7 4]
[ 8 4]]
[[ 2 10]
[15 11]]], shape=(2, 2, 2), dtype=int32)
Concatenated along last dimension:
tf.Tensor(
[[[ 1 2 7 4]
[ 2 3 8 4]]
[[ 4 4 2 10]
[ 5 3 15 11]]], shape=(2, 2, 4), dtype=int32)

Handling mismatched dimensions

Here, we create Two example tensors t1 and t2 using TensorFlow’s tf.constant() function. However, t1 has a shape of (2, 3) while t2 has a shape of (2, 2), resulting in mismatched dimensions along axis 1.

Then, we used TensorFlow’s tf.concat() function to concatenate t1 and t2 along axis 0. However, since they have mismatched dimensions along this axis (3 for t1 and 2 for t2), TensorFlow will raise an error when attempting this concatenation.

Python3




import tensorflow as tf
 
# Example tensors with mismatched dimensions
t1 = tf.constant([[1, 2, 3], [4, 5, 6]])
t2 = tf.constant([[7, 8], [9, 10]])
print('Tensor 1:\n', t1)
print('\nTensor 2:\n', t2)
# Concatenation with mismatched dimensions (will raise an error)
# Uncomment the following line to see the error
result = tf.concat([t1, t2], axis=0)


Output:

Tensor 1:
tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
Tensor 2:
tf.Tensor(
[[ 7 8]
[ 9 10]], shape=(2, 2), dtype=int32)
InvalidArgumentError Traceback (most recent call last)
<ipython-input-5-1c59130f5f55> in <cell line: 7>()
5 # Concatenation with mismatched dimensions (will raise an error)
6 # Uncomment the following line to see the error
----> 7 result = tf.concat([t1, t2], axis=0)

Concatenating tensors with varying shapes along the same axis

Here, we create two example tensors t1 and t2 using TensorFlow’s tf.constant() function. t1 has a shape of (2, 3) and t2 has a shape of (2, 2), resulting in differing shapes along axis 1.

Then, we use The tf.pad function to pad t2 with one extra element along axis 1 to match the shape of t1. The padding configuration [[0, 0], [0, 1]] specifies no padding along axis 0 and one element of padding at the end along axis 1.

Finally, we used tf.concat() to concatenate t1 and t2_reshaped along axis 1

Python3




# Reshape t2 to match the shape of t1 along axis 1
t2_reshaped = tf.pad(t2, [[0, 0], [0, 1]])  # Padding to match the shape
result = tf.concat([t1, t2_reshaped], axis=1)
 
print("Concatenated tensors with varying shapes:", result)


Output:

Concatenated tensors with varying shapes: tf.Tensor(
[[ 1 2 3 7 8 0]
[ 4 5 6 9 10 0]], shape=(2, 6), dtype=int32)

Conclusion:

In conclusion, tensor concatenation in TensorFlow is a powerful operation for combining tensors along specified dimensions. In this article, tf.concat’s syntax, usage, and handling of various scenarios, including mismatched dimensions and tensors with varying shapes.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads