Reputation: 424
I'm trying to forecast time series using LSTMs. In order to reduce variance, I tried to predict using 3 models and take the average of the 3 which sort of gave me better results. After training and validation,I now want to save my model for future forecasts. However, since I have 3 different models, I would like to know if it's possible to merge them into ONE model and then save/load it or if I should save all models one by one and load them later for future predictions?
# fit 3 models
model1 = fit_lstm(train_scaled, batch_size,nb_epochs, nb_neurons)
model2 = fit_lstm(train_scaled, batch_size,nb_epochs, nb_neurons)
model3 = fit_lstm(train_scaled, batch_size,nb_epochs, nb_neurons)
# predict on test set using 3 models
forecast1 = model1.predict(test_reshaped, batch_size=batch_size)
forecast2 = model2.predict(test_reshaped, batch_size=batch_size)
forecast3 = model3.predict(test_reshaped, batch_size=batch_size)
Upvotes: 1
Views: 496
Reputation: 2016
You are after an ensemble model.
Something like the following:
from keras.models import load_model
models=[]
for i in range(numOfModels):
modelTemp=load_model(path2modelx) # load model
modelTemp.name="aUniqueModelName" # change name to be unique
models.append(modelTemp)
def ensembleModels(models, model_input):
# collect outputs of models in a list
yModels=[model(model_input) for model in models]
# averaging outputs
yAvg=layers.average(yModels)
# build model from same input and avg output
modelEns = Model(inputs=model_input, outputs=yAvg, name='ensemble')
return modelEns
model_input = Input(shape=models[0].input_shape[1:]) # c*h*w
modelEns = ensembleModels(models, model_input)
model.summary()
Save the ensemble model:
modelEns.save(<path_to_model>)
Load and predict:
modelEns=load_model(<path_to_model>)
modelEns.summary()
y=modelEns.predict(x)
Check this article as well.
Upvotes: 1