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]


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM