对DynamicRNN输出用sequence_pad报错
Created by: jinxing94
1) 问题: 对DynamicRNN输入时一个Lod_level=1的tensor, 输出的变量,使用sequence_pad报错。
paddle/fluid/framework.py", line 1058, in init self.desc.infer_shape(self.block.desc) paddle.fluid.core.EnforceNotMet: Enforce failed. Expected x_desc->GetLoDLevel() >= 1, but received x_desc->GetLoDLevel():0 < 1:1.
2) 版本 paddle paddle 1.4.1
- 代码
class Model(object):
def __init__(self, input_size, hidden_size):
"""
init
:param input_dim:
:param output_dim:
"""
self.x = fluid.layers.data(name="x", shape=[input_size], lod_level=1)
self.hidden_size = hidden_size
self.build()
def build(self):
"""
build
:return:
"""
self.x_proj = fluid.layers.fc(
self.x, self.hidden_size * 3,
param_attr=ParamAttr(name="gru.weight_ih_l0"),
bias_attr=False)
drnn = fluid.layers.DynamicRNN()
with drnn.block():
input = drnn.step_input(self.x_proj, level=0)
hidden = drnn.memory(shape=[self.hidden_size])
hidden_proj = fluid.layers.fc(
hidden, self.hidden_size * 3,
param_attr=ParamAttr(name="gru.weight_hh_l0"),
bias_attr=False)
i_r, i_i, i_n = fluid.layers.split(
input, 3)
h_r, h_i, h_n = fluid.layers.split(
hidden_proj, 3)
resetgate = fluid.layers.sigmoid(i_r + h_r)
inputgate = fluid.layers.sigmoid(i_i + h_i)
newgate = fluid.layers.tanh(i_n + resetgate * h_n)
new_hidden = newgate + inputgate * (hidden - newgate)
drnn.update_memory(hidden, new_hidden) # set prev to hidden
drnn.output(new_hidden)
self.outputs = drnn()
self.hidden = fluid.layers.sequence_last_step(self.outputs)
pad_val = fluid.layers.assign(
input=np.array([0.0], dtype=np.float32))
self.outputs_pad, _ = fluid.layers.sequence_pad(
self.outputs, pad_val)