# Loading a model from local with best checkpoint

**URL:** <https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707>\
**Category:** Beginners\
**Created:** [October 23, 2020, 11:24am UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707 "2020-10-23T11:24:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![berkayberabi](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@berkayberabi](https://discuss.huggingface.co/u/berkayberabi)\
**Post date:** [October 23, 2020, 11:24am UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/1 "2020-10-23T11:24:54Z")

</div>

Hi all,

I have trained a model and saved it, tokenizer as well. During the training I set the load\_best\_checkpoint\_at\_end to True and can see the test results, which are good

Now I have another file where I load the model and observe results on test data set. I want to be able to do this without training over and over again. But the test results in the second file where I load the model are worse than the ones right after training.

Is there a way to load the model with best validation checkpoint ?

This is how I save:

tokenizer.save\_pretrained(model\_directory)  
trainer.save\_model()

and this is how i load:

```
tokenizer = T5Tokenizer.from_pretrained(model_directory)
model = T5ForConditionalGeneration.from_pretrained(model_directory, return_dict=False)
```

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](https://discuss.huggingface.co/u/valhalla)\
**Post date:** [October 24, 2020, 7:44am UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/2 "2020-10-24T07:44:00Z")

</div>

To load a particular checkpoint, just pass the path to the checkpoint-dir which would load the model from that checkpoint.

---

<div class="post-metadata">

**Author:** ![berkayberabi](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@berkayberabi](https://discuss.huggingface.co/u/berkayberabi)\
**Post date:** [October 24, 2020, 3:40pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/3 "2020-10-24T15:40:07Z")

</div>

Yes but I do not know apriori which checkpoint is the best. I trained the model on another file and saved some of the checkpoints. Yes, I can track down the best checkpoint in the first file but it is not an optimal solution.

I believe that an ideal solution would be to only save the best checkpoint, or overwrite the existing checkpoint when model improves so that in the end I only have one model actually.

Is what I want more clear now?

Is there a way to save only the best checkpoint instead of many?

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](https://discuss.huggingface.co/u/valhalla)\
**Post date:** [October 29, 2020, 5:29pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/4 "2020-10-29T17:29:05Z")

</div>

I am not sure if that’s possible with `Trainer` right now, pinging @sgugger

---

<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:** [October 30, 2020, 4:26pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/5 "2020-10-30T16:26:54Z")

</div>

I don’t understand the question. With `load_best_model_at_end` the model loaded at the end of training is the one that had the best performance on your validation set. So when you save that model, you have the best model on this validation set.

If it’s crap on another set, it means your validation set was not representative of the performance you wanted and there is nothing we can do on `Trainer` to fix that.

---

<div class="post-metadata">

**Author:** ![berkayberabi](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@berkayberabi](https://discuss.huggingface.co/u/berkayberabi)\
**Post date:** [January 12, 2021, 3:08pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/6 "2021-01-12T15:08:05Z")

</div>

I understand. This means that the last saved checkpoint is the checkpoint with the best validation score instead of the final weights if I am saving multiple checkpoints.

Am I right?

---

<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 12, 2021, 3:10pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/7 "2021-01-12T15:10:36Z")

</div>

Yes, that’s what `load_best_model_at_end=True` does.

---

<div class="post-metadata">

**Author:** ![berkayberabi](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@berkayberabi](https://discuss.huggingface.co/u/berkayberabi)\
**Post date:** [January 12, 2021, 3:12pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/8 "2021-01-12T15:12:26Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![johnrobinsn](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/johnrobinsn/32/15081_2.png) [@johnrobinsn](https://discuss.huggingface.co/u/johnrobinsn)\
**Post date:** [March 31, 2023, 1:22pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/9 "2023-03-31T13:22:51Z")

</div>

What if you kill training midway through? Is there an easy way to ask the trainer to load the best model determined up to that point?

---

<div class="post-metadata">

**Author:** ![suzyahyah](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@suzyahyah](https://discuss.huggingface.co/u/suzyahyah)\
**Post date:** [April 4, 2023, 2:02pm UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/10 "2023-04-04T14:02:38Z")

</div>

I’m currently doing

```auto
import os
from transformers.trainer_callback import TrainerState

save_dir = "your_trainer_save_directory"
ckpt_dirs = os.listdir(save_dir)
ckpt_dirs = sorted(ckpt_dirs, key=lambda x: int(x.split('-')[1])
last_ckpt = ckpt_dirs[-1]

state = TrainerState.load_from_json(f"{save_dir}/{last_ckpt}/trainer_state.json")

print(state.best_model_checkpoint) # your best ckpoint.

```

If the above code breaks or doesnt work because of an API change/versioning, you could try tracing the github code starting from the equivalent of `training_args.load_best_model_at_end` to see how the best model checkpoint directory is called.

---

<div class="post-metadata">

**Author:** ![frankl1](https://avatars.discourse-cdn.com/v4/letter/f/8e8cbc/32.png) [@frankl1](https://discuss.huggingface.co/u/frankl1)\
**Post date:** [September 24, 2023, 5:37am UTC](https://discuss.huggingface.co/t/loading-a-model-from-local-with-best-checkpoint/1707/11 "2023-09-24T05:37:38Z")

</div>

The parameter `save_total_limit` of the TrainingArguments object can be set to `1` in order to save only the best checkpoint.

Note that the documentation says that when the best checkout and the last one are different from each other, both could be kept at the end. However, I have not seen this scenario so far.
