Tensor multiplication error

In the previous section we have seen how tensor multiplication works. But when does tensor multiplication give error?

Here we will examine that. In addition, we will also learn the definite rule of tensor multiplication.

Let’s import the TensorFlow first.

Firstly, we will create two tensors with the constant method. 

By the way, we have discussed earlier how the attributes and methods work in the TensorFlow library.

import tensorflow as tf

X = tf.constant([[1, 2, 3], [4, 5, 6]])
X
# output:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>

Y = tf.constant([[1, 2], [3, 4], [5, 6]])
Y
# output:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4],
       [5, 6]], dtype=int32)>

As we see, we have created two different types of tensors. 

The first tensor is of shape (2, 3). 

What does that mean?

It means, there are 2 rows and 3 columns.

However, the second tensor is of shape (3, 2).

That means it has 3 rows and 2 columns.

Since the number of rows of the first tensor matches the number of columns of the second tensor, it will multiply.

As a result, we won’t get any error. 

# this will work
X @ Y
# output:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[22, 28],
       [49, 64]], dtype=int32)>

We have discussed how tensor multiplication takes place before.

Therefore we are not discussing it anymore.

But, what will happen if we try to multiply two tensors of equal shape?

Let’s try and see what happens.

Firstly, create two tensors of the same shape.

M = tf.constant([[1, 2, 3], [4, 5, 6]])
M
# output:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>

N = tf.constant([[7, 8, 9], [10, 11, 12]])
N
# output:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[ 7,  8,  9],
       [10, 11, 12]], dtype=int32)>

And then we will try to multiply them.

# this will not work
M @ N

# output:
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-9-f94c82b8db97> in <module>()
      1 # this will not work
----> 2 M @ N

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
    151     except Exception as e:
    152       filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153       raise e.with_traceback(filtered_tb) from None
    154     finally:
    155       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   7184 def raise_from_not_ok_status(e, name):
   7185   e.message += (" name: " + name if name is not None else "")
-> 7186   raise core._status_to_exception(e) from None  # pylint: disable=protected-access
   7187 
   7188 

InvalidArgumentError: Matrix size-incompatible: In[0]: [2,3], In[1]: [2,3] [Op:MatMul]

What does the error say?

In summary it says that matrix size is incompatible.

What does that mean?

It means the number of rows of the first tensor and the number of columns of the second tensor is not the same. 

It differs.

Quite true, because the number of rows of the first tensor is 2. But, the number of columns of the second tensor is 3.

It should have matched. However, that didn’t happen.

How can we solve this problem?

Simple. 

We can reshape any one of the tensor constants.

Reshaping tensor and rectify the multiplication error

We have used the reshape() method of the TensorFlow library before.

All we need to pass is the constant as the first parameter and the new shape as the second parameter.

Let’s do that first.

M = tf.reshape(M, shape=(3, 2))
M

# output:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4],

Now the shape of the one tensor constant becomes (3, 2).

As a result, the multiplication will work this time.

# this will work
M @ N

# outptut:
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[ 27,  30,  33],
       [ 61,  68,  75],
       [ 95, 106, 117]], dtype=int32)>

Let us check the multiplication in a close manner.

When we write M @ N, what does that mean?

It means as follows.

(3, 2) @ (2, 3)

The inner dimensions of two tensors are 2 and 2. And the outer dimension of the two tensors are 3 and 3.

Since they are equal, the tensor multiplication will always work.

This is the golden rule, you should keep in mind, when you multiply two tensors.

For more TensorFlow related code, please visit this GitHub Repository.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

Twitter

Comments

Leave a Reply