Home > OS >  Custom loss for keras model to penalize certain predictions
Custom loss for keras model to penalize certain predictions

Time:10-25

I'm using a neural network for multiclass classification and I'd like to create a custom loss function that penalizes certain misclassifications. This can be done by calculating the cross-entropy loss and then multiplying it by a weight given by a custom-made penalty matrix, which holds a value for each pair of (predicted label, true label). The output layer is a softmax one with 12 neurons.

I'm having trouble implementing this custom loss for keras.model.compile():

def custom_loss(y_true, y_pred):
  loss = K.categorical_crossentropy(y_true, y_pred)

  # predicted: argmax of y_pred
  # true: argmax of y_true
  # penalty_weight = penalty_matrix[true, predicted]
  # loss = loss * penalty_weight

  return loss

How can predicted and true label be utilized inside this function to transform the loss?

CodePudding user response:

def custom_loss(y_true, y_pred):
  loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred)

  predicted = tf.math.argmax(y_true , axis=-1)
  true = tf.math.argmax(y_true , axis=-1)
  penalty_weight = tf.stack([true , predicted] , axis=1)
  loss  = tf.math.multiply(loss , penalty_weight)
  
  return loss

Want this?

  • Related