keras同时训练2个模型

时间:2018-10-24 18:34:19

标签: python machine-learning keras deep-learning classification

我有2个模型:model1和model2。

我需要获取模型1的输出并手动操作myData并将其(操纵的myData)设置为model2的输入。

model2的输出是相对于预定义分类(即监督)的myData(对model1输出操纵)响应的分类。

  1. 我需要同时改善model1的输出并改善model2的分类。但是在测试中,我将分别使用每种模型。
  2. 我认为我需要将模型2成本函数用作模型1成本函数-如何完成?
  3. 还有其他想法怎么做?

我强调:串联不能解决问题。

请参阅attached diagram

1 个答案:

答案 0 :(得分:1)

嗯,一般的草图如下:

# 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(...)

通过这种方式,model1model2会同时受到训练,您也可以独立使用它们(例如,使用model1.predict()model2.predict())。

相关问题