numpy dot with more than 2 dimensions

Viewed 63

In the numpy dot(...) function documentation it is said that:

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

This is sensibly different from the matmul(...) function, where the matrices product over the last two dimensions is simply broadcasted along the first dimensions (if the matrix has more than 2 dimensions).

However, while it is clear the utility of the matmul behaviour, I wonder what the possible applications are for the result given by dot(...) for matrix with more than 2 dimensions. Is there any pratical application where dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) can be useful?

1 Answers

I don't recall any discussions as to why this approach was chosen. It may just be for ease of calculation. Its limitations have led to the development of einsum and later matmul.

dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

einsum equivalent

np.einsum('ijl,klm->ijkm', a, b)

with a transpose and reshape it can be written as

np.einsum('pl,lo', A, B)    # p=ij, o=km

A,B can then be passed to conventional BLAS functions. That functionality might even be built into BLAS.

Related