如何在Keras中连接两个LSTM模型

我想用Keras创建一个包含两个LSTM层的模型。然而,以下代码却产生了错误:

from keras.models import Sequentialfrom keras.layers import LSTM, Dropout, Activationfrom keras.callbacks import ModelCheckpointfrom keras.utils import to_categoricalmodel = Sequential()model.add(LSTM(5, activation="softmax"))model.add(LSTM(5, activation="softmax"))model.compile(loss='categorical_crossentropy',               optimizer='adam',               metrics=['categorical_accuracy'])# 这些值是要预测的。directions = [-2, -1, 0, 1, 2]# 样本数据。我们有三个时间步,每个时间步一个特征,以及一个结果值。data = [[[[1], [2], [3]], -1],         [[[3], [2], [1]], 2],         [[[4], [5], [7]], 1],        [[[1], [-1], [10]], -2]]X = []y_ = []# 现在我们从上面的数据中取10000个样本。for i in np.random.choice(len(data), 10000):    X.append(data[i][0])    y_.append(data[i][1])X = np.array(X)y_ = np.array(y_)y = to_categorical(y_ + 2, num_classes=5)model.fit(X, y,           epochs=3,          validation_data=(X, y))print(model.summary())loss, acc = model.evaluate(X, y)print("Loss: {:.2f}".format(loss))print("Accuracy: {:.2f}%".format(acc*100))

我得到的错误如下:

ValueError: Input 0 is incompatible with layer lstm_10: expected ndim=3, found ndim=2

完整的错误追溯如下:

---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-35-58fa9218c3f3> in <module>     31 model.fit(X, y,      32           epochs=3,---> 33           validation_data=(X, y))     34 print(model.summary())     35 C:\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)    950             sample_weight=sample_weight,    951             class_weight=class_weight,--> 952             batch_size=batch_size)    953         # Prepare validation data.    954         do_validation = FalseC:\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)    675             # to match the value shapes.    676             if not self.inputs:--> 677                 self._set_inputs(x)    678     679         if y is not None:C:\Anaconda3\lib\site-packages\keras\engine\training.py in _set_inputs(self, inputs, outputs, training)    587                 assert len(inputs) == 1    588                 inputs = inputs[0]--> 589             self.build(input_shape=(None,) + inputs.shape[1:])    590             return    591 C:\Anaconda3\lib\site-packages\keras\engine\sequential.py in build(self, input_shape)    219             self.inputs = [x]    220             for layer in self._layers:--> 221                 x = layer(x)    222             self.outputs = [x]    223             self._build_input_shape = input_shapeC:\Anaconda3\lib\site-packages\keras\layers\recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)    530     531         if initial_state is None and constants is None:--> 532             return super(RNN, self).__call__(inputs, **kwargs)    533     534         # If any of `initial_state` or `constants` are specified and are KerasC:\Anaconda3\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)    412                 # Raise exceptions in case the input is not compatible    413                 # with the input_spec specified in the layer constructor.--> 414                 self.assert_input_compatibility(inputs)    415     416                 # Collect input shapes to build layer.C:\Anaconda3\lib\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs)    309                                      self.name + ': expected ndim=' +    310                                      str(spec.ndim) + ', found ndim=' +--> 311                                      str(K.ndim(x)))    312             if spec.max_ndim is not None:    313                 ndim = K.ndim(x)ValueError: Input 0 is incompatible with layer lstm_10: expected ndim=3, found ndim=2

看起来第一个LSTM层的输出维度(应该是dim=2)与第二个LSTM层所需的输入维度(批次、时间步、特征的dim=3)不匹配。

让我困惑的是,像我这样添加LSTM层的方式在这里似乎是有效的:例如: https://adventuresinmachinelearning.com/keras-lstm-tutorial/

当我移除第二个LSTM层时,模型是可以工作的。


回答:

默认情况下,LSTM只在序列的最后一个元素后返回其最终输出。如果你想将两个LSTM层连接起来,那么你需要将第一个LSTM层在序列的每个元素后的输出传递给第二个LSTM层。例如:

model = Sequential()model.add(LSTM(5, return_sequences=True))model.add(LSTM(5, activation="softmax"))

有关return_sequence如何工作的详细信息,请参阅文档 https://keras.io/layers/recurrent/

Related Posts

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

如何向神经网络输入两个不同大小的输入?

我想向神经网络输入两个数据集。第一个数据集(元素)具有…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

发表回复

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