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