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

pytorch?tensor內(nèi)所有元素相乘實(shí)例

 更新時(shí)間:2022年07月16日 16:32:49   作者:某C姓工程師傅  
這篇文章主要介紹了pytorch?tensor內(nèi)所有元素相乘實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

tensor內(nèi)所有元素相乘

a = torch.Tensor([1,2,3])
print(torch.prod(a))

輸出 

tensor(6.)

tensor乘法運(yùn)算匯總與解析

元素一一相乘

該操作又稱作 “哈達(dá)瑪積”, 簡(jiǎn)單來(lái)說(shuō)就是 tensor 元素逐個(gè)相乘。這個(gè)操作,是通過(guò) * 也就是常規(guī)的乘號(hào)操作符定義的操作結(jié)果。torch.mul 是等價(jià)的。

import torch
def element_by_element():
? ??
? ? x = torch.tensor([1, 2, 3])
? ? y = torch.tensor([4, 5, 6])
? ??
? ? return x * y, torch.mul(x, y)
element_by_element()
(tensor([ 4, 10, 18]), tensor([ 4, 10, 18]))

這個(gè)操作是可以 broad cast 的。

def element_by_element_broadcast():
? ??
? ? x = torch.tensor([1, 2, 3])
? ? y = 2
? ??
? ? return x * y
element_by_element_broadcast()
tensor([2, 4, 6])

向量點(diǎn)乘

torch.matmul: If both tensors are 1-dimensional, the dot product (scalar) is returned.

如果都是1維的,返回的就是 dot product 結(jié)果

def vec_dot_product():
? ??
? ? x = torch.tensor([1, 2, 3])
? ? y = torch.tensor([4, 5, 6])
? ??
? ? return torch.matmul(x, y)
vec_dot_product()
tensor(32)

矩陣乘法

torch.matmul: If both arguments are 2-dimensional, the matrix-matrix product is returned.

如果都是2維,那么就是矩陣乘法的結(jié)果返回。與 torch.mm 是等價(jià)的,torch.mm 僅僅能處理的是矩陣乘法。

def matrix_multiple():
? ??
? ? x = torch.tensor([
? ? ? ? [1, 2, 3],
? ? ? ? [4, 5, 6]
? ? ])
? ? y = torch.tensor([
? ? ? ? [7, 8],
? ? ? ? [9, 10],
? ? ? ? [11, 12]
? ? ])
? ??
? ? return torch.matmul(x, y), torch.mm(x, y)
matrix_multiple()
(tensor([[ 58, ?64],
? ? ? ? ?[139, 154]]), tensor([[ 58, ?64],
? ? ? ? ?[139, 154]]))

vector 與 matrix 相乘

torch.matmul: If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.

如果第一個(gè)是 vector, 第二個(gè)是 matrix, 會(huì)在 vector 中增加一個(gè)維度。也就是 vector 變成了 與 matrix 相乘之后,變成 , 在結(jié)果中將 維 再去掉。

def vec_matrix():
? ? x = torch.tensor([1, 2, 3])
? ? y = torch.tensor([
? ? ? ? [7, 8],
? ? ? ? [9, 10],
? ? ? ? [11, 12]
? ? ])
? ??
? ? return torch.matmul(x, y)
vec_matrix()
tensor([58, 64])

matrix 與 vector 相乘

同樣的道理, vector會(huì)被擴(kuò)充一個(gè)維度。

def matrix_vec():
? ? x = torch.tensor([
? ? ? ? [1, 2, 3],
? ? ? ? [4, 5, 6]
? ? ])
? ? y = torch.tensor([
? ? ? ? 7, 8, 9
? ? ])
? ??
? ? return torch.matmul(x, y)
matrix_vec()
tensor([ 50, 122])

帶有batch_size 的 broad cast乘法

