# Trainer: Save Checkpoint After Each Epoch

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660%3E%5C
**Category:** 🤗Transformers\
**Created:** [October 20, 2020, 5:12pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660 "2020-10-20T17:12:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agemagician](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@agemagician](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fagemagician)%5C
**Post date:** [October 20, 2020, 5:12pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660%2F1 "2020-10-20T17:12:39Z")

</div>

I am trying to fine-tune a model using Pytorch trainer, however, I couldn’t find an option to save checkpoint after each validation of each epoch.

I could only find “save\_steps” which only save a checkpoint after specific steps, but I validatie the model at the end of each epoch, and I want to store the checkpoint at this point.

Any idea ?

---

<div class="post-metadata">

**Author:** ![vblagoje](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/vblagoje/32/20818_2.png) [@vblagoje](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvblagoje)%5C
**Post date:** [October 20, 2020, 8:43pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660%2F2 "2020-10-20T20:43:36Z")

</div>

Perhaps you could use the Trainer callback mechanism and register handler for on\_epoch\_end.

---

<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:** [October 20, 2020, 9:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660%2F3 "2020-10-20T21:19:16Z")

</div>

If you set the option `load_best_model_at_end` to True, the saves will be done at each evaluation (and the `Trainer` will reload the best model found during the fine-tuning).

---

<div class="post-metadata">

**Author:** ![agemagician](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@agemagician](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fagemagician)%5C
**Post date:** [October 21, 2020, 10:03am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660%2F4 "2020-10-21T10:03:36Z")

</div>

Thanks for the tip.

---

<div class="post-metadata">

**Author:** ![agemagician](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@agemagician](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fagemagician)%5C
**Post date:** [October 21, 2020, 10:04am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660%2F5 "2020-10-21T10:04:02Z")

</div>

Thanks a lot @sgugger.  
This is exactly what I am looking for.

---

<div class="post-metadata">

**Author:** ![nnhwin](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@nnhwin](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnnhwin)%5C
**Post date:** [November 24, 2023, 7:25am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-save-checkpoint-after-each-epoch%2F1660%2F6 "2023-11-24T07:25:25Z")

</div>

@sgugger Hello, I would like to train data batch by batch received from Kafka. then save the chekpoints. If next batch is coming from kafka, want to reload the previous checkpoint to the model and then train again with new arrived data. I do not want do the checkpoint separately for each batch, they always need to recombine with previous checkpoints and train again with new data. Only after all data has been received, I would like to pick the best model (from the lastest checkpoint) and then saved as onnx file. Is it possible to do that way. Thank you in advance and hoping your advice.
