# Change saving metric in Trainer

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fchange-saving-metric-in-trainer%2F35673%3E%5C
**Category:** Intermediate\
**Created:** [April 5, 2023, 3:55pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fchange-saving-metric-in-trainer%2F35673 "2023-04-05T15:55:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgavela](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@sgavela](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgavela)%5C
**Post date:** [April 5, 2023, 3:55pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fchange-saving-metric-in-trainer%2F35673%2F1 "2023-04-05T15:55:33Z")

</div>

I am training a model while performing hyperparameter optimization with optuna, therefore I can’t afford saving a lot of checkpoints in each trial because my machine would run out of disk. For this reason in each trial I am setting `save_total_limit=2`. This means that the two checkpoints with less validation loss will be saved for each trial. How can I do if I want to save the two checkpoints which have best f1 instead of the least validation loss? Is there any parameter or I have to inherit Trainer and make my own code?

---

<div class="post-metadata">

**Author:** ![Guldeniz](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/guldeniz/32/16355_2.png) [@Guldeniz](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FGuldeniz)%5C
**Post date:** [May 26, 2023, 12:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fchange-saving-metric-in-trainer%2F35673%2F2 "2023-05-26T12:19:29Z")

</div>

I don’t know if there is a way to do that but there is `load_best_model_at_end=True`. But I’m not quite sure if it is something you want. Maybe see here: [Save only best model in Trainer - #5 by sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsave-only-best-model-in-trainer%2F8442%2F5)

---

<div class="post-metadata">

**Author:** ![Sakuna](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sakuna/32/27100_2.png) [@Sakuna](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSakuna)%5C
**Post date:** [May 18, 2024, 12:21am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fchange-saving-metric-in-trainer%2F35673%2F3 "2024-05-18T00:21:49Z")

</div>

Hi,

You can use the argument `metric_for_best_model` in the `Trainer` to define what metric to use when choosing the best checkpoint. Do not forget to set the `greater_is_better` parameter, too, depending on your metric. Since you are using the F1 score, it should be `True`. Hope this helps!
