我们先来看看公式的定义
我的理解如下
导入路径#首先,在out和索引都是二维数组的情况下# out [ I ] [ j ]=tensor [ index [ I ] [ j ] [ j ] # dim=0# out [ I ] [ j ]=tensor 0] ) (index=0#10print ) torch.Gatter ) ) ) ) ) ) ) )在这种情况下,dim=1# )输出1#4)输出结果的size=index .=tensor [ [0]索引[0] [0]==tensor [0] [0]==1# out [0] [1]=tensor [ [0]索引[1]。==tensor [1] [1]==4# out [1] [1]=tensor [ [1] [1]==tensor [1] [0]==3#例2DIM=1y 0.5 1 ) ) 0.1 # 0.2 # y [0] [0]=y _ hat [ [0] [ y [0] ]==y _ hat [0]==0.1 # y [1] [0]=y例3d im=0t=tot (索引) )在这种情况下,dim=1# )输出1 )3)2)索引[0]=0#索引[0] )1)=0#索引[1]=1=