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)

Tensor Concatenations in Tensorflow With Example

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.

Similar Reads

tf.concat

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

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....

Handling Negative axis

...

Handling mismatched dimensions

...

Conclusion:

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....

Contact Us