0

What I have in the code I was given is something like:

C = np.tensordot(B, A, axes = (0,0))

A is a (20L, 50L) and B is (20L, 20L)

I was supposed to change since someone told me it would be faster with np.einsum, but I guess I don't fully understand what tensordot is outputing.

Right now C is a (20L, 50L) and I don't understand why?

Of course I have read the documentation page, but still didn't grasp the information. Take into consideration that I have just started working with Python.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Iara
  • 1
  • 1

1 Answers1

0

With A having shape (20,50) and B having shape (20,20):

C = np.tensordot(B, A, axes = (0,0))

is equivalent to:

C = np.zeros((20,50))
for i in range(20):
    for j in range(50):
        for k in range(20):
            C[i,j] += A[k,j] * B[k,i]

which is equivalent to:

C = np.einsum('kj,ki->ij',A,B)

einsum indeed seems to be a bit quicker than tensordot:

%timeit np.einsum('kj,ki->ij',A,B)
10000 loops, best of 3: 20.1 µs per loop

%timeit np.tensordot(B,A,axes=(0,0))
10000 loops, best of 3: 33.6 µs per loop
acidtobi
  • 1,375
  • 9
  • 13