日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

pytorch中tensor.mul()和mm()和matmul()

發(fā)布時間:2023/11/27 生活经验 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytorch中tensor.mul()和mm()和matmul() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

tensor.mul

  • tensor.mul和tensor * tensor 都是將矩陣的對應位置的元素相乘,因此要求維度相同,點乘
  • torch.mul(input, other, *, out=None) → Tensor
    參數(shù):
    input (Tensor) – the input tensor.
    other (Tensor or Number)

torch.mul(input, other)是將input和other的對應位相乘,other可以是張量,可以是數(shù)字。
torch.mul(input, other)中input和bother維度相等,但是,對應維度上的數(shù)字可以不同,可以用利用廣播機制擴展到相同的形狀,再進行點乘操作。

tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)
tensor([[1., 0., 1., 1.],[1., 0., 1., 1.],[1., 0., 1., 1.],[1., 0., 1., 1.]])

點乘示例:

print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n")
print(f"tensor * tensor \n {tensor * tensor} \n")
print(f"torch.mul(tensor,100) \n {torch.mul(tensor,10)} \n")
print(f"torch.mul(tensor*2,tensor) \n {torch.mul(tensor*2,tensor)} \n")

輸出:

tensor.mul(tensor) tensor([[1., 0., 1., 1.],[1., 0., 1., 1.],[1., 0., 1., 1.],[1., 0., 1., 1.]]) tensor * tensor tensor([[1., 0., 1., 1.],[1., 0., 1., 1.],[1., 0., 1., 1.],[1., 0., 1., 1.]]) torch.mul(tensor,100) tensor([[10.,  0., 10., 10.],[10.,  0., 10., 10.],[10.,  0., 10., 10.],[10.,  0., 10., 10.]]) torch.mul(tensor*2,tensor) tensor([[2., 0., 2., 2.],[2., 0., 2., 2.],[2., 0., 2., 2.],[2., 0., 2., 2.]]) 

tensor.mm

  • torch.mm(input, mat2, *, out=None) → Tensor
  • 參數(shù):
    input (Tensor) – the first matrix to be matrix multiplied
    mat2 (Tensor) – the second matrix to be matrix multiplied
  • vector1 x vector2 矩陣乘法
  • 如果input是(n×m) 維, mat2 是(m×p) 維, 輸出就是(n×p) 維.
    torch.mm(a, b)是矩陣a和b矩陣相乘,比如a的維度是(3, 4),b的維度是(4, 2),返回的就是(3, 2)的矩陣torch.mm(a, b)針對二維矩陣
print(f"tensor.mm(tensor.T) \n {tensor.mm(tensor.T)} \n")
print(f"torch.mm(tensor,tensor.T) \n {tensor @ tensor.T} \n")
print(f"tensor @ tensor.T \n {tensor @ tensor.T} \n")

輸出:

tensor.mm(tensor.T) tensor([[3., 3., 3., 3.],[3., 3., 3., 3.],[3., 3., 3., 3.],[3., 3., 3., 3.]]) torch.mm(tensor,tensor.T) tensor([[3., 3., 3., 3.],[3., 3., 3., 3.],[3., 3., 3., 3.],[3., 3., 3., 3.]]) tensor @ tensor.T tensor([[3., 3., 3., 3.],[3., 3., 3., 3.],[3., 3., 3., 3.],[3., 3., 3., 3.]]) 

tensor.matmul

  • torch.matmul( input , other , * , out = None ) → Tensor
  • 兩個張量的矩陣乘積。

操作取決于張量的維度,如下所示:

  • 如果兩個張量都是一維的,則返回點積(標量)。
  • 如果兩個參數(shù)都是二維的,則返回矩陣-矩陣乘積。
  • 如果第一個參數(shù)是一維的,而第二個參數(shù)是二維的,則為了矩陣乘法的目的,在其維度前面加上 1。矩陣相乘后,前面的維度被刪除。
  • 如果第一個參數(shù)是二維的,第二個參數(shù)是一維的,則返回矩陣向量乘積。
  • 如果兩個參數(shù)至少是一維的并且至少一個參數(shù)是 N 維的(其中 N > 2),則返回一個批處理矩陣乘法。如果第一個參數(shù)是一維的,為了批量矩陣乘法的目的,在其維度前面加上 1 并在之后刪除。如果第二個參數(shù)是一維的,則為了批處理矩陣倍數(shù)的目的,將 1 附加到其維度并在之后刪除。非矩陣(即批次)維度被廣播(因此必須是可廣播的)。例如,如果input是 ( j×1×n×? )張量并且other是( k×n×? ) 張量,out將是( j×k×n×? ) 張量。

請注意,廣播邏輯在確定輸入是否可廣播時僅查看批次維度,而不是矩陣維度。例如,如果input是 ( j×1×n×m)張量并且other是( k×m×p ) 張量,即使最后兩個維度(即矩陣維度)不同,這些輸入也可用于廣播。out將是一個( j×k×n×p ) 張量。

  • 此運算符支持TensorFloat32。
# vector x vector
tensor1 = torch.randn(3)
tensor2 = torch.randn(3)
torch.matmul(tensor1, tensor2).size()
# # torch.Size([])# matrix x vector
tensor1 = torch.randn(3, 4)
tensor2 = torch.randn(4)
torch.matmul(tensor1, tensor2).size()
# # torch.Size([3])# vector x matrix
tensor1 = torch.randn(4) # 1*4
tensor2 = torch.randn(4, 3)
print(torch.matmul(tensor1, tensor2).size())
# # torch.Size([3])# batched matrix x broadcasted vector
tensor1 = torch.randn(10, 3, 4) # 30*4
tensor2 = torch.randn(4) # 4*1
torch.matmul(tensor1, tensor2).size()
# # torch.Size([10, 3])# batched matrix x batched matrix
# 將b的第0維broadcast成10提出來,后兩維做矩陣乘法即可。
tensor1 = torch.randn(10, 3, 4) # 10, 3*4
tensor2 = torch.randn(10, 4, 5) # 10, 4*5
torch.matmul(tensor1, tensor2).size()
# # torch.Size([10, 3, 5])# batched matrix x broadcasted matrix
tensor1 = torch.randn(10, 3, 4) # 30*4
tensor2 = torch.randn(4, 5) # 4*5
torch.matmul(tensor1, tensor2).size()
# # torch.Size([10, 3, 5])

總結(jié)

以上是生活随笔為你收集整理的pytorch中tensor.mul()和mm()和matmul()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。