在keras中使用堆叠LSTM的InvalidArgumeInterror

在keras中使用堆叠LSTM的InvalidArgumeInterror,keras,recurrent-neural-network,Keras,Recurrent Neural Network,我正在使用一个数据集,其中批处理项是由具有形状的矩阵表示的文本(每个文本的最大句子数,每个句子的最大标记数)。它经过一个嵌入层(变成3d),然后是一个时间分布的LSTM,它为每个句子输出一个向量(回到2d)。然后,第二个LSTM层读取所有句子向量,并为每个批处理项输出最终向量,该向量可以通过正常的密集层 如下所示(使用keras.utils.plot_model生成),每个文本85个句子,每个句子40个标记: 以下是型号代码: inputs = Input([num_sentences, ma

我正在使用一个数据集,其中批处理项是由具有形状的矩阵表示的文本(每个文本的最大句子数,每个句子的最大标记数)。它经过一个嵌入层(变成3d),然后是一个时间分布的LSTM,它为每个句子输出一个向量(回到2d)。然后,第二个LSTM层读取所有句子向量,并为每个批处理项输出最终向量,该向量可以通过正常的密集层

如下所示(使用
keras.utils.plot_model
生成),每个文本85个句子,每个句子40个标记:

以下是型号代码:

inputs = Input([num_sentences, max_sentence_size])

vocab_size, embedding_size = embeddings.shape
init = initializers.constant(embeddings)
emb_layer = Embedding(vocab_size, embedding_size, mask_zero=True,
                      embeddings_initializer=init)
emb_layer.trainable = False
embedded = emb_layer(inputs)

projection_layer = Dense(lstm1_units, activation=None, use_bias=False,
                         name='projection')
projected = projection_layer(embedded)

lstm1 = LSTM(lstm1_units, name='token_lstm')
sentence_vectors = TimeDistributed(lstm1)(projected)

lstm2 = LSTM(lstm2_units, name='sentence_lstm')
final_vector = lstm2(sentence_vectors)

hidden = Dense(hidden_units, activation='relu', name='hidden')(final_vector)
scores = Dense(num_scores, activation='sigmoid', name='scorer')(hidden)

model = keras.models.Model(inputs, scores)
这对我来说很好,但我有以下错误:

Traceback (most recent call last):
  File "src/network.py", line 43, in <module>
    network.fit(x, y, validation_data=(xval, yval))
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1507, in fit
    initial_epoch=initial_epoch)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1156, in _fit_loop
    outs = f(ins_batch)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2269, in __call__
    **self.session_kwargs)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run
    feed_dict_string, options, run_metadata)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run
    target_list, options, run_metadata)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Inputs to operation sentence_lstm/while/Select_2 of type Select must have the same size and shape.  Input 0: [32,4000] != input 1: [32,100]
     [[Node: sentence_lstm/while/Select_2 = Select[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](sentence_lstm/while/Tile, sentence_lstm/while/add_5, sentence_lstm/while/Identity_3)]]


Caused by op u'sentence_lstm/while/Select_2', defined at:
  File "src/network.py", line 37, in <module>
    args.hidden_units)
  File "src/model.py", line 51, in create_model
    final_vector = lstm2(sentence_vectors)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/layers/recurrent.py", line 262, in __call__
    return super(Recurrent, self).__call__(inputs, **kwargs)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 596, in __call__
    output = self.call(inputs, **kwargs)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/layers/recurrent.py", line 341, in call
    input_length=input_shape[1])
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2538, in rnn
    swap_memory=True)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2605, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2438, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2388, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2509, in _step
    new_states = [tf.where(tiled_mask_t, new_states[i], states[i]) for i in range(len(states))]
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 2301, in where
    return gen_math_ops._select(condition=condition, t=x, e=y, name=name)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 2386, in _select
    name=name)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/erick/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Inputs to operation sentence_lstm/while/Select_2 of type Select must have the same size and shape.  Input 0: [32,4000] != input 1: [32,100]
     [[Node: sentence_lstm/while/Select_2 = Select[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](sentence_lstm/while/Tile, sentence_lstm/while/add_5, sentence_lstm/while/Identity_3)]]

好的,我想我找到了错误

final_vector = lstm2(sentence_vectors)
应该是

final vector = (lstm2)(sentence_vectors)
否则,您将调用lstm2作为一个函数,并将
语句_向量
作为参数

从问题转移到:

更新:经过多次搜索,我发现问题在于TimeDistributed不适用于掩蔽。我可以使用TimeDistributed(emb_层)(输入)包装嵌入层调用,使模型运行,但这将禁用整个模型的屏蔽

这是Keras已知的问题,但仍没有解决方案:


看起来是输入的大小有问题。您使用什么作为model.fit的输入?这就是抛出错误的地方。我认为问题不在于输入。我用更多的信息编辑了这个问题,显示问题是在调用第二个LSTM层时引起的。它应该是一个函数调用,因为它是函数KerasAPI。每个层对象实现
call()
方法。
final vector = (lstm2)(sentence_vectors)