To multiply two tensors is a tricky business. Why? Because, there are many factors that influence this multiplication.
Let’s see an example first, after that, we will discuss how it works.
First let’s import the TensorFlow.
# Import TensorFlow
import tensorflow as tf
tensor = tf.constant([[2, 5], [3, 8]])
tensor
# output
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[2, 5],
[3, 8]], dtype=int32)>
Now we can multiply these tensors using two ways.
The first method is simple.
We use a @ keyword. It works as the multiplication symbol like *.
tensor @ tensor
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[19, 50],
[30, 79]], dtype=int32)>
Secondly, we can use the TensorFlow library method “matmul()”.
In addition we need to pass two tensors as arguments.
tf.matmul(tensor, tensor)
# output:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[19, 50],
[30, 79]], dtype=int32)>
However, in both cases, the result is the same.
But how do two tensors multiply? As a result we get a new tensor with the following value.
[
[19, 50],
[30, 79]
]
The initial tensor value was as follows.
[
[2, 5],
[3, 8]
]
When we multiply it looks like this:
[ [
[2, 5], * [2, 5]
[3, 8] [3, 8]
] ]
But the result is as follows.
[
[19, 50],
[30, 79]
]
It appears these two tensors multiply in such a way, that it becomes one. Right?
The multilication starts with the first value of the first row multiplies the first value of first column.
After that, second value of the first row multiplies the second value of the first column.
As a result we get this: 2*2 + 5*3 = 19.
Next, the first value of the first row multiplies the first value of second column.
After that, second value of the first row multiplies the second value of the second column.
In short, that means: 2*5 + 5*8 = 50.
Therefore we get the first row of the new tensor.
[19, 50],
The process continues and we get the second row of the new tensor.
However, this time the second row of the first tensor starts multiplying the first column and the second column repectively.
Leave a Reply