-3

How would you calculate matrix product of the two tensors in PyTorch?

x =  torch.Tensor([[1, 2, 3], [1, 2, 3]]).view(-1, 2)
y =  torch.Tensor([[2, 1]]).view(2, -1)

I am confused between these options.

enter image description here

kmario23
  • 57,311
  • 13
  • 161
  • 150
graseza
  • 51
  • 1
  • 7

1 Answers1

0

You can use one of the options from the code below:

In [188]: torch.einsum("ij, jk -> ik", x, y)
Out[188]: 
tensor([[4.],
        [7.],
        [7.]])

In [189]: x.mm(y)
Out[189]: 
tensor([[4.],
        [7.],
        [7.]])

In [193]: x @ y
Out[193]: 
tensor([[4.],
        [7.],
        [7.]])

In [194]: torch.matmul(x, y)
Out[194]: 
tensor([[4.],
        [7.],
        [7.]])

As you can see, all these approaches would yield us the same result.


x*y is a hadamard product (element-wise multiplication) and will not work in this case. Also, torch.dot() would fail as well because it expects 1D tensors. torch.sum(x*y) would just give a single scalar value and that is also wrong since you wish to do matrix multiplication, not an inner product.

kmario23
  • 57,311
  • 13
  • 161
  • 150