简体   繁体   中英

Unexpected result with numpy.dot

I have two matrices:

>>> a.shape
(100, 3, 1)
>>> b.shape
(100, 3, 3)

I'd like to perform a dot product such that my end result is (100, 3, 1). However, currently I receive:

>>> c = np.dot(b, a)
>>> c.shape
(100, 3, 100, 1)

Could somebody explain what is happening? I'm reading thedocs and can't figure it out.

Edit:

So per the docs (overlooked it):

If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.

And that gives the desired result, but I'm still curious, what is taking place here? What rule of the np.dot function is being applied to yield a result of (100, 3, 100, 1) ?

This is how dot works in your case:

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

Your output shape is exactly how the docs specify it:

(b.shape[0], b.shape[1], a.shape[0], a.shape[2])

If it's not what you expected, you might be looking for another matrix multiplication.

dot will return all the possible products of the matrices stored in the last two dimensions of your arrays. Use matmul aka the @ operator to broadcast the leading dimensions instead of combining them:

np.matmul(b, a)

Or

b @ a

The Swiss Army knife of sum-products is einsum , so you can use that too:

np.einsum('aij,ajk->aik', b, a)

Or

np.einsum('ajk,aij->aik', a, b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM