Pytorch 中 matmul 广播
生活随笔
收集整理的這篇文章主要介紹了
Pytorch 中 matmul 广播
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Pytorch 中 matmul 廣播方式
一、兩個(gè) 1 維,向量?jī)?nèi)積
a = torch.ones(3) b = torch.ones(3) print(torch.matmul(a,b)) # tensor(3.)二、兩個(gè) 2 維,矩陣相乘
a = torch.ones(3,4) b= torch.ones(4,3) print(torch.matmul(a,b)) # tensor([[4., 4., 4.], # [4., 4., 4.], # [4., 4., 4.]])三、一個(gè) 1 維,二個(gè) 2 維,矩陣和向量相乘
注意:相靠近的那個(gè)維數(shù)要相同,比如(7)和(7,8,5),又比如(7,8,5)和(5)
a = torch.ones(3) b= torch.ones(3,4) print(torch.matmul(a,b)) # tensor([3., 3., 3., 3.])a = torch.ones(3,4) b = torch.ones(4) print(torch.matmul(a,b)) # tensor([4., 4., 4.])四、高維情況
注意:兩個(gè)都高于 2 維,那么除掉最后兩個(gè)維度外(最后兩個(gè)維度滿足矩陣乘法,即(m,k)*(k,n)),剩下的滿足廣播機(jī)制
a=torch.ones(5,3,4,1) b=torch.ones( 3,1,1) print(torch.matmul(a,b))總結(jié)
以上是生活随笔為你收集整理的Pytorch 中 matmul 广播的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JavaWeb】XML和Jsoup的相
- 下一篇: 中文幽默语料库构建与计算项目(幽默等级识