理解torch.gather

先上案例:

>>> a = torch.Tensor([[1,2,3],[4,5,6]])
>>> a
tensor([[1., 2., 3.],
        [4., 5., 6.]])
>>> torch.gather(a,dim=0,torch.LongTensor([[0,1,0],[1,0,0]]))
tensor([[1., 5., 3.],
        [4., 2., 3.]])
>>> torch.gather(a,dim=1,torch.LongTensor([[2,0,1],[1,2,0]]))
tensor([[3., 1., 2.],
        [5., 6., 4.]])

a 爲被索引的Tensor。
torch.LongTensor([[0,1,0],[1,0,0]] 爲索引。
dim = 0 : 固定索引的列號,然後確認行號。即,索引第一維度。
dim = 1 : 固定索引的行號,然後確認列號。即,索引第二維度。

如下圖所示:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章