How to write a custom f1 loss function with weighted average for keras? How to write a custom f1 loss function with weighted average for keras? python-3.x python-3.x

How to write a custom f1 loss function with weighted average for keras?


The variables are self explained:

def f1_weighted(true, pred): #shapes (batch, 4)    #for metrics include these two lines, for loss, don't include them    #these are meant to round 'pred' to exactly zeros and ones    #predLabels = K.argmax(pred, axis=-1)    #pred = K.one_hot(predLabels, 4)     ground_positives = K.sum(true, axis=0) + K.epsilon()       # = TP + FN    pred_positives = K.sum(pred, axis=0) + K.epsilon()         # = TP + FP    true_positives = K.sum(true * pred, axis=0) + K.epsilon()  # = TP        #all with shape (4,)        precision = true_positives / pred_positives     recall = true_positives / ground_positives        #both = 1 if ground_positives == 0 or pred_positives == 0        #shape (4,)    f1 = 2 * (precision * recall) / (precision + recall + K.epsilon())        #still with shape (4,)    weighted_f1 = f1 * ground_positives / K.sum(ground_positives)     weighted_f1 = K.sum(weighted_f1)        return 1 - weighted_f1 #for metrics, return only 'weighted_f1'

Important notes:

This loss will work batchwise (as any Keras loss).

So if you are working with small batch sizes, the results will be unstable between each batch, and you may get a bad result. Use big batch sizes, enough to include a significant number of samples for all classes.

Since this loss collapses the batch size, you will not be able to use some Keras features that depend on the batch size, such as sample weights, for instance.