python,arrays,numpy,numpy-einsum

This should do the trick, assuming you are 'dotting' the last dimension of m with the last of a: np.einsum('ij,klj->ikl',m,a) ...

python,numpy,matrix-multiplication,numpy-einsum

Didn't you do something like similarity_matrix = np.empty((N,M),dtype=float) at the start of your calculations? You can't index an array, on right or left side of an equation, before you create it. If that full (N,M) matrix is too big for memory, then just assign your einsum value to another variable,...