首页 > 解决方案 > keras同时训练2个模型

问题描述

我有 2 个模型:model1 和 model2。

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

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

  1. 我需要同时改进模型 1 的输出和改进模型 2 的分类。但是在测试中,我将分别使用每个模型。
  2. 在我看来,我需要使用模型 2 成本函数作为模型 1 成本函数 - 怎么做?
  3. 还有其他想法怎么做?

我强调:连接并不能解决问题。

请参考附图

标签: pythonmachine-learningkerasdeep-learningclassification

解决方案


一般草图如下:

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


推荐阅读