# Trainer gives error after 1st epoch and evaluation

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-gives-error-after-1st-epoch-and-evaluation%2F4006%3E%5C
**Category:** 🤗Transformers\
**Created:** [February 25, 2021, 1:22pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-gives-error-after-1st-epoch-and-evaluation%2F4006 "2021-02-25T13:22:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![theudster](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@theudster](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ftheudster)%5C
**Post date:** [February 25, 2021, 1:22pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-gives-error-after-1st-epoch-and-evaluation%2F4006%2F1 "2021-02-25T13:22:13Z")

</div>

Hi I have managed to get my code working for training, yet after 1 epoch I am getting weird results for the evaluation and also an error

```
Trainer is attempting to log a value of "[0. 0. 0. 1. 0.]" of type 
<class 'numpy.ndarray'> for key "eval/recall" as a scalar. This 
invocation of Tensorboard's writer.add_scalar() is incorrect 
so we dropped this attribute.

```

and then an error

```
TypeError: Object of type ndarray is not JSON serializable

```

I used the compute\_metrics (measure accuracy, f1, precision and accuracy) provided by the tutorial and I am doing a classification task with 5 task.

---

<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:** [February 25, 2021, 1:44pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-gives-error-after-1st-epoch-and-evaluation%2F4006%2F2 "2021-02-25T13:44:32Z")

</div>

> I used the compute\_metrics (measure accuracy, f1, precision and accuracy) provided by the tutorial and I am doing a classification task with 5 task.

Could you tell us which tutorial? From the error message, it seems your metric value is a NumPy array, so sharing your `compute_metric` function would also help.

---

<div class="post-metadata">

**Author:** ![theudster](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@theudster](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ftheudster)%5C
**Post date:** [February 25, 2021, 2:32pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-gives-error-after-1st-epoch-and-evaluation%2F4006%2F3 "2021-02-25T14:32:58Z")

</div>

Of course. it is from the [Training and Finetuning](https://huggingface.co/transformers/training.html?highlight=compute_metrics) tutorial and this is my code:

```
# Metrics
from sklearn.metrics import accuracy_score, precision_recall_fscore_support

def compute_metrics(pred):
    labels = pred.label_ids
    preds = pred.predictions.argmax(-1)
   precision, recall, f1, _ = precision_recall_fscore_support(labels, preds)
   acc = accuracy_score(labels, preds)
   return {
        'accuracy': acc,
        'f1': f1,
        'precision': precision,
       'recall': recall
    }
```

---

<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:** [February 25, 2021, 2:37pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-gives-error-after-1st-epoch-and-evaluation%2F4006%2F4 "2021-02-25T14:37:56Z")

</div>

`precision_recall_fscore_support` requires an argument average to return simple numbers (and not arrays). See the [doc](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_fscore_support.html) to choose the one that suits your need here.

---

<div class="post-metadata">

**Author:** ![jxm](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jxm/32/29614_2.png) [@jxm](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjxm)%5C
**Post date:** [June 2, 2023, 2:40pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-gives-error-after-1st-epoch-and-evaluation%2F4006%2F5 "2023-06-02T14:40:55Z")

</div>

I ran into an issue like this when I inadvertently logged a metric in the trainer that was a Tensor instead of just a float. I did something like `average_scores.mean()`, which returned a Tensor object, which was somehow affixed to `Trainer.state` and then broke json-saving. I changed the metric computation to `average_scores.mean().item()` and this fixed the issue.
