佳木斯湛栽影视文化发展公司

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

Pytorch中的gather使用方法

熱門(mén)標(biāo)簽:呼叫中心市場(chǎng)需求 硅谷的囚徒呼叫中心 百度AI接口 語(yǔ)音系統(tǒng) 客戶服務(wù) Win7旗艦版 企業(yè)做大做強(qiáng) 電話運(yùn)營(yíng)中心

官方說(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類型的結(jié)果(index必須為L(zhǎng)ongTensor類型)。

#參數(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ò)程不太直觀,下面給出較為直觀的解釋:

對(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],類似的,結(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)簽:山西 濟(jì)南 長(zhǎng)沙 海南 喀什 崇左 安康 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Pytorch中的gather使用方法》,本文關(guān)鍵詞  ;如發(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)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    泊头市| 教育| 洪湖市| 玉林市| 仁寿县| 分宜县| 五河县| 常熟市| 赤水市| 临泉县| 融水| 无棣县| 临潭县| 襄汾县| 公主岭市| 宁夏| 平阴县| 武定县| 精河县| 满洲里市| 长泰县| 齐齐哈尔市| 平罗县| 庆城县| 贵州省| 留坝县| 屯昌县| 平塘县| 内黄县| 池州市| 湟源县| 湖口县| 寿阳县| 阳江市| 石景山区| 泊头市| 荔浦县| 杂多县| 伊吾县| 彭州市| 林周县|