How can I save the best model checkpoint for when I have a combination of best validation accuracy and best sensitivity? I have an imbalanced dataset with 16% of the data being class 1 and 84% of the data being class 0. I am using the focal loss with these arguments: gamma=3.0, alpha=0.25
I have this code for saving the best model checkpoint
based on best accuracy:
if epoch_val_accuracy > best_val_acc:
print('inside if - epoch is {}, val_acc is {}, and best_pred is {}'.format(epoch, epoch_val_accuracy, best_val_acc))
best_val_acc = epoch_val_accuracy
best_epoch = epoch
best_preds = epoch_val_preds
best_val_labels = epoch_val_labels
print("Saving the best model...")
torch.save(model.state_dict(), model_path + task_name + ".pth")
result is:
Predicted Low Predicted High
Actual Low 51 24
Actual High 9 5
best val acc: tensor(0.8619, device='cuda:0')
best epoch: 39
best preds: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
best val labels: [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
best val specifity: 1.0
best val sensitivity: 0.07142857142857142
best cm df: Predicted Low Predicted High
Actual Low 75 0
Actual High 13 1
based on best sensitivity:
if epoch_sensitivity > best_val_sensitivity:
best_val_acc = epoch_val_accuracy
best_epoch = epoch
best_preds = epoch_val_preds
best_val_labels = epoch_val_labels
best_val_sensitivity = epoch_sensitivity
best_val_specifity = epoch_specifity
print("Saving the best model...")
torch.save(model.state_dict(), model_path + task_name + ".pth")
best preds: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
best val labels: [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
best val specifity: 0.0
Also, when I somehow tried to combine the accuracy and best sensitivity, like following
I got this result:
if epoch_sensitivity > best_val_sensitivity:
if epoch_val_accuracy > 0.7:
best_val_acc = epoch_val_accuracy
best_epoch = epoch
best_preds = epoch_val_preds
best_val_labels = epoch_val_labels
best_val_sensitivity = epoch_sensitivity
best_val_specifity = epoch_specifity
print("Saving the best model...")
best_cm_df = pd.DataFrame(cm,
columns = ['Predicted Low', 'Predicted High'],
index = ['Actual Low', 'Actual High'])
torch.save(model.state_dict(), model_path + task_name + ".pth")
best val acc: tensor(0.7159, device='cuda:0')
best epoch: 36
best preds: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]
best val labels: [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
best val specifity: 0.7733333333333333
best val sensitivity: 0.2857142857142857
best cm df: Predicted Low Predicted High
Actual Low 58 17
Actual High 10 4