How is it possible to make a classification with custom distance between classes in Keras?

For example, let's say I need to classify betweenA1,A2,B1,B2, but I "prefer" to make a mistake between A1 and A2 than between A1 andB2.


1 Answer 1


It is very easy to implement a custom loss function in Keras, but that may make your model less interpretable, and may make it more difficult to optimize.

You can create a function that takes in the true labels and predicted labels, and returns the loss value and then the function can then be passed to the compile method of your Keras model, along with the loss parameter.

Here is an example that you can refer:

import tensorflow as tf

def custom_distance(y_true, y_pred):
   # y_true and y_pred are tensors of the same shape
   y_true = tf.argmax(y_true, axis=-1)
   y_pred = tf.argmax(y_pred, axis=-1)

   # Calculate the distance between the predicted and true labels
   distance = tf.cast(tf.not_equal(y_true, y_pred), tf.float32)

   # Return a higher distance for mistakes between A1 and B2
   return tf.where(
       tf.logical_and(tf.equal(y_true, 0), tf.equal(y_pred, 2)),
       distance * 2.0,  # Mistake between A1 and B2
       distance         # Other mistakes

# Use the custom distance in your Keras model


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.