Hi all, I want to save the best model and then load it during the test. So I use the following method:
def train():
#training steps …
if acc > best_acc:
best_acc = acc
best_state = model.state_dict()
return best_state
then in the main function I use:
model.load_state_dict(best_state)
to resume the model.
However, I found that best_state is always the same as the last state during training, not the best state. Is anyone know the reason and how to avoid it?  (Version: 1.1.0, Linux, GPU)
By the way, I know I can use torch.save(the_model.state_dict(), PATH) and then load the model by
the_model.load_state_dict(torch.load(PATH)).
However, I don’t want to save the parameters to file as train and test functions are in one file.