Multiple losses for imbalanced dataset with Keras

2019-02-18 21:22发布

My Model:

I built a siamese network that take two input and has three outputs. So My loss functions is :

total loss = alpha( loss1) + alpah( loss2) + (1_alpah) ( loss3)

loss1 and loss2 is categorical cross entropy loss function, to classify the class identity from total of 8 classes. loss3 is similarity loss function ( euclidean distance loss), to verify if the both input from same class or different classes.

My questions are as follow:

  1. If I have different losses, and I want to weight them by using the variable alpha which its value depend on the epoch number. So I have to set the value pf alpha through callback. My question is it possible to pass this alpha variable that its value changed with epoch number (i.e not scalar) through the loss_weights in model.complie. The documentation said:

loss_weights: Optional list or dictionary specifying scalar coefficients (Python floats) to weight the loss contributions of different model outputs. The loss value that will be minimized by the model will then be the weighted sum of all individual losses, weighted by the loss_weights coefficients. If a list, it is expected to have a 1:1 mapping to the model's outputs. If a tensor, it is expected to map output names (strings) to scalar coefficients.

Example

alpha = K.variable(0., dtype=tf.float32)

def changeAlpha(epoch,logs):
    new_alpha = some_function(epoch)
    K.set_value(alpha, new_alpha)
    return

alphaChanger = LambdaCallback(on_epoch_end=changeAlpha) 

model.compile(loss= [loss1, loss2, loss3], loss_weights = [alpha, alpha, (1-alpha)])
  1. My dataset is imbalanced, so I want to use class_wights in model.fit(). So for the same model of three losses, I want to apply the class weights only for categorical cross entropy losses ( loss 1 and loss2) , So will it work on both losses and except the third loss if I pass it to model.fit? Knowing that the third loss is custom loss function.

  2. If I want to classify the classes for siamese network, would my metric be model.compile(metrics= ['out1':'accuracy', 'out2':accuracy']])? But the final accuracy need to be the average of both,I can solve it by building my own custom metric. But is there anyway to weighted summing of both metrics.

0条回答
登录 后发表回答