詳解torch.Tensor的4種乘法
torch.Tensor有4種常見的乘法:*, torch.mul, torch.mm, torch.matmul. 本文拋磚引玉,簡單敘述一下這4種乘法的區(qū)別,具體使用還是要參照官方文檔。
點(diǎn)乘
a與b做*乘法,原則是如果a與b的size不同,則以某種方式將a或b進(jìn)行復(fù)制,使得復(fù)制后的a和b的size相同,然后再將a和b做element-wise的乘法。
下面以*標(biāo)量和*一維向量為例展示上述過程。
* 標(biāo)量
Tensor與標(biāo)量k做*乘法的結(jié)果是Tensor的每個元素乘以k(相當(dāng)于把k復(fù)制成與lhs大小相同,元素全為k的Tensor).
>>> a = torch.ones(3,4) >>> a tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) >>> a * 2 tensor([[2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.]])
* 一維向量
Tensor與行向量做*乘法的結(jié)果是每列乘以行向量對應(yīng)列的值(相當(dāng)于把行向量的行復(fù)制,成為與lhs維度相同的Tensor). 注意此時要求Tensor的列數(shù)與行向量的列數(shù)相等。
>>> a = torch.ones(3,4) >>> a tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) >>> b = torch.Tensor([1,2,3,4]) >>> b tensor([1., 2., 3., 4.]) >>> a * b tensor([[1., 2., 3., 4.], [1., 2., 3., 4.], [1., 2., 3., 4.]])
Tensor與列向量做*乘法的結(jié)果是每行乘以列向量對應(yīng)行的值(相當(dāng)于把列向量的列復(fù)制,成為與lhs維度相同的Tensor). 注意此時要求Tensor的行數(shù)與列向量的行數(shù)相等。
>>> a = torch.ones(3,4) >>> a tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) >>> b = torch.Tensor([1,2,3]).reshape((3,1)) >>> b tensor([[1.], [2.], [3.]]) >>> a * b tensor([[1., 1., 1., 1.], [2., 2., 2., 2.], [3., 3., 3., 3.]])
* 矩陣
經(jīng)Arsmart在評論區(qū)提醒,增補(bǔ)一個矩陣 * 矩陣的例子,感謝Arsmart的熱心評論!
如果兩個二維矩陣A與B做點(diǎn)積A * B,則要求A與B的維度完全相同,即A的行數(shù)=B的行數(shù),A的列數(shù)=B的列數(shù)
>>> a = torch.tensor([[1, 2], [2, 3]]) >>> a * a tensor([[1, 4], [4, 9]])
broadcast
點(diǎn)積是broadcast的。broadcast是torch的一個概念,簡單理解就是在一定的規(guī)則下允許高維Tensor和低維Tensor之間的運(yùn)算。broadcast的概念稍顯復(fù)雜,在此不做展開,可以參考官方文檔關(guān)于broadcast的介紹. 在torch.matmul里會有關(guān)于broadcast的應(yīng)用的一個簡單的例子。
這里舉一個點(diǎn)積broadcast的例子。在例子中,a是二維Tensor,b是三維Tensor,但是a的維度與b的后兩位相同,那么a和b仍然可以做點(diǎn)積,點(diǎn)積結(jié)果是一個和b維度一樣的三維Tensor,運(yùn)算規(guī)則是:若c = a * b
, 則c[i,*,*] = a * b[i, *, *]
,即沿著b的第0維做二維Tensor點(diǎn)積,或者可以理解為運(yùn)算前將a沿著b的第0維也進(jìn)行了expand操作,即a = a.expand(b.size()); a * b
。
>>> a = torch.tensor([[1, 2], [2, 3]]) >>> b = torch.tensor([[[1,2],[2,3]],[[-1,-2],[-2,-3]]]) >>> a * b tensor([[[ 1, 4], [ 4, 9]], [[-1, -4], [-4, -9]]]) >>> b * a tensor([[[ 1, 4], [ 4, 9]], [[-1, -4], [-4, -9]]])
其實,上面提到的二維Tensor點(diǎn)積標(biāo)量、二維Tensor點(diǎn)積行向量,都是發(fā)生在高維向量和低維向量之間的,也可以看作是broadcast.
torch.mul
官方文檔關(guān)于torch.mul的介紹. 用法與*乘法相同,也是element-wise的乘法,也是支持broadcast的。
下面是幾個torch.mul的例子.
乘標(biāo)量
>>> a = torch.ones(3,4) >>> a tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) >>> a * 2 tensor([[2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.]])
乘行向量
>>> a = torch.ones(3,4) >>> a tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) >>> b = torch.Tensor([1,2,3,4]) >>> b tensor([1., 2., 3., 4.]) >>> torch.mul(a, b) tensor([[1., 2., 3., 4.], [1., 2., 3., 4.], [1., 2., 3., 4.]])
乘列向量
>>> a = torch.ones(3,4) >>> a tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) >>> b = torch.Tensor([1,2,3]).reshape((3,1)) >>> b tensor([[1.], [2.], [3.]]) >>> torch.mul(a, b) tensor([[1., 1., 1., 1.], [2., 2., 2., 2.], [3., 3., 3., 3.]])
乘矩陣
例1:二維矩陣 mul 二維矩陣
>>> a = torch.tensor([[1, 2], [2, 3]]) >>> torch.mul(a,a) tensor([[1, 4], [4, 9]])
例2:二維矩陣 mul 三維矩陣(broadcast)
>>> a = torch.tensor([[1, 2], [2, 3]]) >>> b = torch.tensor([[[1,2],[2,3]],[[-1,-2],[-2,-3]]]) >>> torch.mul(a,b) tensor([[[ 1, 4], [ 4, 9]], [[-1, -4], [-4, -9]]])
torch.mm
官方文檔關(guān)于torch.mm的介紹. 數(shù)學(xué)里的矩陣乘法,要求兩個Tensor的維度滿足矩陣乘法的要求.
例子:
>>> a = torch.ones(3,4) >>> b = torch.ones(4,2) >>> torch.mm(a, b) tensor([[4., 4.], [4., 4.], [4., 4.]])
torch.matmul
官方文檔關(guān)于torch.matmul的介紹. torch.mm的broadcast版本.
例子:
>>> a = torch.ones(3,4) >>> b = torch.ones(5,4,2) >>> torch.matmul(a, b) tensor([[[4., 4.], [4., 4.], [4., 4.]], [[4., 4.], [4., 4.], [4., 4.]], [[4., 4.], [4., 4.], [4., 4.]], [[4., 4.], [4., 4.], [4., 4.]], [[4., 4.], [4., 4.], [4., 4.]]])
同樣的a和b,使用torch.mm相乘會報錯
>>> torch.mm(a, b) Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: matrices expected, got 2D, 3D tensors at /pytorch/aten/src/TH/generic/THTensorMath.cpp:2065
到此這篇關(guān)于詳解torch.Tensor的4種乘法的文章就介紹到這了,更多相關(guān)torch.Tensor 乘法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python和anaconda區(qū)別以及先后安裝的問題詳解
Anaconda(開源的Python包管理器)是一個python發(fā)行版,包含了conda、Python等180多個科學(xué)包及其依賴項,下面這篇文章主要給大家介紹了關(guān)于python和anaconda區(qū)別以及先后安裝問題的相關(guān)資料,需要的朋友可以參考下2022-05-05DataFrame 數(shù)據(jù)合并實現(xiàn)(merge,join,concat)
這篇文章主要介紹了DataFrame 數(shù)據(jù)合并實現(xiàn)(merge,join,concat),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06pandas數(shù)據(jù)類型之Series的具體使用
本文主要介紹了pandas數(shù)據(jù)類型之Series的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08