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

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdo-trainer-save-model-saves-the-best-model%2F13296%3E%5C
**Category:** 🤗Transformers\
**Created:** [January 2, 2022, 10:00am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdo-trainer-save-model-saves-the-best-model%2F13296 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FGladiator)%5C
**Post date:** [January 2, 2022, 10:00am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdo-trainer-save-model-saves-the-best-model%2F13296%2F1 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmihai)%5C
**Post date:** [January 3, 2022, 8:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdo-trainer-save-model-saves-the-best-model%2F13296%2F2 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [January 10, 2022, 3:46pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdo-trainer-save-model-saves-the-best-model%2F13296%2F3 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmvumvp)%5C
**Post date:** [July 3, 2023, 11:13am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdo-trainer-save-model-saves-the-best-model%2F13296%2F4 "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?
