首页 > 解决方案 > 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}

标签: pythontensorflowkeras

解决方案


基本上有两种方法可以将不同的损失映射到不同的输出。

方法 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')

推荐阅读