dpnp.tensordot

dpnp.tensordot(a, b, axes=2)[source]

Compute tensor dot product along specified axes.

For full documentation refer to numpy.tensordot.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray, scalar}) -- First input array. Both inputs a and b can not be scalars at the same time.

  • b ({dpnp.ndarray, usm_ndarray, scalar}) -- Second input array. Both inputs a and b can not be scalars at the same time.

  • axes (int or (2,) array_like) --

    • integer_like: If an int N, sum over the last N axes of a and the first N axes of b in order. The sizes of the corresponding axes must match.

    • (2,) array_like: A list of axes to be summed over, first sequence applying to a, second to b. Both elements array_like must be of the same length.

Returns:

out -- Returns the tensor dot product of a and b.

Return type:

dpnp.ndarray

See also

dpnp.dot

Returns the dot product.

dpnp.einsum

Evaluates the Einstein summation convention on the operands.

Notes

Three common use cases are:
  • axes = 0 : tensor product \(a \otimes b\)

  • axes = 1 : tensor dot product \(a \cdot b\)

  • axes = 2 : (default) tensor double contraction \(a:b\)

When axes is integer, the sequence for evaluation will be: first the -Nth axis in a and 0th axis in b, and the -1th axis in a and Nth axis in b last.

When there is more than one axis to sum over - and they are not the last (first) axes of a (b) - the argument axes should consist of two sequences of the same length, with the first axis to sum over given first in both sequences, the second axis second, and so forth.

The shape of the result consists of the non-contracted axes of the first tensor, followed by the non-contracted axes of the second.

Examples

>>> import dpnp as np
>>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> b = np.array([1, 2, 3])
>>> np.tensordot(a, b, 1)
array([14, 32, 50])
>>> a = np.arange(60.).reshape(3,4,5)
>>> b = np.arange(24.).reshape(4,3,2)
>>> c = np.tensordot(a,b, axes=([1,0],[0,1]))
>>> c.shape
(5, 2)
>>> c
array([[4400., 4730.],
       [4532., 4874.],
       [4664., 5018.],
       [4796., 5162.],
       [4928., 5306.]])

A slower but equivalent way of computing the same...

>>> 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]])