首页 Paddle框架 帖子详情
请教一下fluid.layers.gather的用法
收藏
快速回复
Paddle框架 问答深度学习 1085 4
请教一下fluid.layers.gather的用法
收藏
快速回复
Paddle框架 问答深度学习 1085 4

我现在得到了一个(B, N, K)大小的索引index,想从(B, N, C)的数据D中取出索引指向的那一行的向量。
请问该怎么使用fluid.layers.gather这个函数进行提取?
在torch中直接D(index, :)就可以,在tensorflow中使用tf.gather(D, index)就可以。
B的维度一直都是-1。目前尝试过把index进行reshape,变成(B, ) , (B, NK)和(B, 1, NK)的大小,都没有办法进行索引,请问我要怎么做才能正确进行操作。
或者有其他的函数可以这么做吗?
谢谢

0
收藏
回复
全部评论(4)
时间顺序
AIStudio790019
#2 回复于2019-11

import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[-1, 5], dtype='float32')
index = fluid.layers.data(name='index', shape=[-1, 1], dtype='int32')
output = fluid.layers.gather(x, index)
你好,可以参考https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/gather_cn.html#gather

0
回复
AIStudio791309
#3 回复于2019-11

import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[-1, 5], dtype='float32')
index = fluid.layers.data(name='index', shape=[-1, 1], dtype='int32')
output = fluid.layers.gather(x, index)
你好,可以参考https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/gather_cn.html#gather

这个示例我看过。在gather计算过程中会忽略掉-1的维度吗?gather中的index只支持一维的矩阵,怎么算是一维的矩阵呢?(-1, N)算一维还是(-1, )算是一维,还是只有(-1, 1)算是一维的?那么具体的(B, N, K)大小的index矩阵是要变成(-1, BNK)来进行索引呢,还是需要变成(B, N*K)的大小来进行索引呢?具体应该如何操作可以讲一下吗,这个例子太过于模糊了,不太看得懂······谢谢

0
回复
AIStudio790019
#4 回复于2019-11

(-1,1)代表一维
如果是对单个维度的B * N * K个index进行索引,index矩阵要变成(B * N * K,1)的shape
如果是想要对多个维度进行索引,可以使用gather_nd
https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/gather_nd_cn.html#gather-nd

0
回复
AIStudio791309
#5 回复于2019-11

(-1,1)代表一维
如果是对单个维度的B * N * K个index进行索引,index矩阵要变成(B * N * K,1)的shape
如果是想要对多个维度进行索引,可以使用gather_nd
https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/gather_nd_cn.html#gather-nd

了解了,谢谢~

0
回复
需求/bug反馈?一键提issue告诉我们
发现bug?如果您知道修复办法,欢迎提pr直接参与建设飞桨~
在@后输入用户全名并按空格结束,可艾特全站任一用户