PaddlePaddle使用长短期记忆模型的时候出现维度错误

  • 关键字:数据维度词向量

  • 问题描述:在使用fluid.layers.dynamic_lstm建立一个长短期记忆网络时,出现数据维度或者权重不一致的错误。

  • 报错信息:

 in stacked_lstm_net(data, input_dim, class_dim, emb_dim, hid_dim, stacked_num)
      6 
      7 #     fc1 = fluid.layers.fc(input=emb, size=hid_dim)
----> 8     lstm1, cell1 = fluid.layers.dynamic_lstm(input=emb, size=hid_dim)
      9 
     10     inputs = [fc1, lstm1]

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/layers/nn.py in dynamic_lstm(input, size, h_0, c_0, param_attr, bias_attr, use_peepholes, is_reverse, gate_activation, cell_activation, candidate_activation, dtype, name)
    434             'gate_activation': gate_activation,
    435             'cell_activation': cell_activation,
--> 436             'candidate_activation': candidate_activation
    437         })
    438     return hidden, cell

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/layer_helper.py in append_op(self, *args, **kwargs)
     48 
     49     def append_op(self, *args, **kwargs):
---> 50         return self.main_program.current_block().append_op(*args, **kwargs)
     51 
     52     def multiple_input(self, input_param_name='input'):

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/framework.py in append_op(self, *args, **kwargs)
   1205         """
   1206         op_desc = self.desc.append_op()
-> 1207         op = Operator(block=self, desc=op_desc, *args, **kwargs)
   1208         self.ops.append(op)
   1209         return op

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/framework.py in __init__(***failed resolving arguments***)
    654         if self._has_kernel(type):
    655             self.desc.infer_var_type(self.block.desc)
--> 656             self.desc.infer_shape(self.block.desc)
    657 
    658     def _has_kernel(self, op_type):

EnforceNotMet: Enforce failed. Expected w_dims[0] == frame_size, but received w_dims[0]:128 != frame_size:32.
The first dimension of Input(Weight) should be 32. at [/paddle/paddle/fluid/operators/lstm_op.cc:63]
PaddlePaddle Call Stacks: 
  • 问题复现:使用fluid.layers.embedding接口把输入的转换成词向量,然后使用这些词向量传入到fluid.layers.dynamic_lstm接口中,计划使用fluid.layers.dynamic_lstm接口创建一个长短期记忆网络。但是在执行训练时就报以上的错误,错误代码如下:
emb = fluid.layers.embedding(
    input=data, size=[input_dim, emb_dim], is_sparse=True)
lstm1, cell1 = fluid.layers.dynamic_lstm(input=emb, size=hid_dim)
  • 解决问题:上面的错误是因为使用fluid.layers.embedding创建的词向量和fluid.layers.dynamic_lstm所需的输入的维度不一致,为了解决这个问题,可以在中间加一个全连接层统一大小。正确代码如下:
emb = fluid.layers.embedding(
    input=data, size=[input_dim, emb_dim], is_sparse=True)
fc1 = fluid.layers.fc(input=emb, size=hid_dim)
lstm1, cell1 = fluid.layers.dynamic_lstm(input=fc1, size=hid_dim)

你可能感兴趣的:(PaddlePaddle,问答专区)