本文簡要介紹python語言中 torch.matmul
的用法。
用法:
torch.matmul(input, other, *, out=None) → Tensor
兩個張量的矩陣乘積。
行為取決於張量的維度,如下所示:
如果兩個張量都是一維的,則返回點積(標量)。
如果兩個參數都是二維的,則返回 matrix-matrix 乘積。
如果第一個參數是一維的,第二個參數是二維的,為了矩陣乘法的目的,在它的維數前麵加上一個 1。在矩陣相乘之後,前置維度被移除。
如果第一個參數是二維的,第二個參數是一維的,則返回 matrix-vector 乘積。
如果兩個參數至少為一維且至少一個參數為 N 維(其中 N > 2),則返回批處理矩陣乘法。如果第一個參數是一維的,則將 1 添加到其維度,以便批量矩陣相乘並在之後刪除。如果第二個參數是一維的,則將 1 附加到其維度以用於批量矩陣倍數並在之後刪除。非矩陣(即批次)維度是廣播的(因此必須是可廣播的)。例如,如果
input
是 張量並且other
是 張量,則out
將是 張量。請注意,廣播邏輯在確定輸入是否可廣播時僅查看批處理維度,而不是矩陣維度。例如,如果
input
是 張量,而other
是 張量,則即使最後兩個維度(即矩陣維度)不同,這些輸入對於廣播也是有效的。out
將是一個 張量。
該運算符支持 TensorFloat32。
注意
此函數的一維點積版本不支持
out
參數。例子:
>>> # 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]) >>> # batched matrix x broadcasted vector >>> tensor1 = torch.randn(10, 3, 4) >>> tensor2 = torch.randn(4) >>> torch.matmul(tensor1, tensor2).size() torch.Size([10, 3]) >>> # batched matrix x batched matrix >>> tensor1 = torch.randn(10, 3, 4) >>> tensor2 = torch.randn(10, 4, 5) >>> torch.matmul(tensor1, tensor2).size() torch.Size([10, 3, 5]) >>> # batched matrix x broadcasted matrix >>> tensor1 = torch.randn(10, 3, 4) >>> tensor2 = torch.randn(4, 5) >>> torch.matmul(tensor1, tensor2).size() torch.Size([10, 3, 5])
相關用法
- Python PyTorch matrix_rank用法及代碼示例
- Python PyTorch matrix_exp用法及代碼示例
- Python PyTorch matrix_power用法及代碼示例
- Python PyTorch matrix_norm用法及代碼示例
- Python PyTorch max用法及代碼示例
- Python PyTorch maximum用法及代碼示例
- Python PyTorch masked_select用法及代碼示例
- Python PyTorch maskrcnn_resnet50_fpn用法及代碼示例
- Python PyTorch make_tensor用法及代碼示例
- Python PyTorch monitored_barrier用法及代碼示例
- Python PyTorch mean用法及代碼示例
- Python PyTorch multinomial用法及代碼示例
- Python PyTorch meshgrid用法及代碼示例
- Python PyTorch mm用法及代碼示例
- Python PyTorch mv用法及代碼示例
- Python PyTorch min用法及代碼示例
- Python PyTorch msort用法及代碼示例
- Python PyTorch mode用法及代碼示例
- Python PyTorch movedim用法及代碼示例
- Python PyTorch minimum用法及代碼示例
- Python PyTorch multi_dot用法及代碼示例
- Python PyTorch mul用法及代碼示例
- Python PyTorch movielens_25m用法及代碼示例
- Python PyTorch multigammaln用法及代碼示例
- Python PyTorch movielens_20m用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.matmul。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。