torch matmul two matrix row by row
I want to find a decent way to write the below function in torch. Appreciate for clean solution to complete this.
import torch
a=torch.randn(3,100)
b=torch.randn(3,100)
row_num = a.size()[0] # 3

# Given two matrix with shape (n1,n2)
# I want to have the row-wise `matmul` results which will result in a tensor with size (n1, ) 

scores = []
for i in range(row_num):
    score_i = a[i,:].matmul(b[i,:])
    scores.append(score_i)
expected_result = torch.tensor(scores)


The operation you are trying to do is essentially the values of a dot product (matmul, a @ b.T) which lie on its diagonal.
You can get the same using torch.matmul or @ operator between a and b.T and then get the torch.diagonal -
np.diagonal(a @ b.T)

You can also use torch.einsum directly to get the same result -
torch.einsum('ij,ij-&gt;i',a,b)

