tensordot
Description
Compute tensor dot product along specified axes.
Given two tensors, a and b, and an integer or two array_like objects (a_axes and b_axes), calculate the product of a and b over the axes specified by a_axes and b_axes.
Mandatory Input Parameters
Parameter |
Type |
Description |
|---|---|---|
a |
array_like |
First tensor |
b |
array_like |
Second tensor |
axes |
int or (2,) array_like |
If it is an integer, the last N axes of a and the first N axes of b are summed over in order. The sizes of the corresponding axes must match. If it is two arrays, it is a list of axes to be summed over. The first sequence applies to a and second to b. Both array_like elements must be of the same length. The array_like elements represent the axes to be deleted from the sequences a and b. |
Optional Input Parameters
None
Return Value
Type |
Description |
|---|---|
ndarray |
Dot product of the input tensors |
Examples
>>> import numpy as np
>>> a = np.arange(60).reshape(3,4,5)
>>> b = np.arange(24).reshape(4,3,2)
>>> a
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],
[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
>>> b
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17]],
[[18, 19],
[20, 21],
[22, 23]]])
>>> c = np.tensordot(a, b, axes=([1,0],[0,1]))
>>> c
array([[4400, 4730],
[4532, 4874],
[4664, 5018],
[4796, 5162],
[4928, 5306]])
>>> c.shape
(5, 2)
>>>
>>> d = np.zeros((5,2))
>>> for i in range(5):
... for j in range(2):
... for k in range(3):
... for n in range(4):
... d[i,j] += a[k,n,i] * b[n,k,j]
...
>>> c == d
array([[ True, True],
[ True, True],
[ True, True],
[ True, True],
[ True, True]])
>>>