python-3.xtensorflowkerasmultilabel-classificationscikits

how to implement hamming loss as a custom metric in keras model


how to implement hamming loss as a custom metric in keras model
I have a multilabel classification with 6 classes

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',hamming_loss])

I tried using

from sklearn.metrics import hamming_loss
def custom_hl(y_true, y_pred):    
  return hamming_loss(y_true, y_pred)

which doesn't work because i have the y_true , y_pred as follow

YTRUE
Tensor("Cast_10:0", shape=(None, 6), dtype=float32)
YPRED
Tensor("model_1/dense_1/Sigmoid:0", shape=(None, 6), dtype=float32)

also tried the function in this question and it doesn't work Getting the accuracy for multi-label prediction in scikit-learn is there any way I can get the hamming loss as metric in keras thanks for any help


Solution

  • so i found a way and

    def Custom_Hamming_Loss(y_true, y_pred):
      return K.mean(y_true*(1-y_pred)+(1-y_true)*y_pred)
    
    def Custom_Hamming_Loss1(y_true, y_pred):
      tmp = K.abs(y_true-y_pred)
      return K.mean(K.cast(K.greater(tmp,0.5),dtype=float))
    

    source:https://groups.google.com/g/keras-users/c/_sjndHbejTY?pli=1