根据有状态的LSTM预测确定形状以解决回归问题

时间:2020-04-26 11:21:43

标签: python tensorflow machine-learning keras lstm

基于this stackoverflow postthis one,我正在尝试构建RNN LSTM模型来预测回归问题。

我的数据是25个批次的2720个样本,每个样本具有16个特征,有些批次用-10值填充。我建立了以下模型:

model = Sequential()
opt = Adam(learning_rate=0.0001, clipnorm=1)

num_samples = train_x.shape[1]
num_features = train_x.shape[2]

# Masking -10 rows
model.add(Masking(mask_value=-10., input_shape=(num_samples, num_features)))
model.add(LSTM(32, return_sequences=True, stateful=False activation='tanh'))
model.add(Dropout(0.3))

#this is the last LSTM layer, use return_sequences=False
model.add(LSTM(16, return_sequences=True, stateful=False,  activation='tanh'))
model.add(Dropout(0.3))
model.add(Dense(16, activation='tanh'))
model.add(Dense(8, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='mse', optimizer='adam' ,metrics=[metrics.mean_absolute_error, metrics.mean_squared_error])

摘要:

Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
masking_4 (Masking)          (None, 2720, 16)          0         
_________________________________________________________________
lstm_8 (LSTM)                (None, 2720, 32)          6272      
_________________________________________________________________
dropout_8 (Dropout)          (None, 2720, 32)          0         
_________________________________________________________________
lstm_9 (LSTM)                (None, 2720, 16)          3136      
_________________________________________________________________
dropout_9 (Dropout)          (None, 2720, 16)          0         
_________________________________________________________________
dense_12 (Dense)             (None, 2720, 16)          272       
_________________________________________________________________
dense_13 (Dense)             (None, 2720, 8)           136       
_________________________________________________________________
dense_14 (Dense)             (None, 2720, 1)           9         
=================================================================
Total params: 9,825
Trainable params: 9,825
Non-trainable params: 0
_________________________________________________________________

在训练时,模型不是有状态的,并且在预测要建立相同的模型时,这次是有状态的模型,该模型没有遮罩层,批处理大小为1:

s_model = Sequential()
...
s_model.add(LSTM(32, return_sequences=True, stateful=True, activation='tanh',batch_input_shape=(1, num_samples, num_features)))
...
s_model.add(LSTM(16, return_sequences=True, stateful=True,  activation='tanh'))
...
s_model.summary()

摘要:

Model: "sequential_23"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_41 (LSTM)               (1, 2720, 32)             6272      
_________________________________________________________________
dropout_31 (Dropout)         (1, 2720, 32)             0         
_________________________________________________________________
lstm_42 (LSTM)               (1, 2720, 16)             3136      
_________________________________________________________________
dropout_32 (Dropout)         (1, 2720, 16)             0         
_________________________________________________________________
dense_39 (Dense)             (1, 2720, 16)             272       
_________________________________________________________________
dense_40 (Dense)             (1, 2720, 8)              136       
_________________________________________________________________
dense_41 (Dense)             (1, 2720, 1)              9         
=================================================================
Total params: 9,825
Trainable params: 9,825
Non-trainable params: 0
_________________________________________________________________

我将权重加载到状态模型中,并尝试通过样本进行预测(过滤-10个样本并在每个序列后重置,如下所示:

#loading weights from trained model
s_model.set_weights(model.get_weights())

for sequence in test_x:
  for sample in sequence:
    #filtering padded samples
    if sample[0] is not -10:
      score = s_model.predict_on_batch([[[sample]]])
      print(score)
  print("-----------------------------------------------------")
  s_model.reset_states()

但是,我的代码失败,并且出现以下错误:

ValueError: Error when checking input: expected lstm_39_input to have shape (2720, 16) but got array with shape (1, 16)

任何帮助将不胜感激

0 个答案:

没有答案