torch.mm, torch.mul, torch.matmul的区别

一、点乘

  • torch.mul(a, b)是矩阵a和b对应位相乘,a和b的维度一般相等,比如a的维度是(1, 2),b的维度是(1, 2),返回的仍是(1, 2)的矩阵
>>> a = torch.rand(1, 2)
>>> b = torch.rand(1, 2)
>>> torch.mul(a, b)  # 返回 1*2 的tensor

#乘列向量
>>> a = torch.ones(3,4) 
>>> a
tensor([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> b = torch.Tensor([1,2,3]).reshape((3,1))
>>> b
tensor([[1.],
       [2.],
       [3.]])
>>> torch.mul(a, b)
tensor([[1., 1., 1., 1.],
       [2., 2., 2., 2.],
       [3., 3., 3., 3.]])

二、矩阵相乘

  • torch.mm(a, b)是矩阵a和b矩阵相乘,比如a的维度是(3, 4),b的维度是(4, 2),返回的就是(3, 2)的矩阵torch.mm(a, b)针对二维矩阵
>>> a = torch.ones(3,4)
>>> b = torch.ones(4,2)
>>> torch.mm(a, b)
tensor([[4., 4.],
        [4., 4.],
        [4., 4.]])
  • torch.matmul(a, b)一般是高维矩阵a和b相乘,比如a的维度是(3, 4),b的维度是(5, 4, 2),返回的就是(5, 3, 2)的矩阵,因为后两维(3, 4)和(4, 2)矩阵相乘的结果是(3, 2)
>>> a = torch.ones(5,4,2)
>>> b = torch.ones(5,2,3)
>>> torch.matmul(a, b).shape
torch.Size([5, 4, 3])

参考
https://blog.csdn.net/da_kao_la/article/details/87484403?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
https://blog.csdn.net/weixin_42105432/article/details/100691592
https://blog.csdn.net/Real_Brilliant/article/details/85756477?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值