首页 文章

LSTM单元,输入形状错误

提问于
浏览
0

我正在尝试使用包含41个字段的输入数据构建LSTM Net . 我的想法是,电流输出是电流输入和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)

~\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),但根本不起作用 .

你能告诉我这是错的吗?

1 回答

  • 0

    模型代码设置似乎很好 . 您需要将数据转换为LSTM作为第一层的时间序列 . input_shape=(49, 41) 意味着每个时间步长49个时间步长和41个特征 . 您可以使用 TimeseriesGeneratordocumentation)以这种方式窗口化数据 . 有点像:

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

相关问题