最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch中常用的乘法運(yùn)算及相關(guān)的運(yùn)算符(@和*)

 更新時(shí)間:2022年01月07日 11:43:46   作者:諸神黃昏的幸存者  
pytorch是深度學(xué)習(xí)框架,而深度學(xué)習(xí)其實(shí)本質(zhì)就是一大堆矩陣乘法,最后用來模擬一個(gè)高維擬合函數(shù),下面這篇文章主要給大家介紹了關(guān)于pytorch中常用的乘法運(yùn)算及相關(guān)的運(yùn)算符(@和*)的相關(guān)資料,需要的朋友可以參考下

前言

這里總結(jié)一下pytorch常用的乘法運(yùn)算以及相關(guān)的運(yùn)算符(@、*)。

總結(jié)放前面:

torch.mm : 用于兩個(gè)矩陣(不包括向量)的乘法。如維度為(l,m)和(m,n)相乘

torch.bmm : 用于帶batch的三維向量的乘法。如維度為(b,l,m)和(b,m,n)相乘

torch.mul : 用于兩個(gè)同維度矩陣的逐像素點(diǎn)相乘(點(diǎn)乘)。如維度為(l,m)和(l,m)相乘

torch.mv : 用于矩陣和向量之間的乘法(矩陣在前,向量在后)。如維度為(l,m)和(m)相乘,結(jié)果的維度為(l)。

torch.matmul : 用于兩個(gè)張量(后兩維滿足矩陣乘法的維度)相乘或者是矩陣與向量間的乘法,因?yàn)槠渚哂袕V播機(jī)制(broadcasting,自動(dòng)補(bǔ)充維度)。如維度為(b,l,m)和(b,m,n);(l,m)和(b,m,n);(b,c,l,m)和(b,c,m,n);(l,m)和(m)相乘等?!酒渥饔冒瑃orch.mm、torch.bmm和torch.mv】

@運(yùn)算符 : 其作用類似于torch.matmul。

*運(yùn)算符 : 其作用類似于torch.mul。

1、torch.mm

import torch
a = torch.ones(1, 2)
print(a)
b = torch.ones(2, 3)
print(b)
output = torch.mm(a, b)
print(output)
print(output.size())
"""
tensor([[1., 1.]])
tensor([[1., 1., 1.],
        [1., 1., 1.]])
tensor([[2., 2., 2.]])
torch.Size([1, 3])
"""

2、torch.bmm

a = torch.randn(2, 1, 2)
print(a)
b = torch.randn(2, 2, 3)
print(b)
output = torch.bmm(a, b)
print(output)
print(output.size())
"""
tensor([[[-0.1187,  0.2110]],

        [[ 0.7463, -0.6136]]])
tensor([[[-0.1186,  1.5565,  1.3662],
         [ 1.0199,  2.4644,  1.1630]],

        [[-1.9483, -1.6258, -0.4654],
         [-0.1424,  1.3892,  0.7559]]])
tensor([[[ 0.2293,  0.3352,  0.0832]],

        [[-1.3666, -2.0657, -0.8111]]])
torch.Size([2, 1, 3])
"""

3、torch.mul

a = torch.ones(2, 3) * 2
print(a)
b = torch.randn(2, 3)
print(b)
output = torch.mul(a, b)
print(output)
print(output.size())
"""
tensor([[2., 2., 2.],
        [2., 2., 2.]])
tensor([[-0.1187,  0.2110,  0.7463],
        [-0.6136, -0.1186,  1.5565]])
tensor([[-0.2375,  0.4220,  1.4925],
        [-1.2271, -0.2371,  3.1130]])
torch.Size([2, 3])
"""

4、torch.mv

