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]
posted @ 2019-08-02 13:53  烨然2333  阅读(2062)  评论(0编辑  收藏  举报