Here’s what works for me:
  model = model.train()
    best_accuracy = 0
    ...
    for epoch in range(100):
        for idx, data in enumerate(data_loader):
                        ...
        if cur_accuracy > best_accuracy:
                torch.save(model.state_dict(), 'best_model.pt')