0

Is there a built in function to calculate efficiently all pairwaise dot products of two tensors in Pytorch? e.g.
input - tensor A (shape NxD)
tensor B (shape NxD)

output - tensor C (shape NxN) such that C_i,j = torch.dot(A_i, B_j) ?

Shai
  • 111,146
  • 38
  • 238
  • 371
obar
  • 357
  • 6
  • 17
  • Have you trired this? https://stackoverflow.com/questions/44524901/how-to-do-product-of-matrices-in-pytorch – Ananda Jan 28 '21 at 11:15

1 Answers1

2

Isn't it simply

C = torch.mm(A, B.T)  # same as C = A @ B.T

BTW,
A very flexible tool for matrix/vector/tensor dot products is torch.einsum:

C = torch.einsum('id,jd->ij', A, B)
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    Did you mean `torch.mm(A, B.T)`? – Ivan Jan 28 '21 at 11:26
  • @Ivan is there a difference between `torch.mm`, `torch.bmm` and `torch.dot` for 2d tensors? – Shai Jan 28 '21 at 11:29
  • @Ivan one can always use the more general `torch.tensordot` and my personal favorite `torch.einsum`... – Shai Jan 28 '21 at 11:30
  • I believe `torch.dot` only works with 1D tensors. As for `torch.bmm`, it expects a *batch* axis, *i.e.* a third axis. – Ivan Jan 28 '21 at 11:32
  • Do you know about the efficiency of `torch.einsum` *vs.* specific operators such as `torch.mm`, is it as good? – Ivan Jan 28 '21 at 16:00