# How to define the compute\_metrics() function in Trainer?

**URL:** <https://discuss.huggingface.co/t/how-to-define-the-compute-metrics-function-in-trainer/12953>\
**Category:** 🤗Transformers\
**Created:** [December 19, 2021, 2:41am UTC](https://discuss.huggingface.co/t/how-to-define-the-compute-metrics-function-in-trainer/12953 "2021-12-19T02:41:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![olaffson](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/olaffson/32/2416_2.png) [@olaffson](https://discuss.huggingface.co/u/olaffson)\
**Post date:** [December 19, 2021, 2:41am UTC](https://discuss.huggingface.co/t/how-to-define-the-compute-metrics-function-in-trainer/12953/1 "2021-12-19T02:41:12Z")

</div>

Hello,

Coming from `tensorflow` I am a bit confused as to how to properly define the compute\_metrics() in Trainer. For instance, I see in the notebooks various possibilities

```auto
def compute_metrics(eval_pred):
    predictions, labels = eval_pred
    predictions = predictions[:, 0]
    return metric.compute(predictions=predictions, references=labels)

```

My question may seem stupid (maybe it is) but how can I know how to compute the metrics if I cannot see what `eval_pred` looks like in Trainer? It is as if I had to guess what the output will be before actually training the model. Am I missing something here?

Thanks!

---

<div class="post-metadata">

**Author:** ![merve](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/merve/32/49809_2.png) [@merve](https://discuss.huggingface.co/u/merve)\
**Post date:** [December 20, 2021, 10:44am UTC](https://discuss.huggingface.co/t/how-to-define-the-compute-metrics-function-in-trainer/12953/2 "2021-12-20T10:44:00Z")

</div>

Hello, I’m a bit confused about your question. Are you trying to implement this on Trainer or TensorFlow?

---

<div class="post-metadata">

**Author:** ![olaffson](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/olaffson/32/2416_2.png) [@olaffson](https://discuss.huggingface.co/u/olaffson)\
**Post date:** [December 20, 2021, 1:23pm UTC](https://discuss.huggingface.co/t/how-to-define-the-compute-metrics-function-in-trainer/12953/3 "2021-12-20T13:23:07Z")

</div>

hello @merve, thanks. I am trying to write the correct Trainer (in pytorch). The point is that I can follow the tutorial and copy-paste the compute\_metrics functions there. However, if I want to modify them I need to understand and play a little bit with the `eval_pred` output. How can I get `eval_pred` from a `trainer` so that I can see what `predictions` and `labels` look like?

---

<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:** [December 20, 2021, 1:57pm UTC](https://discuss.huggingface.co/t/how-to-define-the-compute-metrics-function-in-trainer/12953/4 "2021-12-20T13:57:10Z")

</div>

Just run `trainer.predict` on your eval/test dataset.
