参考:https://zhuanlan.zhihu.com/p/352877584本期code:https://github.com/chunhuizhang/bilibili_vlogs/blob/master/learn_torch/basics/torch.gather.ipynb, 视频播放量 5165、弹幕量 6、点赞数 148、投硬币枚数 71、收藏人数 181、转发人数 17, 视频作者 五道口纳什,
我们可以通过索引来取得里面的每一个值,比如1对应s[0][0][0]。 接下来我们分别设置参数dim=0,1,2来看看结果。 torch.gather(source, dim, index): source:需要操作的张量) dim:维度 index:索引 dim=0 s1=torch.gather(s,0,torch.tensor([[[0,0,0,1],[0,1,1,1],[0,1,1,1]]]))tensor([[...
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 out[i][j][k]=input...
torch.gather的使用及理解 结论:使用方法 # gather,沿dim指定的轴收集值。 y_hat.gather(1, y.view(-1, 1))# y.view(-1, 1)会变成一列,y_hat的取y作为的索引的值 分步理解:先创建一个2*3的tensor >>y_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]]) tensor([[0.1000, 0.3000...
index = torch.tensor([[2, 1, 0]]) tensor_1 = tensor_0.gather(dim=1, index) print(tensor_1) (1) output.shape = index.shape # 确定最后输出的output的shape必须与index的相同,这里是13的tensor,那么output必须也是13的tensor,先把壳打起来torch.tensor([[?,?,?]]) ...
gather函数就是通过length矩阵也就是我上述的矩阵来进行取值的。例如X20就代表了input的第2行第0列的2.0。 因为dim=0 代表的是横向,按行取值。 即length矩阵中的数的值代表的是行数,数的位置代表的列数,比如length矩阵中的第三行第三列(从0数起)的数 0,其值是0,代表在input中所取的数是第0行,位置...
gather函数用于从张量中提取元素。要使用gather函数,输入与索引必须具有相同的维度。例如,如果输入数据为二维,索引也应为二维,尽管它们的形状可以不同。函数输出与索引相同。以二维张量为例,创建一个索引。索引同样应为二维。当dim设置为0时,gather函数操作相当于在行上进行选择。假设索引值为[0, 2, ...
Torch.gather 该函数的作用为:收集指定索引位置的值。 先将函数原型写出: torch.gather(input, dim, index, out=None) → Tensor 参数: input (Tensor) – 源张量 dim (int) – 索引的轴 index (LongTensor) – 聚合元素的下标 out (Tensor, optional) – 目标张量...
torch.gather()《动⼿学Pytorch》:代码: y_hat =torch.tensor([[0.1,0.3,0.6],[0.3.0.2.0.5]])y = torch.LongTensor([0,2]) y_hat.gather(1,y.view(-1,1))输出: tensor([[0.1000,[0.5000]]])理解: gather(dim,index) 其中,dim指定索引维度,在上例中dim=1...
torch.gather(t,1,torchLongTensor([[0,0],[1,0]])) 1,1 4,3 可以看出gather的作用是根据索引返回该项元素,首先先输入一个Tensor 然后根据dim进行判断是是行的还是列的,当dim=0 时候竖行查找,当dim=1的时候是横向查找 上题中,dim=1,那么索引就是列号。index的大小就是输出的大小,比如index是[1,0...