LSTM 单元格,输入形状错误



我正在尝试使用包含 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(密集((无、无、50( 2100


dense_42(密集((无、无、20( 1020


dense_43(密集((无、无、10( 210


dense_44(密集((无、无、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)
~Anaconda3libsite-packageskerasmodels.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,
~Anaconda3libsite-packageskerasenginetraining.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
~Anaconda3libsite-packageskerasenginetraining.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,
~Anaconda3libsite-packageskerasenginetraining.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(文档(以这种方式对数据进行窗口。大致如下:

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

最新更新