torch.mul torch.bmm

torch.bmm:https://blog.csdn.net/guotong1988/article/details/78707619 

参考 https://blog.csdn.net/Real_Brilliant/article/details/85756477

batch matrix multiply  相当于矩阵的乘法。

>>> batch1 = torch.randn(10, 3, 4)
>>> batch2 = torch.randn(10, 4, 5)
>>> res = torch.bmm(batch1, batch2)
>>> res.size()
torch.Size([10, 3, 5])

 

  • torch.mul(a, b)是矩阵a和b对应位相乘,a和b的维度必须相等,比如a的维度是(1, 2),b的维度是(1, 2),返回的仍是(1, 2)的矩阵
  • torch.mm(a, b)是矩阵a和b矩阵相乘,比如a的维度是(1, 2),b的维度是(2, 3),返回的就是(1, 3)的矩阵
  • import torch
     
    a = torch.rand(1, 2)
    b = torch.rand(1, 2)
    c = torch.rand(2, 3)
     
    print(torch.mul(a, b))  # 返回 1*2 的tensor
    print(torch.mm(a, c))   # 返回 1*3 的tensor
    print(torch.mul(a, c))  # 由于a、b维度不同,报错
    >>> import torch
    >>> a =[ [[1,2,3],[2,3,4]]]
    >>> a = torch.Tensor(a)
    >>> b =[ [[1,2,3],[2,3,4]]]
    >>> b = torch.Tensor(b)
    >>> c = torch.mul(a,b)
    >>> c
    tensor([[[ 1.,  4.,  9.],
             [ 4.,  9., 16.]]])
    >>> c.requires_grad
    False
    >>> d = a*b
    >>> d
    tensor([[[ 1.,  4.,  9.],
             [ 4.,  9., 16.]]])
    >>> d.requires_grad
    False
    >>> 
    

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值