This article will help you to understand how to calculate a tensor dot product using the tensordot( ) function from the numpy library. Once a pair of tensors whose dot product is to be found are fed as inputs in the form of arrays, the tensordot( ) function sums the products of a’s and b’s elements over the axes specified.
Also read: Numpy Trace: A Guide to Calculating Trace Using Numpy in Python
Let’s get started in understanding this function by first importing the numpy library using the below code.
import numpy as np
We shall further explore the tensordot( ) function through each of the following sections.
- Syntax of tensordot( ) function
- Calculating Tensor Dot Product
- Similarities & Differences with numpy.dot( )
Syntax of tensordot( ) function
The following are the constructs that are required for the effective functioning of the tensordot( ) function.
numpy.tensordot(a, b, axes)
- a – array-like object which serves as the first tensor
- b – array-like object which serves as the second tensor
- axes – can be a scalar or array-like object which specifies the axes along which the dot product is to be calculated
Calculating Tensor Dot Product
In this section let us find the tensor dot product of a pair of tensors given in the form of N-dimensional arrays as shown below.
ar1 = np.array([[1, 3, 7], [2, 9, 4]]) ar2 = np.array([[5, 6], [8, 0], [1, 7]])
Now let us deploy the tensordot( ) function specifying the direction of axes as ‘1’.
np.tensordot(ar1, ar2, axes=1)
Once the above code is run, the following computation happens in the back end for calculating the results which shall be returned in the form of an array.
- The first element from the first row of the first tensor is multiplied by the first element of the first column of the second tensor (i.e) 1×5 = 5.
- The second element from the first row of the first tensor is multiplied by the second element of the first column of the second tensor (i.e) 3×8 = 24.
- The same happens with the subsequent third elements in the first & the second tensor too (i.e) 7×1 = 7. To put things in perspective here are the inputs printed for the readers to comprehend better.
- Once done, the product of all the elements deduced above are summed up to obtain the first element of the first row of the tensor dot product (i.e) 5+24+7 = 36.
- The same process is iterated with the subsequent rows and columns of the input tensors to find the other elements of the tensor dot product.
- The second element of the first row of the tensor dot product is retrieved from (1×6) + (3×0) + (7×7) = 55
- The first element of the second row of the tensor dot product is retrieved from (2×5) + (9×8) + (4×1) = 86
- The second element of the second row of the tensor dot product is retrieved from (2×6) + (9×0) + (4×7) = 40
Following is the result that we get after the code is run and one can find the striking similarity between the element-wise deductions detailed above with that which has been displayed below.
Similarities & Differences with numpy.dot( )
When the same inputs are run through the numpy.dot( ) function the results obtained can bear an uncanny resemblance.
This is because setting the axes to ‘1’ while running a tensordot( ) function results in a conventional matrix dot product, which explains the above similarity. However, changing the axes setting to, say zero (0) for instance, would dictate the direction in which the tensordot( ) needs to compute yielding a different result.
np.tensordot(ar1, ar2, axes=0)
The underlying logic behind the computation of the above result is multiplying the entire array ‘ar2’ with each element of ‘ar1’. The very first sub-array is a result of multiplying ‘ar2’ with the first element of ‘ar1’ ~ 1!
Now that we have reached the end of this article, hope it has elaborated on how to use the tensordot( ) function from the numpy library to calculate the tensordot product for the given pair of tensors. Here’s another article that explains the trace( ) function from numpy in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, hasta luego!