topk 文档中说支持输入为一维向量,但实测时报错。
Created by: lgone2000
文档为 http://paddlepaddle.org/documentation/docs/zh/1.4/api_cn/layers_cn.html#topk
测试代码如下
def test_top():
input = fluid.layers.data(name='input', shape=[5], dtype='float32')
# this can't work ,
reshapeinput = fluid.layers.reshape(input,[-1])
#this can work
#reshapeinput = fluid.layers.reshape(input,[1,-1])
topval, topindex = fluid.layers.topk(reshapeinput, k = 3, name='topk')
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
inputdata = np.random.rand(10,5).astype(np.float32)
outputlist = exe.run(
fluid.default_main_program(),
feed={'input': inputdata},
fetch_list=[reshapeinput.name, topval.name])
print outputlist