LSTM 单元,输入形状错误

LSTM Cells, Input Shape Error

我正在尝试使用我的 41 个字段的输入数据构建 LSTM 网络。我的想法是,当前输出是当前输入以及 49 个先前输入的函数。我正在尝试 运行 以下内容:

CommonModel = Sequential()
CommonModel.add(LSTM(50, return_sequences=True, input_shape=(None, 41)))
CommonModel.add(LSTM(50, return_sequences=True))
CommonModel.add(LSTM(50))
CommonModel.add(Dense(20,activation='relu'))
CommonModel.add(Dense(10,activation='relu'))
CommonModel.add(Dense(1,activation='relu'))
CommonModel.compile(loss = 'mse', optimizer = 'adam', metrics=['accuracy'])
CommonModel.summary()

图层(类型)输出形状参数#

dense_41 (密集) (None, None, 50) 2100


dense_42 (密集) (None, None, 20) 1020


dense_43 (密集) (None, None, 10) 210


dense_44 (密集) (None, None, 1) 11

总参数:3,341 可训练参数:3,341 不可训练的参数:0


CommonModel.fit(Axis_X,Axis_Y,epochs=140,batch_size=64)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-90-f80115738f18> in <module>()
----> 1 CommonModel.fit(Axis_X,Axis_Y,epochs=140,batch_size=64)

~\Anaconda3\lib\site-packages\keras\models.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)
    961                               initial_epoch=initial_epoch,
    962                               steps_per_epoch=steps_per_epoch,
--> 963                               validation_steps=validation_steps)
    964 
    965     def evaluate(self, x=None, y=None,

~\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)
   1628             sample_weight=sample_weight,
   1629             class_weight=class_weight,
-> 1630             batch_size=batch_size)
   1631         # Prepare validation data.
   1632         do_validation = False

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
   1474                                     self._feed_input_shapes,
   1475                                     check_batch_axis=False,
-> 1476                                     exception_prefix='input')
   1477         y = _standardize_input_data(y, self._feed_output_names,
   1478                                     output_shapes,

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    111                         ': expected ' + names[i] + ' to have ' +
    112                         str(len(shape)) + ' dimensions, but got array '
--> 113                         'with shape ' + str(data_shape))
    114                 if not check_batch_axis:
    115                     data_shape = data_shape[1:]

ValueError: Error when checking input: expected dense_41_input to have 3 dimensions, but got array with shape (1827, 41)

我试过 input_shape = (41) 根本不起作用。

你能告诉我哪里出了问题吗?

模型代码设置看起来不错。您需要将数据转换为 LSTM 的时间序列作为第一层。 input_shape=(49, 41) 表示每个时间步有 49 个时间步和 41 个特征。您可以使用 TimeseriesGenerator (documentation) 以这种方式 window 您的数据。大致如下:

data_gen = TimeseriesGenerator(data, targets, length=49)