为了减少方差,我预测在训练/测试过程中会使用3种模型
test_reshaped = test_reshaped.reshape(len(test_reshaped), 1, 1)
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)
model1.save('lstm_model1.h5')
model2.save('lstm_model2.h5')
model3.save('lstm_model3.h5')
然后,为了将来使用,我创建了一个集成模型,该模型使用以下函数对3取平均值:
models = list()
nb_models = 3
for i in range(nb_models):
model_tmp = load_model("lstm_model"+str(i+1)+".h5")
model_tmp.name = "model_"+str(i+1)
models.append(model_tmp)
def create_ensemble(models,model_input):
# take-in all outputs fro all models
outModels = [model(model_input) for model in models]
# calculate average of all results
outAvg = layers.average(outModels)
# merge into one model
modelMerge = Model(inputs=model_input,outputs=outAvg,name='ensemble')
return modelMerge
model_input = Input(shape=models[0].input_shape[1:])
modelEns = create_ensemble(models,model_input)
modelEns.summary()
modelEns.save('ensemble_model.h5')
当我加载集合模型并在具有相同形状的相同数据上使用它时:
test_reshaped = test_reshaped.reshape(len(test_reshaped), 1, 1)
forecast1 = model.predict(test_reshaped,batch_size = batch_size)
我得到了错误: 您必须使用dtype float和shape [1,1,1]输入占位符张量“ lstm_2_input”的值 [[{{node lstm_2_input}}]]
生成3个子模型的功能:
# model
def fit_lstm(train, batch_size, nb_epoch, nb_neurons):
X, y = train[:, 0:-1], train[:, -1]
X = X.reshape(X.shape[0], 1, X.shape[1])
model = Sequential()
model.add(LSTM(nb_neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(nb_epoch):
model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
model.reset_states()
return model
3个模型的摘要:
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (1, 1) 12
_________________________________________________________________
dense_1 (Dense) (1, 1) 2
=================================================================
集成模型摘要:
_________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 1, 1) 0
__________________________________________________________________________________________________
model_1 (Sequential) multiple 14 input_1[0][0]
__________________________________________________________________________________________________
model_2 (Sequential) multiple 14 input_1[0][0]
__________________________________________________________________________________________________
model_3 (Sequential) multiple 14 input_1[0][0]
__________________________________________________________________________________________________
average_1 (Average) (None, 1) 0 model_1[1][0]
model_2[1][0]
model_3[1][0]
==================================================================================================