问题
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:
- If I have different losses, and I want to weight them by using the variable
alphawhich its value depend on the epoch number. So I have to set the value pfalphathrough callback. My question is it possible to pass this alpha variable that its value changed with epoch number (i.e not scalar) through theloss_weightsinmodel.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)])
My dataset is imbalanced, so I want to use
class_wightsinmodel.fit(). So for the same model of three losses, I want to apply theclass weightsonly 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 tomodel.fit? Knowing that the third loss is custom loss function.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.
来源:https://stackoverflow.com/questions/52269603/multiple-losses-for-imbalanced-dataset-with-keras