mat = torch.randn(3, 4)
print(mat)
vec = torch.randn(4)
print(vec)
output = torch.mv(mat, vec)
print(output)
print(output.size())
print(torch.mm(mat, vec.unsqueeze(1)).squeeze(1))
"""
tensor([[-0.1187,  0.2110,  0.7463, -0.6136],
        [-0.1186,  1.5565,  1.3662,  1.0199],
        [ 2.4644,  1.1630, -1.9483, -1.6258]])
tensor([-0.4654, -0.1424,  1.3892,  0.7559])
tensor([ 0.5982,  2.5024, -5.2481])
torch.Size([3])
tensor([ 0.5982,  2.5024, -5.2481])
"""

5、torch.matmul

# 其作用包含torch.mm、torch.bmm和torch.mv。其他類似,不一一舉例。
a = torch.randn(2, 1, 2)
print(a)
b = torch.randn(2, 2, 3)
print(b)
output = torch.bmm(a, b)
print(output)
output1 = torch.matmul(a, b)
print(output1)
print(output1.size())
"""
tensor([[[-0.1187,  0.2110]],

        [[ 0.7463, -0.6136]]])
tensor([[[-0.1186,  1.5565,  1.3662],
         [ 1.0199,  2.4644,  1.1630]],

        [[-1.9483, -1.6258, -0.4654],
         [-0.1424,  1.3892,  0.7559]]])
tensor([[[ 0.2293,  0.3352,  0.0832]],

        [[-1.3666, -2.0657, -0.8111]]])
tensor([[[ 0.2293,  0.3352,  0.0832]],

        [[-1.3666, -2.0657, -0.8111]]])
torch.Size([2, 1, 3])
"""
# 維度為(b,l,m)和(b,m,n);(l,m)和(b,m,n);(b,c,l,m)和(b,c,m,n);(l,m)和(m)等
a = torch.randn(2, 3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
a = torch.randn(3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
a = torch.randn(2, 3, 3, 4)
b = torch.randn(2, 3, 4, 5)
print(torch.matmul(a, b).size())
a = torch.randn(2, 3)
b = torch.randn(3)
print(torch.matmul(a, b).size())
"""
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 3, 5])
torch.Size([2])
"""

6、@運(yùn)算符

# @運(yùn)算符:其作用類似于torch.matmul
a = torch.randn(2, 3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
print((a @ b).size())
a = torch.randn(3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
print((a @ b).size())
a = torch.randn(2, 3, 3, 4)
b = torch.randn(2, 3, 4, 5)
print(torch.matmul(a, b).size())
print((a @ b).size())
a = torch.randn(2, 3)
b = torch.randn(3)
print(torch.matmul(a, b).size())
print((a @ b).size())
"""
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 3, 5])
torch.Size([2, 3, 3, 5])
torch.Size([2])
torch.Size([2])
"""

7、*運(yùn)算符

# *運(yùn)算符:其作用類似于torch.mul
a = torch.ones(2, 3) * 2
print(a)
b = torch.ones(2, 3) * 3
print(b)
output = torch.mul(a, b)
print(output)
print(output.size())
output1 = a * b
print(output1)
print(output1.size())
"""
tensor([[2., 2., 2.],
        [2., 2., 2.]])
tensor([[3., 3., 3.],
        [3., 3., 3.]])
tensor([[6., 6., 6.],
        [6., 6., 6.]])
torch.Size([2, 3])
tensor([[6., 6., 6.],
        [6., 6., 6.]])
torch.Size([2, 3])
"""

附:二維矩陣乘法

神經(jīng)網(wǎng)絡(luò)中包含大量的 2D 張量矩陣乘法運(yùn)算,而使用 torch.matmul 函數(shù)比較復(fù)雜,因此 PyTorch 提供了更為簡(jiǎn)單方便的 torch.mm(input, other, out = None) 函數(shù)。下表是 torch.matmul 函數(shù)和 torch.mm 函數(shù)的簡(jiǎn)單對(duì)比。

torch.matmul 函數(shù)支持廣播,主要指的是當(dāng)參與矩陣乘積運(yùn)算的兩個(gè)張量中其中有一個(gè)是 1D 張量,torch.matmul 函數(shù)會(huì)將其廣播成 2D 張量參與運(yùn)算,最后將廣播添加的維度刪除作為最終 torch.matmul 函數(shù)的返回結(jié)果。torch.mm 函數(shù)不支持廣播,相對(duì)應(yīng)的輸入的兩個(gè)張量必須為 2D。

import torch

input = torch.tensor([[1., 2.], [3., 4.]])
other = torch.tensor([[5., 6., 7.], [8., 9., 10.]])

result = torch.mm(input, other)
print(result)
# tensor([[21., 24., 27.],
#         [47., 54., 61.]])

總結(jié)

到此這篇關(guān)于pytorch中常用的乘法運(yùn)算及相關(guān)的運(yùn)算符(@和*)的文章就介紹到這了,更多相關(guān)pytorch常用乘法運(yùn)算及運(yùn)算符內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python刪除文件夾下相同文件和無法打開的圖片

    python刪除文件夾下相同文件和無法打開的圖片

    這篇文章主要為大家詳細(xì)介紹了python刪除文件夾下相同文件和無法打開的圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • python中split方法用法分析

    python中split方法用法分析

    這篇文章主要介紹了python中split方法用法,實(shí)例分析了split方法的功能及相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Python 使用PIL中的resize進(jìn)行縮放的實(shí)例講解

    Python 使用PIL中的resize進(jìn)行縮放的實(shí)例講解

    今天小編就為大家分享一篇Python 使用PIL中的resize進(jìn)行縮放的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 利用Python實(shí)現(xiàn)Excel的文件間的數(shù)據(jù)匹配功能

    利用Python實(shí)現(xiàn)Excel的文件間的數(shù)據(jù)匹配功能

    這篇文章主要介紹了利用Python實(shí)現(xiàn)Excel的文件間的數(shù)據(jù)匹配,本文通過一個(gè)函數(shù)實(shí)現(xiàn)此功能,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 手把手教會(huì)你雙目攝像頭Matlab參數(shù)定標(biāo)

    手把手教會(huì)你雙目攝像頭Matlab參數(shù)定標(biāo)

    雙目標(biāo)定是立體視覺系統(tǒng)中的一個(gè)關(guān)鍵步驟,下面這篇文章主要給大家介紹了關(guān)于雙目攝像頭Matlab參數(shù)定標(biāo)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • Python異常?ValueError的問題

    Python異常?ValueError的問題

    這篇文章主要介紹了Python異常?ValueError的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python實(shí)現(xiàn)自動(dòng)清理文件夾舊文件

    python實(shí)現(xiàn)自動(dòng)清理文件夾舊文件

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)自動(dòng)清理文件夾舊文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • django批量導(dǎo)入xml數(shù)據(jù)

    django批量導(dǎo)入xml數(shù)據(jù)

    從網(wǎng)上下載的一些數(shù)據(jù),excel表格,xml文件,txt文件等有時(shí)候我們想把它導(dǎo)入數(shù)據(jù)庫,應(yīng)該如何操作呢?下面我們就來詳細(xì)討論下。
    2016-10-10
  • python 中的命名空間,你真的了解嗎?

    python 中的命名空間,你真的了解嗎?

    這篇文章主要介紹了python 中命名空間的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • 對(duì)json字符串與python字符串的不同之處詳解

    對(duì)json字符串與python字符串的不同之處詳解

    今天小編就為大家分享一篇對(duì)json字符串與python字符串的不同之處詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評(píng)論

莲花县| 抚宁县| 巍山| 台南市| 安西县| 常熟市| 历史| 佛学| 托里县| 万安县| 清镇市| 平顶山市| 盘山县| 郴州市| 石渠县| 横山县| 甘肃省| 长阳| 沈丘县| 农安县| 桦川县| 新巴尔虎右旗| 江门市| 南阳市| 北安市| 五家渠市| 尉犁县| 安龙县| 平罗县| 武穴市| 峨眉山市| 长汀县| 黄龙县| 图木舒克市| 大化| 伊川县| 丰城市| 岑巩县| 曲周县| 蒙城县| 永泰县|