zoukankan      html  css  js  c++  java
  • 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]
    
    如果有一天我们淹没在茫茫人海中庸碌一生,那一定是我们没有努力活得丰盛
  • 相关阅读:
    Linux命令笔记
    拆功放板笔记
    从输入 URL 到页面加载完的过程中都发生了什么---优化
    python学习笔记(三)
    python学习笔记(二)
    python学习笔记(一)
    公交wifi运营平台分析
    testNG小试牛刀
    maven小项目注册服务(三)--web模块
    用maven进行测试
  • 原文地址:https://www.cnblogs.com/yeran/p/11288171.html
Copyright © 2011-2022 走看看