1

I am training a deep learning model. I have around 50 samples in validation set and thus getting same accuracy multiple times. I want to save the best model with highest accuracy and lowest loss given that accuracy. I have tried to use LambdaCallback but can't understand how to update the best validation loss and accuracy. First, I have defined the below function.

def save_best_model(epoch,logs,save_loc,model,best_val_acc,best_val_loss):
    val_acc = logs['val_acc']
    val_loss = logs['val_loss']
    if val_acc > best_val_acc:
        best_val_acc = val_acc
        model.save(save_loc)
    elif val_acc == best_val_acc:
        if val_loss < best_val_loss:
            best_val_loss=val_loss
            model.save(save_loc)
    print(best_val_acc,best_val_loss)
    return best_val_acc,best_val_loss

This code was answered in the following question.

How can I modify ModelCheckPoint in keras to monitor both val_acc and val_loss and save accordingly the best model?

However, I can't return the updated best loss and accuracy inside Lambdacallback. I have tried to write in following way, but it generates error.

erl_stop=keras.callbacks.LambdaCallback(on_epoch_end= lambda epoch, logs: (best_val_acc,best_val_loss)=save_best_model(epoch,logs,save_loc,model,best_val_acc,best_val_loss))

Can anyone suggest me how can I update the best loss and accuracy after every epoch inside the callback?

Suman
  • 59
  • 1
  • 11
  • Maybe [this](https://keras.io/callbacks/#create-a-callback) will help you to create custom callback by extending the base class ```keras.callbacks.Callback``` – Kurtis Streutker Jan 26 '19 at 03:19
  • Is it possible to define a function rather than define a class? – Suman Jan 26 '19 at 21:37

0 Answers0