[英]Incompatible dimensions in LSTM Keras layer
我正在使用 Keras 对序列模型进行编码,但出现此错误:
ValueError: Input 0 of layer lstm_59 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 20]
这是我的数据:
print(database['sentence1'][0], database['sentence2'][0])
>>> 'It does not matter how you win it , just as long as you do .',
>>> 'It does not matter how you win , only as long as you do it .'
我为我的数据创建了一个序数编码(每个单词都是一个类别),所以我为输入和目标句子创建了一个字典,这是一些变量形状:
number of samples = 2500
unique_input_words = 12738
unique_output_words = 12230
input_length = 20
output_length = 20
encoding_input.shape = (2500, 20)
decoding_input.shape = (2500, 20)
decoding_output.shape = (2500, 20)
基本上编码/解码数组是 2500 个样本的列表,每个样本有 20 个元素的长度,(解码将返回一个句子):
print(encoding_input[0])
[12049 5684 3021 11494 8362 8598 8968 8371 3622 5583 8362 840 4061 8917 11710 4860 4491 4860 6411 4166]
这是我使用 LSTM 层的 RNN 模型(使用功能性 Keras API):
def create_model(
input_length=20,
output_length=20):
encoder_input = tf.keras.Input(shape=(None, input_length,))
decoder_input = tf.keras.Input(shape=(None, output_length,))
encoder, state_h, state_c = tf.keras.layers.LSTM(64, return_state=True, return_sequences=False)(encoder_input)
decoder = tf.keras.layers.LSTM(64, return_sequences=True)(decoder_input, initial_state=[state_h, state_c])
decoder = tf.keras.layers.Dense(20, activation="softmax")(decoder)
model = tf.keras.Model(inputs=[encoder_input, decoder_input], outputs=[decoder])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
model = create_model()
如果我用我的数据拟合模型:
model.fit([encoder_input, decoder_input],
decoder_output,
batch_size=64,
epochs=5)
首先我收到这个警告:
WARNING:tensorflow:Model was constructed with shape (None, None, 20) for input Tensor("input_67:0", shape=(None, None, 20), dtype=float32), but it was called on an input with incompatible shape (None, 20).
WARNING:tensorflow:Model was constructed with shape (None, None, 20) for input Tensor("input_68:0", shape=(None, None, 20), dtype=float32), but it was called on an input with incompatible shape (None, 20).
然后是整个追溯:
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:508 _run_internal_graph
outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/recurrent.py:663 __call__
return super(RNN, self).__call__(inputs, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
self.name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:180 assert_input_compatibility
str(x.shape.as_list()))
ValueError: Input 0 of layer lstm_59 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 20]
Model.summary()
:
Model: "functional_45"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_67 (InputLayer) [(None, None, 20)] 0
__________________________________________________________________________________________________
input_68 (InputLayer) [(None, None, 20)] 0
__________________________________________________________________________________________________
lstm_59 (LSTM) [(None, 64), (None, 21760 input_67[0][0]
__________________________________________________________________________________________________
lstm_60 (LSTM) (None, None, 64) 21760 input_68[0][0]
lstm_59[0][1]
lstm_59[0][2]
__________________________________________________________________________________________________
dense_22 (Dense) (None, None, 20) 1300 lstm_60[0][0]
==================================================================================================
Total params: 44,820
Trainable params: 44,820
Non-trainable params: 0
__________________________________________________________________________________________________
我知道错误可能是由于我的输出维度而发生的,但实际上我已经尝试了很多解决方案,但都没有奏效。
根据错误,预期维度为 3,而您的输入维度为 2。
LSTM 要求输入具有以下形状。
inputs
:形状为[batch, timesteps, feature]
3D 张量。 feature
维度通常包含嵌入向量,但您的代码没有任何嵌入,因此您的第三维应该是 1。您可以使用tf.expand_dims方法在输入的末尾添加一个维度: (2500,20,1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.