Python keras同时培训2个模型
我有两个模型:模型1和模型2 我需要获取模型1的输出并手动操作myData,然后将其设置为模型2的输入 model2的输出是myData响应的分类(对model1输出操作),相对于预定义的分类(即监督)Python keras同时培训2个模型,python,machine-learning,keras,deep-learning,classification,Python,Machine Learning,Keras,Deep Learning,Classification,我有两个模型:模型1和模型2 我需要获取模型1的输出并手动操作myData,然后将其设置为模型2的输入 model2的输出是myData响应的分类(对model1输出操作),相对于预定义的分类(即监督) 我需要同时改进model1输出和model2分类。但是,在测试中,我将分别处理每个模型 在我看来,我需要使用模型2的成本函数作为模型1的成本函数-如何做到这一点 还有其他办法吗 我强调:串联并不能解决问题 请参考好吧,一般草图如下: # define model 1 architecture .
请参考好吧,一般草图如下:
# define model 1 architecture
...
# define model 2 architecture
...
# define manipulation logic
out1 = model1.output # get the output of model1
out1 = SomeLayer()(out1) # apply any number of layers as you wish
...
out_final = model2(out1) # feed the manipulated output to model2
# define the joint model
final_model = Model(model1.input, out_final)
# compile the model ...
final_model.compile(loss=..., optimizer=...) # loss is computed based on the output of model2
# fit the model
final_model.fit(...)
通过这种方式,
model1
和model2
将同时接受培训,您也可以独立使用它们(例如,使用model1.predict()
或model2.predict()
)。Sean的可能副本,在这种情况下,它们连接模型。在这里,我强调它们是独立的模型。请看一看图表。@Gandalf如果答案解决了您的问题,请单击答案旁边的复选标记将其标记为“已回答”以接受它-请参阅@Gandalf Long time,no see!你的错误是什么?编辑你的问题,并在文章末尾包含你得到的错误。