Einsum in python

66 Views Asked by At

Please explain me what exactly happening in the einsum of the below code. The output of the code gives a size of (6,6,6,6,6) tensor. Does is actually performing a outer product?

import numpy as np
a1 = np.arange(6)
a2 = np.arange(6)
a3 = np.arange(6)
a4 = np.arange(6)
a5 = np.arange(6)
oo = np.einsum('i,j,k,l,m',a1,a2,a3,a4,a5)
2

There are 2 best solutions below

0
On

Each element of the 6,6,6,6,6 tensor is the result of the product of the elements from the input arrays,

i.e. the element oo[0,2,3,0,1] will be a1[0] * a2[2] * a3[3] * a4[0] * a5[1]

0
On

Yes, agree with @Attack68 . And I add an explicit example in 2 dimensions

a1 = np.array([0, 1, 2, 3])
a2 = np.array([5, 7, 11])
oo = np.einsum('i,j', a1, a2)

The output oo is :

array([[ 0,  0,  0],
       [ 5,  7, 11],
       [10, 14, 22],
       [15, 21, 33]])