Matrix Multiplication Question

I’m working on an embedding problem. I want to multiply each row of a tensor with another matrix and sum the result. I’m struggling with how to do this without using a loop. The following example works via indexing (row 0 for tensor x). How do I do this without having to use a loop?

import torch
x = torch.rand(100,120)
y = torch.rand(514, 120)
result = (x[0]*y).sum(0)
print(result)

You could use einsum.

import torch
x = torch.rand(100,120)
y = torch.rand(514, 120)

# Yours
result_1 = (x[0]*y).sum(0)

# Einsum
result_2 = torch.einsum("ij, kj -> ij", [x, y])

# Select only the first row of result_2 to compare to the indexed version
print(torch.allclose(result_1, result_2[0]))
# Output:
True
1 Like

I ran across the einsum when doing some searching but wasn’t sure if it was the right solution or not. Thanks for taking the time for pointing me in the right direction, thank you!

1 Like