gather(1, action_batch) 其中Q(S_t),即policy_net(state_batch)为shape=(128, 2)的二维表,动作数为2 而我们通过神经网络输出的对应批量动作为 此时,使用gather()函数即可轻松获取批量状态对应批量动作的Q(S_t,a) 3 总结 从以上典型案例,我们可以归纳出torch.gather()的使用要点 输入index的shape等于输...
pytorch二维三维数据下的gather函数_torch gather 二维数组-CSDN博客blog.csdn.net/qq_45196535/article/details/127512038 gather函数的input与index必须有相同的维度,例如你输入的数据是二维的,那么index也必须是二维的,但他们的shape可以不同,最终输出的结果与输入的index相同。接下来首先通过二维张量举例。 importtor...
gather操作是scatter操作的逆操作,如果说scatter是根据index和src求self(input),那么gather操作是根据self(input)和index求src。具体来说gather操作是根据index指出的索引,沿dim指定的轴收集input的值。 对于一个三维张量来说,gather函数的输出公式为: out[i][j][k]= input[index[i][j][k]][j][k]# if dim...
torch.gather()详解 简介:torch.gather()函数:利用index来索引input特定位置的数值dim = 1表示横向。 一、函数参数 torch.gather(input, dim, index, *, sparse_grad=False, out=None) → Tensor torch.gather()函数:利用index来索引input特定位置的数值 dim = 1表示横向。 对于三维张量,其output是: out[i]...
tensor.gather()的作用就是按照索引取对应的数据出来。之前看图解PyTorch中的torch.gather函数,那个图示看得我有点懵逼,所以自己画了两张图总结了一下规律来理解一下。 首先新建一个3*3的二维矩阵。 import torch t1 = torch.tensor([[1, 2, 3], ...
torch.gather函数是一个非常实用的函数,用于按照给定的索引从输入tensor中检索元素。在三维数据处理中,可以使用torch.gather函数从三维tensor中选择或重排特定的元素。该函数需要三个输入参数:input、dim和index。其中,input是包含要检索元素的原始tensor,dim是要操作的维度,index是一个tensor,包含检索元素的索引。 2.安装...
一、函数参数 torch.gather(input,dim,index,*,sparse_grad=False,out=None) 1. torch.gather()函数:利用index来索引input特定位置的数值 dim = 1表示横向。 对于三维张量,其output是: out[i][j][k]=input[index[i][j][k]][j][k]# if dim == 0 ...
torch.gather(input, dim, index, out=None) → Tensor 参数: input (Tensor) – 源张量 dim (int) – 索引的轴 index (LongTensor) – 聚合元素的下标 out (Tensor, optional) – 目标张量 首先来用自己的语言解释该函数的各个参数。 第一个参数就是自己要收集值的Tensor,不用多解释。
是对于out指定位置上的值,去寻找input里面对应的索引位置,根据是index 官方文档给的例子是: 代码语言:javascript 复制 >>>t=torch.Tensor([[1,2],[3,4]])>>>torch.gather(t,1,torch.LongTensor([[0,0],[1,0]]))1143[torch.FloatTensorofsize 2x2] ...
那么用程序这么获得标签对应的概率呢,这里就可以用gather函数。 myY_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]]) myY = torch.LongTensor([0, 2]) print(myY.view(-1, 1)) print(myY_hat.gather(1, myY.view(-1, 1)))...