np.tensordot: How to Use tensordot() Method in Python

Numpy tensordot() calculates the tensor dot product of two given tensors.

np.tensordot

The np.tensordot() is a numpy library function that calculates the tensor dot product along specified axes. The tensordot() function sums the product of a’s elements and b’s elements over the axes specified by a_axes and b_axes.

These a_axes and b_axes can be a scaler, too, let’s say N. In this case, the last N dimension of the given tensors is summed over.

Given two tensors, a and b, and an array_like object containing two array_like objects (a_axes, b_axes), sum the products of a’s and b’s elements (components) over the axes specified by a_axes and b_axes.

The third argument can be a single non-negative integer_like scalar, N; if it is such, then the last N dimensions of a and the first N dimensions of b are summed over.

Syntax

numpy.tensordot(tensor1, tensor2, axes)

Parameters

The tensordot() function takes three main arguments:

  1. tensor1: This is an array-like object.
  2. tensor2: This is also an array-like object. We will calculate the tensordot of tensor1 and tensor2.
  3. axes: This can be a scalar as well as an array-like object. This indicates the axes on which we have to find tensordot.

Return Value

The tensordot() function returns the tensordot product of the given tensors.

Calculating tensor dot when axes is an array-like object

See the following code.

# Programming example of tensordot when axes is array-like object
import numpy as np
# Declaring arrays
arr1 = np.arange(8).reshape(4, 2)
arr2 = np.arange(4, 12).reshape(2, 4)
print("Tensor1 is: ", arr1)
print("\nTensor2 is: ", arr2)

# Now we will calculate tensor dot
ans = np.tensordot(arr1, arr2, axes=([1, 0], [0, 1]))
print("Tensordot of these tensors is:\n", ans)

Output

Tensor1 is:  [[0 1]
 [2 3]
 [4 5]
 [6 7]]

Tensor2 is:  [[ 4  5  6  7]
 [ 8  9 10 11]]
Tensordot of these tensors is:
 238

Explanation

In this programming example, we have first declared two tensors and printed them in the output. Then we have called the tensordot() function to calculate the tensordot of these two given tensors. In this case, our given axes are array-like objects.

Calculating tensor dot when axes is a scalar

See the following code.

# Programming example of tensordot when axes is an scalar
import numpy as np
# Declaring arrays
arr1 = np.arange(8).reshape(4, 2)
arr2 = np.arange(4, 12).reshape(2, 4)
print("Tensor1 is: ", arr1)
print("\nTensor2 is: ", arr2)

# Now we will calculate tensor dot
ans = np.tensordot(arr1, arr2, axes=1)
print("Tensordot of these tensors is:\n", ans)

Output

Tensor1 is:  [[0 1]
 [2 3]
 [4 5]
 [6 7]]

Tensor2 is:  [[ 4  5  6  7]
 [ 8  9 10 11]]
Tensordot of these tensors is:
 [[  8   9  10  11]
 [ 32  37  42  47]
 [ 56  65  74  83]
 [ 80  93 106 119]]

Explanation

In this programming example, we have first declared two tensors and printed them in the output. Then we have called the tensordot() function to calculate the tensordot of these two given tensors. In this case, our given axes are scalar values.

Conclusion

The idea with tensordot is pretty simple – We input the arrays and the respective axes along which the sum reductions are intended.

The axes that take part in sum-reduction are removed from the output, and all of the remaining axes from the input arrays are spread out as different axes in the output, keeping the order in which the input arrays are fed.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.