LSTM层在Keras中的维度不兼容

我在用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 = 2500unique_input_words = 12738unique_output_words = 12230input_length = 20output_length = 20encoding_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 modelmodel = 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,820Trainable params: 44,820Non-trainable params: 0__________________________________________________________________________________________________

我知道这个错误可能是因为我的输出的维度,但我实际上已经尝试了很多解决方案,但都没有奏效。


回答:

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注