婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Pytorch中的gather使用方法

Pytorch中的gather使用方法

熱門(mén)標(biāo)簽:河北防封卡電銷(xiāo)卡 電銷(xiāo)機(jī)器人的風(fēng)險(xiǎn) 應(yīng)電話機(jī)器人打電話違法嗎 開(kāi)封語(yǔ)音外呼系統(tǒng)代理商 地圖標(biāo)注線上如何操作 開(kāi)封自動(dòng)外呼系統(tǒng)怎么收費(fèi) 手機(jī)網(wǎng)頁(yè)嵌入地圖標(biāo)注位置 天津電話機(jī)器人公司 400電話辦理哪種

官方說(shuō)明

gather可以對(duì)一個(gè)Tensor進(jìn)行聚合,聲明為:torch.gather(input, dim, index, out=None) → Tensor

一般來(lái)說(shuō)有三個(gè)參數(shù):輸入的變量input、指定在某一維上聚合的dim、聚合的使用的索引index,輸出為T(mén)ensor類(lèi)型的結(jié)果(index必須為L(zhǎng)ongTensor類(lèi)型)。

#參數(shù)介紹:
input (Tensor) – The source tensor
dim (int) – The axis along which to index
index (LongTensor) – The indices of elements to gather
out (Tensor, optional) – Destination tensor
#當(dāng)輸入為三維時(shí)的計(jì)算過(guò)程:
out[i][j][k] = input[index[i][j][k]][j][k]  # dim=0
out[i][j][k] = input[i][index[i][j][k]][k]  # dim=1
out[i][j][k] = input[i][j][index[i][j][k]]  # dim=2
#樣例:
t = torch.Tensor([[1,2],[3,4]])
torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
#    1  1
#    4  3
#[torch.FloatTensor of size 2x2]

實(shí)驗(yàn)

用下面的代碼在二維上做測(cè)試,以便更好地理解

t = torch.Tensor([[1,2,3],[4,5,6]])
index_a = torch.LongTensor([[0,0],[0,1]])
index_b = torch.LongTensor([[0,1,1],[1,0,0]])
print(t)
print(torch.gather(t,dim=1,index=index_a))
print(torch.gather(t,dim=0,index=index_b))

輸出為:

>>tensor([[1., 2., 3.],
        [4., 5., 6.]])
>>tensor([[1., 1.],
        [4., 5.]])
>>tensor([[1., 5., 6.],
        [4., 2., 3.]])

由于官網(wǎng)給的計(jì)算過(guò)程不太直觀,下面給出較為直觀的解釋?zhuān)?/h2>

對(duì)于index_a,dim為1表示在第二個(gè)維度上進(jìn)行聚合,索引為列號(hào),[[0,0],[0,1]]表示結(jié)果的第一行取原數(shù)組第一行列號(hào)為[0,0]的數(shù),也就是[1,1],結(jié)果的第二行取原數(shù)組第二行列號(hào)為[0,1]的數(shù),也就是[4,5],這樣就得到了輸出的結(jié)果[[1,1],[4,5]]。

對(duì)于index_b,dim為0表示在第一個(gè)維度上進(jìn)行聚合,索引為行號(hào),[[0,1,1],[1,0,0]]表示結(jié)果的第一行第d(d=0,1,2)列取原數(shù)組第d列行號(hào)為[0,1,1]的數(shù),也就是[1,5,6],類(lèi)似的,結(jié)果的第二行第d列取原數(shù)組第d列行號(hào)為[1,0,0]的數(shù),也就是[4,2,3],這樣就得到了輸出的結(jié)果[[1,5,6],[4,2,3]]

接下來(lái)以index_a為例直接用官網(wǎng)的式子計(jì)算一遍加深理解:

output[0,0] = input[0,index[0,0]]  #1 = input[0,0]
output[0,1] = input[0,index[0,1]]  #1 = input[0,0]
output[1,0] = input[1,index[1,0]]  #4 = input[1,0]
output[1,1] = input[1,index[1,1]]  #5 = input[1,1]

以下兩種寫(xiě)法得到的結(jié)果是一樣的:

r1 = torch.gather(t,dim=1,index=index_a)

r2 = t.gather(1,index_a)

補(bǔ)充:Pytorch中的torch.gather函數(shù)的個(gè)人理解

最近在學(xué)習(xí)pytorch時(shí)遇到gather函數(shù),開(kāi)始沒(méi)怎么理解,后來(lái)查閱網(wǎng)上相關(guān)資料后大概明白了原理。

gather()函數(shù)

