WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Keras · Advanced · question 43 of 100

How do you implement custom loss functions and custom metrics in Keras, and when would you need to do so?

📕 Buy this interview preparation book: 100 Keras questions & answers — PDF + EPUB for $5

In Keras, you can implement custom loss functions and custom metrics by defining them as functions using the backend functions provided by Keras.

A custom loss function can be defined by creating a function that takes in two arguments - y_true (the ground truth values) and y_pred (the predicted values) - and returns a scalar value representing the loss. For example, let’s say you want to implement a custom loss function called "weighted_binary_crossentropy" that calculates binary cross-entropy with a weighted penalty for misclassifying positive and negative samples, you could define it as follows:

import keras.backend as K

def weighted_binary_crossentropy(y_true, y_pred):
    # Set class weights as an example
    class_weight_0 = 0.2
    class_weight_1 = 0.8
    
    # Calculate binary crossentropy
    bce = K.binary_crossentropy(y_true, y_pred)
    
    # Apply weights
    weighted_bce = class_weight_0*(1-y_true)*K.log(1-y_pred) + 
                   class_weight_1*(y_true)*K.log(y_pred)
    
    return -K.mean(weighted_bce)

To use this custom loss function, you would simply pass its name to the ‘compile()‘ method of your Keras model:

model.compile(optimizer='adam',
              loss=weighted_binary_crossentropy)

Similarly, a custom metric can be defined by creating a function that takes in two arguments - y_true and y_pred - and returns a scalar value representing the metric. For example, let’s say you want to implement a custom metric called "f1_score" that calculates the F1 score of a binary classification problem, you could define it as follows:

def f1_score(y_true, y_pred):
    # Calculate true positives, false positives, and false negatives
    tp = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    fp = K.sum(K.round(K.clip(y_pred - y_true, 0, 1)))
    fn = K.sum(K.round(K.clip(y_true - y_pred, 0, 1)))
    
    # Calculate precision and recall
    p = tp / (tp + fp + K.epsilon())
    r = tp / (tp + fn + K.epsilon())
    
    # Calculate F1 score
    f1 = 2*(p*r) / (p+r+K.epsilon())
    
    return f1

To use this custom metric, you would pass its name to the ‘compile()‘ method of your Keras model, just as you would with a built-in metric:

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

You might need to implement custom loss functions or custom metrics when working with complex machine learning problems where the built-in Keras functionalities may not be sufficient. For example, you might want to implement a custom loss function or metric when working with imbalanced datasets, or when you are working on a problem where the built-in functionality does not provide an adequate solution. In general, it is a good idea to use built-in loss functions and metrics whenever possible, as they are generally well optimized and well documented. However, when they are not adequate, Keras provides the flexibility of implementing custom functions to suit your needs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Keras interview — then scores it.
📞 Practice Keras — free 15 min
📕 Buy this interview preparation book: 100 Keras questions & answers — PDF + EPUB for $5

All 100 Keras questions · All topics