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.
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?