1

I am using pytorch.

If I have a matrix M of size (d1, d2) and a vector V of size d2, doing M*V gives me an output OUT of size (d1, d2), where each row of M has been multiplied by V.

I need to do the same thing batch-wise, where the matrix M is fixed and I have a batch of dB vectors.

In practice, given a tensor M of size (d1, d2) and a tensor V of size (dB, d2), I need to get as output OUT a tensor of size (dB, d1, d2), so that OUT[i] = M*V[i].

How can I efficiently get it with pytorch?

2 Answers 2

3

This simple trick works for the problem:

M.unsqueeze(0) * V.unsqueeze(1) 

This does multiplications of tensors having shapes (1, d1, d2) and (dB, 1, d2) and you get the desired output having shape (dB, d1, d2).

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Einstein Notation to achieve this:

torch.einsum('ij,bj->bij', M, V) 

1 Comment

Wow, great I hadn't seen it yet!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.