# Do trainer.save\_model saves the best model?

**URL:** <https://discuss.huggingface.co/t/do-trainer-save-model-saves-the-best-model/13296>\
**Category:** 🤗Transformers\
**Created:** [January 2, 2022, 10:00am UTC](https://discuss.huggingface.co/t/do-trainer-save-model-saves-the-best-model/13296 "2022-01-02T10:00:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gladiator](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/gladiator/32/17490_2.png) [@Gladiator](https://discuss.huggingface.co/u/Gladiator)\
**Post date:** [January 2, 2022, 10:00am UTC](https://discuss.huggingface.co/t/do-trainer-save-model-saves-the-best-model/13296/1 "2022-01-02T10:00:23Z")

</div>

I have set `load_best_model_at_end` to `True` for the `Trainer` class. And I want to save the best model in a specified directory. Does the method `save_model` of `Trainer` saves the best model or the last model in the specified directory?

---

<div class="post-metadata">

**Author:** ![mihai](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mihai/32/3567_2.png) [@mihai](https://discuss.huggingface.co/u/mihai)\
**Post date:** [January 3, 2022, 8:18am UTC](https://discuss.huggingface.co/t/do-trainer-save-model-saves-the-best-model/13296/2 "2022-01-03T08:18:13Z")

</div>

save\_model itself does what it say on the can: saves the model, good, bad, best it does not matter. It’s the rotate checkpoints method that will keep the best model from being deleted.

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [January 10, 2022, 3:46pm UTC](https://discuss.huggingface.co/t/do-trainer-save-model-saves-the-best-model/13296/3 "2022-01-10T15:46:52Z")

</div>

As @mihai said, it saves the model currently inside the `Trainer`. If you call it after `Trainer.train()`, since `load_best_model_at_end` will have reloaded the best model, it will save the best model.

---

<div class="post-metadata">

**Author:** ![mvumvp](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@mvumvp](https://discuss.huggingface.co/u/mvumvp)\
**Post date:** [July 3, 2023, 11:13am UTC](https://discuss.huggingface.co/t/do-trainer-save-model-saves-the-best-model/13296/4 "2023-07-03T11:13:45Z")

</div>

what is the source code to find the best model? Is best\_model defined based on the minimum loss of a checkpoint or just the latest checkpoint?
