This is what I got so far for making an lstm ensemble with one model input for each of the lstm models and for the ensemble model and it works perfectly.

model_input = Input(shape=(50,2))
def firstmodel(model_input):
      hiddenA1 = LSTM(6, return_sequences=True)(model_input)
      hiddenA2 = LSTM(4, activation='relu')(hiddenA1)
      outputA = Dense(24)(hiddenA2)
      model = Model(inputs= model_input, outputs= outputA, name="firstmodel")
      return model
def secondmodel(model_input):
      hiddenB1 = LSTM(30, return_sequences=True)(model_input)
      hiddenB2 = LSTM(20, activation='relu')(hiddenB1)
      outputB = Dense(24)(hiddenB2)
      model = Model(inputs= model_input, outputs= outputB, name="secondmodel")
      return model
firstmodel = firstmodel(model_input)
secondmodel = secondmodel(model_input)
models = [firstmodel, secondmodel]
def ensemble(models, model_input):
      outputs = [model.outputs[0] for model in models]
      y = Average()(outputs)
      model = Model(inputs = model_input, outputs = y, name="ensemble")
      return model
ensemblemodel = (models, model_input)
def evaluate_rmse(model):
      pred = model.predict(X_test)
      rmse = np.sqrt(mean_squared_error(y_test, pred))  
      return rmse
ensemble_rmse = evaluate_rmse(ensemblemodel)

But what happens when there are two different model inputs for the first and second model:

first_model_input = Input(shape=(50,2)) second_model_input = Input(shape=(60,2))

What will be the model_input for the ensemble model then? Also what will the shape of X_test be for the ensemble model? Because when there was only one input, like the first_model_input = Input(shape=(50,2)) then the X_test.shape was (2554, 50, 2).

But now there are two inputs, so there are two X_test shapes, one for first model input which is (2554, 50, 2) and one for the second model input which is (2544, 60, 2).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.