numpy.tensordot#

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

Compute tensor dot product along specified axes.

Given two tensors,a andb, and an array_like object containingtwo array_like objects,(a_axes,b_axes), sum the products ofa’s andb’s elements (components) over the axes specified bya_axes andb_axes. The third argument can be a single non-negativeinteger_like scalar,N; if it is such, then the lastN dimensionsofa and the firstN dimensions ofb are summed over.

Parameters:
a, barray_like

Tensors to “dot”.

axesint or (2,) array_like
  • integer_likeIf an int N, sum over the last N axes ofa and the first N axesofb in order. The sizes of the corresponding axes must match.

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

Returns:
outputndarray

The tensor dot product of the input.

See also

dot,einsum

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

Whenaxes is integer_like, the sequence of axes for evaluationwill be: from the -Nth axis to the -1th axis ina,and from the 0th axis to (N-1)th axis inb.For example,axes=2 is the equal toaxes=[[-2,-1],[0,1]].When N-1 is smaller than 0, or when -N is larger than -1,the element ofa andb are defined as theaxes.

When there is more than one axis to sum over - and they are not the last(first) axes ofa (b) - the argumentaxes should consist oftwo sequences of the same length, with the first axis to sum over givenfirst in both sequences, the second axis second, and so forth.The calculation can be referred tonumpy.einsum.

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

Examples

An example on integer_like:

>>>a_0=np.array([[1,2],[3,4]])>>>b_0=np.array([[5,6],[7,8]])>>>c_0=np.tensordot(a_0,b_0,axes=0)>>>c_0.shape(2, 2, 2, 2)>>>c_0array([[[[ 5,  6],         [ 7,  8]],        [[10, 12],         [14, 16]]],       [[[15, 18],         [21, 24]],        [[20, 24],         [28, 32]]]])

An example on array_like:

>>>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)>>>carray([[4400., 4730.],       [4532., 4874.],       [4664., 5018.],       [4796., 5162.],       [4928., 5306.]])

A slower but equivalent way of computing the same…

>>>d=np.zeros((5,2))>>>foriinrange(5):...forjinrange(2):...forkinrange(3):...forninrange(4):...d[i,j]+=a[k,n,i]*b[n,k,j]>>>c==darray([[ True,  True],       [ True,  True],       [ True,  True],       [ True,  True],       [ True,  True]])

An extended example taking advantage of the overloading of + and *:

>>>a=np.array(range(1,9))>>>a.shape=(2,2,2)>>>A=np.array(('a','b','c','d'),dtype=object)>>>A.shape=(2,2)>>>a;Aarray([[[1, 2],        [3, 4]],       [[5, 6],        [7, 8]]])array([['a', 'b'],       ['c', 'd']], dtype=object)
>>>np.tensordot(a,A)# third argument default is 2 for double-contractionarray(['abbcccdddd', 'aaaaabbbbbbcccccccdddddddd'], dtype=object)
>>>np.tensordot(a,A,1)array([[['acc', 'bdd'],        ['aaacccc', 'bbbdddd']],       [['aaaaacccccc', 'bbbbbdddddd'],        ['aaaaaaacccccccc', 'bbbbbbbdddddddd']]], dtype=object)
>>>np.tensordot(a,A,0)# tensor product (result too long to incl.)array([[[[['a', 'b'],          ['c', 'd']],          ...
>>>np.tensordot(a,A,(0,1))array([[['abbbbb', 'cddddd'],        ['aabbbbbb', 'ccdddddd']],       [['aaabbbbbbb', 'cccddddddd'],        ['aaaabbbbbbbb', 'ccccdddddddd']]], dtype=object)
>>>np.tensordot(a,A,(2,1))array([[['abb', 'cdd'],        ['aaabbbb', 'cccdddd']],       [['aaaaabbbbbb', 'cccccdddddd'],        ['aaaaaaabbbbbbbb', 'cccccccdddddddd']]], dtype=object)
>>>np.tensordot(a,A,((0,1),(0,1)))array(['abbbcccccddddddd', 'aabbbbccccccdddddddd'], dtype=object)
>>>np.tensordot(a,A,((2,1),(1,0)))array(['acccbbdddd', 'aaaaacccccccbbbbbbdddddddd'], dtype=object)
On this page