Hamming Loss implementation in Keras

2,074 views
Skip to first unread message

Sudheer Kumar

unread,
Oct 31, 2018, 1:11:36 AM10/31/18
to Keras-users
Is there an example of Hamming Loss implementation in Keras for multi-label classification? Also wanted to see if anyone can share their experience with hamming noise loss function for multi-label classification. 

Is it possible to use SciKit implementation of Hamming Noise loss function with Keras?

With Best Regards
Sudheer Kumar

Lance N.

unread,
Nov 15, 2018, 6:44:40 PM11/15/18
to Keras-users
I found this on stackoverflow, but I have not tried it.

def hamming_loss(y_true, y_pred):
        return K.mean(y_true*(1-y_pred)+(1-y_true)*y_pred)

Sudheer Kumar

unread,
Nov 16, 2018, 4:02:09 PM11/16/18
to Keras-users
Thank you, Lance. I found another implementation by Manish on Medium. I wish Keras would provide some of these functions in the upcoming releases. I used this function but I didn't see a huge lift compared BCE. 

# CREDITS

import keras.backend as K

def hn_multilabel_loss(y_true, y_pred):
    # Avoid divide by 0
    y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())
    # Multi-task loss
    return K.mean(K.sum(- y_true * K.log(y_pred) - (1 - y_true) * K.log(1 - y_pred), axis=1))

ckm...@gmail.com

unread,
May 27, 2019, 8:07:16 AM5/27/19
to Keras-users
Hi, what Manish has implemented is cross entropy loss over the entire data. Hamming loss is something which computes the difference between true labels and predicted labels. See here, https://scikit-learn.org/stable/modules/model_evaluation.html#hamming-loss
Message has been deleted

ckm...@gmail.com

unread,
May 27, 2019, 10:37:55 AM5/27/19
to Keras-users
Here is my imp. I assume that y_pred is coming from a sigmoid layer without thresholding at 0.5

def Hamming_loss(y_true, y_pred):
    tmp = K.abs(y_true-y_pred)
    return K.mean(K.cast(K.greater(tmp,0.5),dtype=float))
- show quoted text -
Reply all
Reply to author
Forward
0 new messages