问题
Based on this stackoverflow post and this one, I'm trying to build an RNN LSTM model to predict on a regression problem.
My data is 25 batches of 2720 samples with 16 features each, some batches are padded with -10 values. I've build the following model:
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])
Summary:
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
_________________________________________________________________
While training, the model is not stateful and when predicting I'm building the same model, this time a stateful model without the masking layer and with batch size of 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()
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
_________________________________________________________________
I loaded the weights to the stateful model and tried to predict by sample (filtering the -10 samples and reseting after each sequence like so:
#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()
However, my code fails and i get the following error:
ValueError: Error when checking input: expected lstm_39_input to have shape (2720, 16) but got array with shape (1, 16)
Any help would be appreciated
来源:https://stackoverflow.com/questions/61439833/fixing-shape-on-a-stateful-lstm-predictions-for-a-regression-problem