pytorch中的乘法

本文详细介绍了PyTorch中不同类型的乘法操作,包括torch.mul()的元素乘法,torch.mm()的二维矩阵乘法,以及torch.matmul()的广播和批量矩阵乘法。torch.mul()支持自动增加维度并进行广播,而torch.mm()遵循矩阵乘法规则不支持广播。torch.matmul()则可以处理各种情况,包括向量点乘、矩阵与向量乘、批量矩阵乘法等复杂运算。
摘要由CSDN通过智能技术生成

总结:按元素相乘用torch.mul,二维矩阵乘法用torch.mm,batch二维矩阵用torch.bmm,batch、广播用torch.matmul

if __name__=="__main__":
    a = torch.tensor([1, 2, 3])
    b = torch.arange(0, 12).reshape((4, 3))
    c = torch.tensor([4, 5, 6, 7])
    d = torch.arange(0, 12).reshape((3, 4))

    aa = torch.unsqueeze(a, dim=1)
    cc = torch.unsqueeze(c, dim=0)
    print('a:{0},a.shape:{1}'.format(a, a.shape))
    print('b:{0},b.shape:{1}'.format(b, b.shape))
    print('c:{0},c.shape:{1}'.format(c, c.shape))
    print('d:{0},d.shape:{1}'.format(d, d.shape))
    print('aa:{0},aa.shape:{1}'.format(aa, aa.shape))
    print('cc:{0},cc.shape:{1}'.format
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值