gather_nd 用法分享
如意_鸡蛋 发布于2020-07-28 23:20 浏览:248 回复:0
0
收藏
快速回复

https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/gather_nd_cn.html

原API的使用实例一点都没写明白,我也是最近使用静态图才摸索出使用方法,现在分享一下

目的,是在[4,3,64]维的张量输出合并后的目标索引,通过测试后满足个人使用的需求,供大家参考!

 

import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[4, 3, 64], dtype='float32')
index = fluid.layers.data(name='index', shape=[4,2], dtype='int32')
output = fluid.layers.gather_nd(x, index)


use_gpu = False
if use_gpu:
place = fluid.CUDAPlace(0)
else:
place = fluid.CPUPlace()

exe = fluid.executor.Executor(place=place)
exe.run(fluid.default_startup_program())
x = np.random.uniform(size=(4, 3, 64)).astype('float32')
print(x)
d = np.array([[0,0],[1,1],[2,2],[3,1]]).astype('int')
[y_] = exe.run(fluid.default_main_program(), feed={'x':x,'index':d}, fetch_list=[output])
print('M',y_.shape)
print('Y',y_)
收藏
点赞
0
个赞
TOP
切换版块