tensor乘运算

torch.mul(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)的矩阵

torch.bmm(a,b)是矩阵a和b在维度1、2上矩阵相乘,一般要求是 三维矩阵,a的维度是(64,1,2),b的维度是(64,2,3)返回的是(64,1,3)矩阵

import torch


if __name__ == "__main__":
    x = torch.ones(1, 2)
    y = torch.ones(1, 2) * 2
    z = torch.mul(x, y)
    print("torch.mul() example ")
    print("x.shape is ", x.shape) 
    print("y.shape is ", y.shape)
    print("z.shape is ", z.shape) ## [1, 2]

    x = torch.ones(1, 2)
    y = torch.ones(2, 3) 
    z = torch.mm(x, y)
    print("torch.mm() example ")
    print("x.shape is ", x.shape)
    print("y.shape is ", y.shape)
    print("z.shape is ", z.shape) ## [1,3]

    x = torch.randn(64, 1, 2)
    y = torch.randn(64, 2, 3)
    z = torch.bmm(x, y)
    print("torch.bmm example ")
    print("x.shape is ", x.shape)
    print("y.shape is ", y.shape)
    print("z.shape is ", z.shape) ## [64, 1, 3]

转载于:https://www.cnblogs.com/yeran/p/11288171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值