在pytorch中,gather()函數(shù)的作用是將數(shù)據(jù)從input中按index提出,我們看gather函數(shù)的的官方文檔說(shuō)明如下:

torch.gather(input, dim, index, out=None) → Tensor
    Gathers values along an axis specified by dim.
    For a 3-D tensor the output is specified by:

    out[i][j][k] = input[index[i][j][k]][j][k]  # dim=0
    out[i][j][k] = input[i][index[i][j][k]][k]  # dim=1
    out[i][j][k] = input[i][j][index[i][j][k]]  # dim=2

    Parameters: 

        input (Tensor) – The source tensor
        dim (int) – The axis along which to index
        index (LongTensor) – The indices of elements to gather
        out (Tensor, optional) – Destination tensor

    Example:

    >>> t = torch.Tensor([[1,2],[3,4]])
    >>> torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
     1  1
     4  3
    [torch.FloatTensor of size 2x2]

可以看出,在gather函數(shù)中我們用到的主要有三個(gè)參數(shù):

1)input:輸入

2)dim:維度,常用的為0和1

3)index:索引位置

貼一段代碼舉例說(shuō)明:

a=t.arange(0,16).view(4,4)
print(a)

index_1=t.LongTensor([[3,2,1,0]])
b=a.gather(0,index_1)
print(b)

index_2=t.LongTensor([[0,1,2,3]]).t()#tensor轉(zhuǎn)置操作:(a)T=a.t()
c=a.gather(1,index_2)
print(c)

輸出如下:

tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]])
       
tensor([[12,  9,  6,  3]])

tensor([[ 0],
        [ 5],
        [10],
        [15]])

在gather中,我們是通過(guò)index對(duì)input進(jìn)行索引把對(duì)應(yīng)的數(shù)據(jù)提取出來(lái)的,而dim決定了索引的方式。

在上面的例子中,a是一個(gè)4×4矩陣:

1)當(dāng)維度dim=0,索引index_1為[3,2,1,0]時(shí),此時(shí)可將a看成1×4的矩陣,通過(guò)index_1對(duì)a每列進(jìn)行行索引:第一列第四行元素為12,第二列第三行元素為9,第三列第二行元素為6,第四列第一行元素為3,即b=[12,9,6,3];

2)當(dāng)維度dim=1,索引index_2為[0,1,2,3]T時(shí),此時(shí)可將a看成4×1的矩陣,通過(guò)index_1對(duì)a每行進(jìn)行列索引:第一行第一列元素為0,第二行第二列元素為5,第三行第三列元素為10,第四行第四列元素為15,即c=[0,5,10,15]T;

總結(jié)

gather函數(shù)在提取數(shù)據(jù)時(shí)主要靠dim和index這兩個(gè)參數(shù),dim=1時(shí)將input看為n×1階矩陣,index看為k×1階矩陣,取index每行元素對(duì)input中每行進(jìn)行列索引(如:index某行為[1,3,0],對(duì)應(yīng)的input行元素為[9,8,7,6],提取后的結(jié)果為[8,6,9]);

同理,dim=0時(shí)將input看為1×n階矩陣,index看為1×k階矩陣,取index每列元素對(duì)input中每列進(jìn)行行索引。

gather函數(shù)提取后的矩陣階數(shù)和對(duì)應(yīng)的index階數(shù)相同。

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

您可能感興趣的文章:
  • 使用pytorch時(shí)所遇到的一些問(wèn)題總結(jié)
  • Pytorch高階OP操作where,gather原理
  • 淺談Pytorch中的torch.gather函數(shù)的含義
  • Pytorch深度學(xué)習(xí)gather一些使用問(wèn)題解決方案

標(biāo)簽:駐馬店 六盤(pán)水 江蘇 常州 宿遷 蘭州 山東 成都

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Pytorch中的gather使用方法》,本文關(guān)鍵詞  Pytorch,中的,gather,使用方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Pytorch中的gather使用方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Pytorch中的gather使用方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 乳源| 博客| 金山区| 蕲春县| 崇义县| 常山县| 克拉玛依市| 富锦市| 得荣县| 福海县| 镇康县| 甘肃省| 杂多县| 泌阳县| 夏邑县| 高台县| 什邡市| 安溪县| 化德县| 苍梧县| 龙井市| 绩溪县| 甘谷县| 社会| 卢湾区| 綦江县| 峨边| 连云港市| 九寨沟县| 宁城县| 拜城县| 盐山县| 利辛县| 昌乐县| 钟祥市| 梧州市| 达日县| 广丰县| 库尔勒市| 贡嘎县| 霍州市|