# Trainer log my custom metrics at training step

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-log-my-custom-metrics-at-training-step%2F55204%3E%5C
**Category:** Beginners\
**Created:** [September 16, 2023, 4:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-log-my-custom-metrics-at-training-step%2F55204 "2023-09-16T04:41:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bossalex](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bossalex/32/19486_2.png) [@bossalex](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fbossalex)%5C
**Post date:** [September 16, 2023, 4:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-log-my-custom-metrics-at-training-step%2F55204%2F1 "2023-09-16T04:41:38Z")

</div>

Hey there.  
I’m using the Huggingface Trainer to finetune my model, and use tensorboard to display the mertics.  
I find that the trainer only logs the train\_loss which is return by the model\_ouput. However, I wonder if there is a way for me to have more information logged during the train\_step, such as my own loss which is part the trian\_loss.  
I check the trainer code

```auto
    def _maybe_log_save_evaluate(self, tr_loss, model, trial, epoch, ignore_keys_for_eval):
        if self.control.should_log:
            if is_torch_tpu_available():
                xm.mark_step()

            logs: Dict[str, float] = {}

            # all_gather + mean() to get average loss over all processes
            tr_loss_scalar = self._nested_gather(tr_loss).mean().item()

            # reset tr_loss to zero
            tr_loss -= tr_loss

            logs["loss"] = round(tr_loss_scalar / (self.state.global_step - self._globalstep_last_logged), 4)
            logs["learning_rate"] = self._get_learning_rate()

            self._total_loss_scalar += tr_loss_scalar
            self._globalstep_last_logged = self.state.global_step
            self.store_flos()

            self.log(logs)

```

I think this code has defined the log metrics which I may not have the change to customize my own metric during the training step.  
I would like to know if there are some ways to log.

Cheers & Thanks for an tips!

---

<div class="post-metadata">

**Author:** ![HaohuaLv](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/haohualv/32/22227_2.png) [@HaohuaLv](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FHaohuaLv)%5C
**Post date:** [December 18, 2023, 5:18pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-log-my-custom-metrics-at-training-step%2F55204%2F2 "2023-12-18T17:18:27Z")

</div>

I have the same problem. Have u solve it ?

---

<div class="post-metadata">

**Author:** ![ch272h](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ch272h/32/29655_2.png) [@ch272h](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fch272h)%5C
**Post date:** [January 24, 2024, 9:14pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-log-my-custom-metrics-at-training-step%2F55204%2F3 "2024-01-24T21:14:20Z")

</div>

If you don’t use gradient accumulation, then I usually just hack by overwriting `Trainer.compute_loss` and tucking in one line of `self.log(compute_my_metric(output)`

If you use gradient accumulation, one alternative is to trigger a CustomCallback per [Metrics for Training Set in Trainer - #7 by Kaveri](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmetrics-for-training-set-in-trainer%2F2461%2F7). For example, you can do one forward pass on the entire train set on\_epoch\_end or on\_evaluate. It will be repeated work, slow and coarse.

And let me know if you figured out an easy way to log custom loss!

---

<div class="post-metadata">

**Author:** ![zipzou](https://avatars.discourse-cdn.com/v4/letter/z/a587f6/32.png) [@zipzou](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fzipzou)%5C
**Post date:** [July 11, 2024, 2:19am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-log-my-custom-metrics-at-training-step%2F55204%2F4 "2024-07-11T02:19:23Z")

</div>

[https://github.com/zipzou/hf-multitask-trainer](https://github.com/zipzou/hf-multitask-trainer)  
You can try to use this to track different losses.
