python - keras同时训练2个模型
问题描述
我有 2 个模型:model1 和 model2。
我需要获取模型 1 的输出并手动操作 myData 并将其(操作的 myData)设置为模型 2 的输入。
model2 的输出是 myData 响应的分类(对 model1 输出操作),相对于预定义的分类(即监督)。
- 我需要同时改进模型 1 的输出和改进模型 2 的分类。但是在测试中,我将分别使用每个模型。
- 在我看来,我需要使用模型 2 成本函数作为模型 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(...)
这样既可以同时训练model1
,model2
也可以独立使用它们(例如使用model1.predict()
或model2.predict()
)。
推荐阅读
- swift - Cocoa swift - 在 tableview 上缓慢加载数据
- amazon-web-services - AWS Lambda 在成功和超时之间交替
- amazon-cognito - AWS Cognito 不使用无服务器发送验证电子邮件
- javascript - ds3.js 颜色范围。
- javascript - POST 动态创建所需的输入字段
- java - 在布局中对齐 imageview
- apache-calcite - 方解石桌子上的重铸器
- r - 将 xml 文件差异转换为图形可视化
- git - git lfs 在钩子脚本上失去执行标志
- c# - 如何为 JSON 创建通用 DataContract 类