I was reading the documentation for numpy.dot, and it says, verbatim:
If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
I tried it out for larger dimensions, and it checks out:
k = 2
A = np.random.rand(3, 5, 7, k)
B = np.random.rand(9, 11, k, 13)
np.dot(A, B).shape # Shows `(3, 5, 7, 9, 11, 13)`
Can someone explain why is it defined that way? That is, why is the "second-to-last" axis of matrix b (not first)? If it is an optimization / acceleration trick, I would really appreciate an explanation for that, if possible :)