求教育,我希望使用DynamicRNN逐个获取序列元素的embedding结果并记录在memory中(方便后续pooling),可是我concat得到的结果与预期不一样,代码如下,请问我错在哪里,或者我该如何去做,才能达到我想要的结果呢(借助DynamicRNN逐个获取序列元素的embedding并做pooling)~~
代码:
# coding: utf-8
from pprint import pprint
import numpy as np
import paddle.fluid as fluid
a = fluid.create_lod_tensor(np.array([1,2,3,4,5,6,7,8,9,10], dtype='int64').reshape(-1, 1),
[[2,3,5]],
fluid.CPUPlace())
sentence = fluid.layers.data(name='sentence', shape=[1], dtype='int64', lod_level=1)
embedding = fluid.layers.embedding(input=sentence, size=[100, 6], is_sparse=True)
drnn = fluid.layers.DynamicRNN()
with drnn.block():
word = drnn.step_input(embedding)
curr_memory = drnn.memory(shape=[6], dtype='float32', value=1)
new_memory = fluid.layers.concat(input=[curr_memory, word])
drnn.update_memory(ex_mem=curr_memory, new_mem=new_memory)
drnn.output(new_memory)
rnn_output = drnn()
last = fluid.layers.sequence_last_step(rnn_output)
cpu = fluid.CPUPlace()
exe = fluid.Executor(cpu)
exe.run(fluid.default_startup_program())
rslt = exe.run(feed={'sentence': a}, fetch_list = [last])
print rslt
输出结果:
[array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]], dtype=float32)]
1条答案
按热度按时间7d7tgy0s1#
用别的方式跳过这个问题了。
因为我的目标是:逐个获取序列元素的embedding,累积之后,做pooling。
所以,rnn block之中,我可以不用memory,而是直接输出embedding,再上sequence_pool就够了。