python - 集成模型不采用与其子模型相同的输入
问题描述
为了减少方差,我预测在训练/测试期间使用 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] [[{{node lstm_2_input}}]] 为占位符张量“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]
==================================================================================================
解决方案
推荐阅读
- snowflake-cloud-data-platform - Snowflake - 推断所有表/视图列的数据类型并转换数据类型的脚本
- sql-server - 在查询完成之前,不会保存 SQL Server 中的字符串修改
- azure - 具有多个运行时的 Azure Function App?
- mediastore - 如何将下载管理器与存储访问框架/媒体存储一起使用
- javascript - 按复选框过滤 Div 中的多个值
- javascript - 接收 HTTPXMLResponse 500 '内部服务错误'
- containers - Saltstack 不会将克隆的 Proxmox LXC 容器识别为新容器
- javascript - 按从 001 到 999 的顺序生成数字,而不在数据中放置相同的数字
- java - 如何获得这个成就?
- c# - C# Nullable 静态分析——您可以使用具有多个返回值的条件后置条件吗?