# Log multiple metrics while training

**URL:** <https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115>\
**Category:** 🤗Datasets\
**Created:** [July 7, 2021, 12:25am UTC](https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115 "2021-07-07T00:25:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![muralidandu](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@muralidandu](https://discuss.huggingface.co/u/muralidandu)\
**Post date:** [July 7, 2021, 12:25am UTC](https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115/1 "2021-07-07T00:25:46Z")

</div>

Hi, I am fine-tuning a classification model and would like to log accuracy, precision, recall and F1 using Trainer API.

While I am using `metric = load_metric("glue", "mrpc")` it logs accuracy and F1, but when I am using `metric = load_metric("precision", "recall", "f1")` it only logs the first metric.

Is it by design? Do I need to write a custom script if I want to log all these metrics by epochs/steps using Trainer API?

---

<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:** [July 7, 2021, 12:24pm UTC](https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115/2 "2021-07-07T12:24:06Z")

</div>

You need to load each of those metrics separately, I don’t think the loader accepts a list.

---

<div class="post-metadata">

**Author:** ![muralidandu](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@muralidandu](https://discuss.huggingface.co/u/muralidandu)\
**Post date:** [July 7, 2021, 5:45pm UTC](https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115/3 "2021-07-07T17:45:23Z")

</div>

Can you please elaborate on your first statement. I tried these two variations - First one returns only `recall` and second one throws and error saying `tuple is not accepted`.

```auto
def compute_metrics(eval_pred):
    # metric = load_metric("glue", "mrpc")
    metric = load_metric("precision")
    metric = load_metric("recall")
    
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return metric.compute(predictions=predictions, references=labels)

```

```auto
def compute_metrics(eval_pred):
    # metric = load_metric("glue", "mrpc")
    metric1 = load_metric("precision")
    metric2 = load_metric("recall")
    
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return metric1.compute(predictions=predictions, references=labels), metric2.compute(predictions=predictions, references=labels)

```

---

<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:** [July 7, 2021, 5:54pm UTC](https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115/4 "2021-07-07T17:54:28Z")

</div>

You need to return a dictionary with the metrics you want (please checkout the [course](https://huggingface.co/course/chapter3/3?fw=pt) on the `Trainer` and the Trainer video) so in this case:

```auto
def compute_metrics(eval_pred):
    metric1 = load_metric("precision")
    metric2 = load_metric("recall")
    
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    precision = metric1.compute(predictions=predictions, references=labels)["precision"]
    recall = metric2.compute(predictions=predictions, references=labels)["recall"]
    return {"precision": precision, "recall": recall}

```

---

<div class="post-metadata">

**Author:** ![muralidandu](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@muralidandu](https://discuss.huggingface.co/u/muralidandu)\
**Post date:** [July 7, 2021, 6:18pm UTC](https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115/5 "2021-07-07T18:18:11Z")

</div>

Awesome 🙂 I completed the course recently and missed that it returns a dictionary. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![ashutoshsaboo](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ashutoshsaboo/32/4183_2.png) [@ashutoshsaboo](https://discuss.huggingface.co/u/ashutoshsaboo)\
**Post date:** [March 15, 2022, 8:07am UTC](https://discuss.huggingface.co/t/log-multiple-metrics-while-training/8115/6 "2022-03-15T08:07:13Z")

</div>

Hi @sgugger , defining a custom compute\_metrics is fine - but how do you call add\_batch in each mini-batch within a single epoch when you want to log & compute multiple metrics towards the end of epoch (such as in the above case both precision and recall)? Could you please give an example of the same if possible?
