python - How to give conditions to a loss functionin in keras
问题描述
Say the model looks like this
inp = input()
feature = some_feature_layer()(inp)
out_1 = Dense(1,activation='sigmoid')(feature)
out_2 = Dense(10, activation='softmax')(feature)
What I want is to use the out_1 to weight the loss I use for out_2, which means the loss for out_2 should be something like
out_2_loss = out_1 * some_loss_function(y_true, out2)
I thought of writing the loss inside of the model, use the loss function as model output then simply increase/decrease the loss like this:
model = Model(inputs=[inp], outputs=[out_1, out_2_loss])
Then the problem becomes how to map different loss to different output. Is it possible to use a mapping like this in keras?
loss = {out_1 : 'binary_crossentropy',
out_2_loss : linear_function}
解决方案
基本上有两种方法可以将不同的损失映射到不同的输出。
方法 1: 如果输出被命名,使用 dict 将名称映射到相应的损失:
out1 = Dense(10, activation='softmax', name='binary_crossentropy')(x)
out2 = Dense(10, name='out2')(x)
model = Model(x, [out1, out2])
model.compile(loss={'binary_crossentropy': 'binary_crossentropy', 'out2': out_2_loss},
optimizer='adam')
方法 2: 使用损失列表
model = Model(x, [out1, out2])
model.compile(loss=['binary_crossentropy', out_2_loss], optimizer='adam')
推荐阅读
- c - 如何创建一个简单的函数,给定一个单词可以删除 C 中包含它的特定行
- spring - Spring WS 中配置自己替换 DefaultMethodEndpointAdapter 的正确方法是什么?
- c# - LINQ 等效于 SQL LIKE 用于带有数字的文本字段
- c# - 调用下一个委托后,中间件应使请求失败
- java - 定期从 spring 安全上下文中检索到的用户为空
- php - Spatie Medialibrary 裁剪图像白色背景边框
- wordpress - 在 WordPress 中为 SplitIt Payments 重新定位插件操作 woocommerce_before_cart_totals
- python - 将数据库项目输出到 pdf 文档
- hyperledger-fabric - 为什么 Go 链码不需要注解?
- kurento - Kurento自定义过滤器未实现