欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

輸出 

tensor(6.)

tensor乘法運算匯總與解析

元素一一相乘

該操作又稱作 “哈達瑪積”, 簡單來說就是 tensor 元素逐個相乘。這個操作,是通過 * 也就是常規(guī)的乘號操作符定義的操作結(jié)果。torch.mul 是等價的。

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]))

這個操作是可以 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])

向量點乘

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 是等價的,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.

如果第一個是 vector, 第二個是 matrix, 會在 vector 中增加一個維度。也就是 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會被擴充一個維度。

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 功能更加強大,但是 bmm 的語義非常明確, 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]])

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

相關(guān)文章

  • Python定義一個Actor任務(wù)

    Python定義一個Actor任務(wù)

    這篇文章主要介紹了Python定義一個Actor任務(wù),文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • pycharm 使用心得(三)Hello world!

    pycharm 使用心得(三)Hello world!

    作為PyCharm編輯器的起步,我們理所當(dāng)然的先寫一個Hello word,并運行它。(此文獻給對IDE不熟悉的初學(xué)者)
    2014-06-06
  • pytorch中的卷積和池化計算方式詳解

    pytorch中的卷積和池化計算方式詳解

    今天小編就為大家分享一篇pytorch中的卷積和池化計算方式詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python中偏函數(shù)用法示例

    Python中偏函數(shù)用法示例

    這篇文章主要介紹了Python中偏函數(shù)用法,結(jié)合實例形式分析了Python基于functools模塊創(chuàng)建和使用偏函數(shù)的相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2018-06-06
  • python3 Scrapy爬蟲框架ip代理配置的方法

    python3 Scrapy爬蟲框架ip代理配置的方法

    Scrapy是用python實現(xiàn)的一個為了爬取網(wǎng)站數(shù)據(jù),提取結(jié)構(gòu)性數(shù)據(jù)而編寫的應(yīng)用框架。使用Twisted高效異步網(wǎng)絡(luò)框架來處理網(wǎng)絡(luò)通信。這篇文章主要介紹了python3 Scrapy爬蟲框架ip代理配置,需要的朋友可以參考下
    2020-01-01
  • python3 簡單實現(xiàn)組合設(shè)計模式

    python3 簡單實現(xiàn)組合設(shè)計模式

    這篇文章主要介紹了python3 簡單實現(xiàn)組合設(shè)計模式的方法,文中示例代碼非常詳細,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 深入理解python虛擬機生成器停止背后原理

    深入理解python虛擬機生成器停止背后原理

    這篇文章主要介紹了python虛擬機生成器停止背后原理深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • python正則表達式查找和替換內(nèi)容的實例詳解

    python正則表達式查找和替換內(nèi)容的實例詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于python正則表達式查找和替換內(nèi)容的實例詳解內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2021-10-10
  • Python 下載及安裝詳細步驟

    Python 下載及安裝詳細步驟

    這篇文章主要介紹了載及安裝Python詳細步驟,安裝python分三個步驟,具體安裝方法本文給大家介紹的非常詳細,需要的朋友可以參考下
    2019-11-11
  • Python運算符的使用簡單介紹

    Python運算符的使用簡單介紹

    這篇文章主要介紹了Python運算符的使用簡單介紹,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08

最新評論