Milad Rad
Milad Rad

Reputation: 31

Custom keras loss

I'm using keras with tensorflow backend and I'm trying to write a custom keras loss function and I need to calculate the f1 score for each of my classes (I have 4 classes) the problem is when I write the code I get an error while compiling the model since the y_true and y_pred are Placeholders while the model is compiling and for that I can't use scikit-learn to calculate the f1 score. The usual answer to the problem is to use keras backend built-in functions for custom loss but it seems a bit hard to use them for per class f1 score calculation. I would appreciate any help on the matter :)

def F1_Loss(y_true,y_pred):
    y_true = K.eval(y_true)
    y_pred = K.eval(y_pred)
    f1_vector = sklearn.metrics.f1_score(y_true,to_categorical(np.argmax(y_pred,axis=1),num_classes=4),average=None)
    return np.mean(f1_vector)

Upvotes: 0

Views: 127

Answers (1)

Milad Rad
Milad Rad

Reputation: 31

I solved the problem I post it here in case anyone has the same problem

def F1_Vector(y_true,y_pred):
    tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
    tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)
    fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)
    fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
    p = tp / (tp + fp + K.epsilon())
    r = tp / (tp + fn + K.epsilon())
    f1 = 2*p*r / (p+r+K.epsilon())
    f1_vector = (tf.where(tf.math.is_nan(f1), tf.zeros_like(f1), f1))
    return f1_vector


Upvotes: 2

Related Questions