0

I have a bunch of 5-dimensional vectors of shape (5,) in a 2-dimensional array of shape (1000, 5), each of which which I want to multiply by a matrix of shape (6, 5).

I would have assumed that broadcasting would allow me to do

A = np.random.rand(1000, 5)
B = np.random.rand(1, 6, 5) # empty axis for broadcasting
np.matmul(B, A)

but this doesn't work properly.

Is there a way to do this kind of multiplication so that np.matmul(B, A) produces an output of shape (1000, 6)?

David
  • 567
  • 4
  • 16
JAustin
  • 890
  • 10
  • 16

2 Answers2

1

If you actually have this:

A = np.random.rand(1000, 5)
B = np.random.rand(6, 5)

Then a much easier way to do this operation is

A @ B.T

or

A.dot(B.T)

Remember matrix multiplaction is an inner product, so the inner dimensions must match (in this case, be 5). By transposing .T you swap the 6 and 5 dimensions to get them in the right order.

If you don't want to play with the order (say you have a ton of dimensions), you can also always set the axes explicitly with np.einsum

np.einsum('ij,kj->ik', A, B, optimize = True)

optimize makes this almost as fast as .dot() or @, but currently only works when you don't need to broadcast a dimension on your inputs (you don't here).

Daniel F
  • 13,620
  • 2
  • 29
  • 55
0

I just figured the answer out. The solution is to format the arrays as

A = np.ones((100, 5, 1))
B = np.ones((6, 5))
np.matmul(B, A)

and the broadcasting comes out all right. Hope this helps!

JAustin
  • 890
  • 10
  • 16