我正在尝试使用LSTM预测时间序列。为了减少方差,我尝试使用3个模型进行预测并取3个模型的平均值,这给了我更好的结果。经过培训和验证后,我现在想保存模型以供将来预测。但是,由于我有3种不同的模型,所以我想知道是否有可能将它们合并为一个模型,然后保存/加载它,或者是否应该一一保存所有模型并稍后加载以供将来预测?
# 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)
答案 0 :(得分:1)
您正在追求整体模型。
类似以下内容:
askopenfilename
保存整体模型:
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()
加载并预测:
modelEns.save(<path_to_model>)
还要选中this article。