def batched_matrix_broadcasted_vector():
? ? x = torch.tensor([
? ? ? ? [
? ? ? ? ? ? [1, 2], [3, 4]
? ? ? ? ],
? ? ? ? [
? ? ? ? ? ? [5, 6], [7, 8]
? ? ? ? ]
? ? ])
? ??
? ? print(f"x shape: {x.size()} \n {x}")
? ? y = torch.tensor([1, 3])
? ??
? ? return torch.matmul(x, y)
batched_matrix_broadcasted_vector()
x shape: torch.Size([2, 2, 2])?
?tensor([[[1, 2],
? ? ? ? ?[3, 4]],
? ? ? ? [[5, 6],
? ? ? ? ?[7, 8]]])
tensor([[ 7, 15],
? ? ? ? [23, 31]])
batched matrix x batched matrix
def batched_matrix_batched_matrix():
? ? x = torch.tensor([
? ? ? ? [
? ? ? ? ? ? [1, 2, 1], [3, 4, 4]
? ? ? ? ],
? ? ? ? [
? ? ? ? ? ? [5, 6, 2], [7, 8, 0]
? ? ? ? ]
? ? ])
? ??
? ? y = torch.tensor([
? ? ? ? [
? ? ? ? ? ? [1, 2],?
? ? ? ? ? ? [3, 4],?
? ? ? ? ? ? [5, 6]
? ? ? ? ],
? ? ? ? [
? ? ? ? ? ? [7, 8],?
? ? ? ? ? ? [9, 10],?
? ? ? ? ? ? [1, 2]
? ? ? ? ]
? ? ])
? ??
? ? print(f"x shape: {x.size()} \n y shape: {y.size()}")
? ? return torch.matmul(x, y)
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")
x shape: torch.Size([2, 2, 3])?
?y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2])?
?tensor([[[ 12, ?16],
? ? ? ? ?[ 35, ?46]],
? ? ? ? [[ 91, 104],
? ? ? ? ?[121, 136]]])

上面的效果與 torch.bmm 是一樣的。matmul 比 bmm 功能更加強(qiáng)大,但是 bmm 的語(yǔ)義非常明確, bmm 處理的只能是 3維的。

