首页 > 解决方案 > 如何为 LSTM 生成训练样本?

问题描述

我在下面的数据框中有 100 个单独的时间序列(单位 - 索引),并且想要训练 LSTM 来预测目标('RUL')。

我的数据看起来像:

train.head()

cycles  os1 os2 sm2 sm3 sm4 sm6 sm7 sm8 sm9 sm11    sm12    sm13    sm14    sm15    sm17    sm20    sm21    RUL
unit                                                                            
1   1   -0.0007 -0.0004 641.82  1589.70 1400.60 21.61   554.36  2388.06 9046.19 47.47   521.66  2388.02 8138.62 8.4195  392 39.06   23.4190 191
1   2   0.0019  -0.0003 642.15  1591.82 1403.14 21.61   553.75  2388.04 9044.07 47.49   522.28  2388.07 8131.49 8.4318  392 39.00   23.4236 190
1   3   -0.0043 0.0003  642.35  1587.99 1404.20 21.61   554.26  2388.08 9052.94 47.27   522.42  2388.03 8133.23 8.4178  390 38.95   23.3442 189
1   4   0.0007  0.0000  642.35  1582.79 1401.87 21.61   554.45  2388.11 9049.48 47.13   522.86  2388.08 8133.83 8.3682  392 38.88   23.3739 188
1   5   -0.0019 -0.0002 642.37  1582.85 1406.22 21.61   554.00  2388.06 9055.15 47.28   522.19  2388.04 8133.80 8.4294  393 38.90   23.4044 187

维度是:

# Training data
X.shape
(20631, 18)


# Labels/targets
y.shape
(20631,)

到目前为止,我已经尝试过:

from keras.preprocessing.sequence import TimeseriesGenerator

data_gen = TimeseriesGenerator(X, y,
                               length=10, sampling_rate=2,
                               batch_size=2)

# Model
from keras.models import Sequential
from keras.layers import Dense, LSTM

model = Sequential()
model.add(LSTM(units=128, activation='relu', dropout=0.25, input_shape=(18,1))) # Input layer
model.add(Dense(units=1)) # Output layer
model.compile(optimizer='adam', loss='rmae')
#model.fit(x, y, epochs=30, batch_size=7)
model.fit_generator(data_gen, steps_per_epoch=len(data_gen), epochs=100)

...产生:

ValueError:无法将大小为 5 的序列复制到维度为 18 的数组轴

帮助将不胜感激。

标签: pythonpandasnumpykerasdeep-learning

解决方案


模型等待并输入 (x) 长度为 18 ( input_shape=(18,1)) 的数组/序列。但它得到一个长度为 5 的值。您应该更改TimeseriesGeneratorto的参数length=18sampeling_rate=1也许,以生成长度为 18 的输入。


推荐阅读