Saving a Model State with Optimal Accuracy in PyTorch

I have a question regarding selecting model parameters with optimal accuracy, here in below screenshot 2500th iteration have optimal accuracy and If I want to choose that particular model state in PyTorch, how should I do it? Please assist

  1. You could check in each iteration if the current accuracy is lesser than highest accuracy so far. If this happens for say n(3-5) iterations continuously, then you can stop.

Kindly refer to this for inspiration.

n_epochs_stop = 5 # no. of epochs to stop training after no improvement
epochs_no_improve = 0 # keep track of no improvement epochs

# inside the training loop ........
if epochs_no_improve == n_epochs_stop:
            print('Early stopping!' )
            early_stop = True
    if early_stop:
  1. You could Pytorch-lightning, which has the EarlyStopping callback - you can monitor any metric like val_loss, accuracy .
© Copyright 2013-2019 Analytics Vidhya