# \[trainer\] 'train\_loss' different from 'loss'

**URL:** <https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403>\
**Category:** 🤗Transformers\
**Created:** [January 5, 2022, 7:49pm UTC](https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403 "2022-01-05T19:49:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![katze](https://avatars.discourse-cdn.com/v4/letter/k/5e9695/32.png) [@katze](https://discuss.huggingface.co/u/katze)\
**Post date:** [January 5, 2022, 7:49pm UTC](https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403/1 "2022-01-05T19:49:59Z")

</div>

Hi all,

I am using the Trainer and training GPT2 from scratch. I have trained it for 50 epochs and during training I had logs like the one shown below:

`{'loss': 6.513, 'learning_rate': 1.1749500646222535e-07, 'epoch': 49.99}`

However, after the last epoch I get a log with some train metrics:

```auto

 *****train metrics*****
epoch = 50.0
train_loss = 0.0084
train_runtime = 0:12:31.27
train_samples = 8716143
train_samples_per_second = 580087.323
train_steps_per_second = 566.434

```

Notice that these values are significantly different (6.5 vs. 0.0084). If the last loss is the real training loss, then what losses were the logs outputting during training?

Thanks

---

<div class="post-metadata">

**Author:** ![asfhgj](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@asfhgj](https://discuss.huggingface.co/u/asfhgj)\
**Post date:** [June 29, 2022, 4:35pm UTC](https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403/2 "2022-06-29T16:35:08Z")

</div>

Hi! I just had this exact question while doing my own training. Did you ever find out the answer?

---

<div class="post-metadata">

**Author:** ![Daromog](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/daromog/32/35113_2.png) [@Daromog](https://discuss.huggingface.co/u/Daromog)\
**Post date:** [August 18, 2022, 10:50pm UTC](https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403/3 "2022-08-18T22:50:51Z")

</div>

I have the same problem , did you find the answer???

---

<div class="post-metadata">

**Author:** ![OrTag](https://avatars.discourse-cdn.com/v4/letter/o/db5fbb/32.png) [@OrTag](https://discuss.huggingface.co/u/OrTag)\
**Post date:** [March 26, 2023, 5:09pm UTC](https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403/4 "2023-03-26T17:09:41Z")

</div>

For future people coming with the same question:  
The final result is the average of all the losses.

---

<div class="post-metadata">

**Author:** ![pyjhzwh](https://avatars.discourse-cdn.com/v4/letter/p/4491bb/32.png) [@pyjhzwh](https://discuss.huggingface.co/u/pyjhzwh)\
**Post date:** [March 31, 2023, 1:02am UTC](https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403/5 "2023-03-31T01:02:38Z")

</div>

According to [trainer.py](https://github.com/huggingface/transformers/blob/v4.27.2/src/transformers/trainer.py#L2025) the ‘train\_loss’ in the metric is the average loss across all steps. The ‘loss’ at each logging step is the average loss from the previous logging step to current logging step.  
The significant difference of ‘train\_loss’ and ‘loss’ is probably because you resume training from a checkpoint where the “self.\_total\_loss\_scalar” is cleared (not stored in the checkpoint), but the `self.state.global_step` is still correct. Therefore, using  
`train_loss = self._total_loss_scalar / self.state.global_step` to get`train_loss` resuming from a checkpoint will give you a value (e.g. 0.0084) way less than the real average loss.

---

<div class="post-metadata">

**Author:** ![Anshulky](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/anshulky/32/52539_2.png) [@Anshulky](https://discuss.huggingface.co/u/Anshulky)\
**Post date:** [August 13, 2025, 10:21am UTC](https://discuss.huggingface.co/t/trainer-train-loss-different-from-loss/13403/6 "2025-08-13T10:21:28Z")

</div>

How can I log train\_loss instead of loss? I tried setting eval\_strategy=“epoch”; however, I still think the logged loss is of the last iter.
