关于torch中几种矩阵相乘的比较
作者:互联网
深度学习中免不了要用到torch,经常用到tensor的矩阵相乘,每次都会记错,所以这次把几种tensor的几种矩阵相乘的方法放到一起比较下:
import torch
a = torch.tensor([[1,1],
[2,2]])
b = torch.tensor([[1,1],
[0,2]])
result1 = torch.mm(a,b)#矩阵相乘
print(result1)
result2 = torch.mul(a,b)#对应位相乘
print(result2)
result3 = a * b#对应位相乘
print(result3)
result4 = a @ b#矩阵相乘
print(result4)
输出:
tensor([[1, 3], [2, 6]]) tensor([[1, 1], [0, 4]]) tensor([[1, 1], [0, 4]]) tensor([[1, 3], [2, 6]])
标签:tensor,矩阵,torch,result2,相乘,print 来源: https://www.cnblogs.com/lyxblogs/p/16272119.html