解析Pytorch中的torch.gather()函數(shù)
參數(shù)說明
以官方說明為例,gather()函數(shù)需要三個參數(shù),輸入input,維度dim,以及索引index
input必須為Tensor類型
dim為int類型,代表從哪個維度進(jìn)行索引
index為LongTensor類型
舉例說明
input=torch.tensor([[1,2,3],[4,5,6]]) #作為輸入 index1=torch.tensor([[0,1,1],[0,1,1]]) #作為索引矩陣 # dim=0時,按列進(jìn)行索引 print (torch.gather(input,dim=0,index=index1)) # dim=1時,按行進(jìn)行索引 print (torch.gather(input,dim=1,index=index1))
結(jié)果如下圖所示:
# 按列進(jìn)行索引 tensor([[1, 5, 6], [4, 2, 6]]) # 按行進(jìn)行索引 tensor([[1, 2, 2], [5, 4, 5]])
畫圖說明
官方文檔
def gather(self, input, dim, index, *args, **kwargs): For a 3-D tensor the output is specified by:: out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0 out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1 out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2 Args: input (Tensor): the source tensor dim (int): the axis along which to index index (LongTensor): the indices of elements to gather Example:: >>> t = torch.tensor([[1, 2], [3, 4]]) >>> torch.gather(t, 1, torch.tensor([[0, 0], [1, 0]])) tensor([[ 1, 1], [ 4, 3]])
到此這篇關(guān)于Pytorch中的torch.gather()函數(shù)的文章就介紹到這了,更多相關(guān)Pytorch torch.gather()函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sentry的安裝、配置、使用教程(Sentry日志手機(jī)系統(tǒng))
Sentry?是一個實(shí)時事件日志記錄和聚合平臺,由于ExceptionLess官方提供的客戶端只有.Net/.NetCore平臺和js的,本文繼續(xù)介紹另一個日志收集系統(tǒng)Sentry,感興趣的朋友一起看看吧2022-07-07Pycharm+Python工程,引用子模塊的實(shí)現(xiàn)
這篇文章主要介紹了Pycharm+Python工程,引用子模塊的實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python獲取當(dāng)前時間日期的實(shí)現(xiàn)示例
本文主要介紹了Python獲取當(dāng)前時間日期,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python中的?sorted()函數(shù)和sort()方法區(qū)別
這篇文章主要介紹了python中的?sorted()函數(shù)和sort()方法,首先看sort()方法,sort方法只能對列表進(jìn)行操作,而sorted可用于所有的可迭代對象。具體內(nèi)容需要的小伙伴可以參考下面章節(jié)2022-02-02教你怎么用Python處理excel實(shí)現(xiàn)自動化辦公
這篇文章主要介紹了教你怎么用Python處理excel實(shí)現(xiàn)自動化辦公,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04