def batched_matrix_batched_matrix_bmm():
? ? x = torch.tensor([
? ? ? ? [
? ? ? ? ? ? [1, 2, 1], [3, 4, 4]
? ? ? ? ],
? ? ? ? [
? ? ? ? ? ? [5, 6, 2], [7, 8, 0]
? ? ? ? ]
? ? ])
? ??
? ? y = torch.tensor([
? ? ? ? [
? ? ? ? ? ? [1, 2],?
? ? ? ? ? ? [3, 4],?
? ? ? ? ? ? [5, 6]
? ? ? ? ],
? ? ? ? [
? ? ? ? ? ? [7, 8],?
? ? ? ? ? ? [9, 10],?
? ? ? ? ? ? [1, 2]
? ? ? ? ]
? ? ])
? ??
? ? print(f"x shape: {x.size()} \n y shape: {y.size()}")
? ? return torch.bmm(x, y)
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")
x shape: torch.Size([2, 2, 3])?
?y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2])?
?tensor([[[ 12, ?16],
? ? ? ? ?[ 35, ?46]],
? ? ? ? [[ 91, 104],
? ? ? ? ?[121, 136]]])
tensordot
def tesnordot():
? ? x = torch.tensor([
? ? ? ? [1, 2, 1],?
? ? ? ? [3, 4, 4]])
? ? y = torch.tensor([
? ? ? ? [7, 8],?
? ? ? ? [9, 10],?
? ? ? ? [1, 2]])
? ? print(f"x shape: {x.size()}, y shape: {y.size()}")
? ? return torch.tensordot(x, y, dims=([0], [1]))
tesnordot()
x shape: torch.Size([2, 3]), y shape: torch.Size([3, 2])
tensor([[31, 39, ?7],
? ? ? ? [46, 58, 10],
? ? ? ? [39, 49, ?9]])

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python對(duì)切片命名的實(shí)現(xiàn)方法

    Python對(duì)切片命名的實(shí)現(xiàn)方法

    在本篇文章里我們給大家分享了關(guān)于Python對(duì)切片命名的實(shí)現(xiàn)方法的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。
    2018-10-10
  • Python繪制三角函數(shù)圖(sin\cos\tan)并標(biāo)注特定范圍的例子

    Python繪制三角函數(shù)圖(sin\cos\tan)并標(biāo)注特定范圍的例子

    今天小編就為大家分享一篇Python繪制三角函數(shù)圖(sin\cos\tan)并標(biāo)注特定范圍的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • pandas的相關(guān)系數(shù)與協(xié)方差實(shí)例

    pandas的相關(guān)系數(shù)與協(xié)方差實(shí)例

    今天小編就為大家分享一篇pandas的相關(guān)系數(shù)與協(xié)方差實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python?Fuzzywuzzy庫(kù)基本函數(shù)及模糊字符串匹配應(yīng)用實(shí)戰(zhàn)

    Python?Fuzzywuzzy庫(kù)基本函數(shù)及模糊字符串匹配應(yīng)用實(shí)戰(zhàn)

    fuzzywuzzy?是一個(gè)用于模糊字符串匹配的?Python?庫(kù),它基于編輯距離算法,提供了多個(gè)函數(shù)來(lái)比較字符串之間的相似性,在實(shí)際開(kāi)發(fā)中,字符串匹配是一項(xiàng)常見(jiàn)但具有挑戰(zhàn)性的任務(wù),用戶可能犯拼寫錯(cuò)誤,使用縮寫或者輸入同義詞,因此,我們需要一種方法來(lái)處理這些情況
    2023-12-12
  • python利用urllib和urllib2訪問(wèn)http的GET/POST詳解

    python利用urllib和urllib2訪問(wèn)http的GET/POST詳解

    urllib模塊提供的上層接口,使我們可以像讀取本地文件一樣讀取www和ftp上的數(shù)據(jù)。下面這篇文章主要給大家介紹了關(guān)于python如何利用urllib和urllib2訪問(wèn)http的GET/POST的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-09-09
  • 使用Python的PIL模塊來(lái)進(jìn)行圖片對(duì)比

    使用Python的PIL模塊來(lái)進(jìn)行圖片對(duì)比

    這篇文章主要介紹了使用Python的PIL模塊來(lái)進(jìn)行圖片對(duì)比的方法,搜索引擎最基本的圖片搜索也是利用圖片顏色值的對(duì)比來(lái)實(shí)現(xiàn)的,需要的朋友可以參考下
    2016-02-02
  • Python改變對(duì)象的字符串顯示的方法

    Python改變對(duì)象的字符串顯示的方法

    這篇文章主要介紹了Python改變對(duì)象的字符串顯示的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • python愛(ài)心表白 每天都是浪漫七夕!

    python愛(ài)心表白 每天都是浪漫七夕!

    每天都是浪漫七夕!這篇文章主要為大家詳細(xì)介紹了python愛(ài)心表白,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 零基礎(chǔ)小白多久能學(xué)會(huì)python

    零基礎(chǔ)小白多久能學(xué)會(huì)python

    在本篇文章里小編給大家分享的是一篇關(guān)于零基礎(chǔ)學(xué)python要多久的相關(guān)文章內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。
    2020-06-06
  • 10 分鐘快速入門 Python3的教程

    10 分鐘快速入門 Python3的教程

    這篇文章主要介紹了10 分鐘快速入門 Python3的教程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01

最新評(píng)論

营口市| 星座| 昆明市| 安丘市| 湖北省| 周至县| 吴桥县| 海丰县| 蕉岭县| 施秉县| 常宁市| 台前县| 文登市| 陕西省| 西林县| 昭通市| 德保县| 繁昌县| 宜章县| 兴安县| 方城县| 商丘市| 赤城县| 玉门市| 太康县| 手机| 陕西省| 麻阳| 略阳县| 德州市| 阿图什市| 湄潭县| 白山市| 桃园县| 老河口市| 洪洞县| 华容县| 清流县| 仁寿县| 垣曲县| 天台县|