# Error when fine-tuning with the Trainer API

**URL:** <https://discuss.huggingface.co/t/error-when-fine-tuning-with-the-trainer-api/12660>\
**Category:** 🤗Datasets\
**Created:** [December 9, 2021, 11:06am UTC](https://discuss.huggingface.co/t/error-when-fine-tuning-with-the-trainer-api/12660 "2021-12-09T11:06:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lenn](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lenn](https://discuss.huggingface.co/u/Lenn)\
**Post date:** [December 9, 2021, 11:06am UTC](https://discuss.huggingface.co/t/error-when-fine-tuning-with-the-trainer-api/12660/1 "2021-12-09T11:06:03Z")

</div>

Hi,

I’m fine-tuning a model with the Trainer API and following these instructions: [https://huggingface.co/docs/transformers/training#finetuning-in-pytorch-with-the-trainer-api](https://huggingface.co/docs/transformers/training#finetuning-in-pytorch-with-the-trainer-api)

However, after I have defined the compute\_metrics function and tried to run the script, it gave me the following error:

```auto
Traceback (most recent call last):
  File "/home/le/torch_tutorial/lm_1_perpl.py", line 77, in <module>
    trainer.train()
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/transformers/trainer.py", line 1391, in train
    self._maybe_log_save_evaluate(tr_loss, model, trial, epoch, ignore_keys_for_eval)
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/transformers/trainer.py", line 1491, in _maybe_log_save_evaluate
    metrics = self.evaluate(ignore_keys=ignore_keys_for_eval)
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/transformers/trainer.py", line 2113, in evaluate
    output = eval_loop(
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/transformers/trainer.py", line 2354, in evaluation_loop
    metrics = self.compute_metrics(EvalPrediction(predictions=all_preds, label_ids=all_labels))
  File "/home/le/torch_tutorial/lm_1_perpl.py", line 67, in compute_metrics
    return metric.compute(predictions=predictions, references=labels)
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/datasets/metric.py", line 393, in compute
    self.add_batch(predictions=predictions, references=references)
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/datasets/metric.py", line 434, in add_batch
    batch = self.info.features.encode_batch(batch)
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/datasets/features/features.py", line 1049, in encode_batch
    encoded_batch[key] = [encode_nested_example(self[key], obj) for obj in column]
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/datasets/features/features.py", line 1049, in <listcomp>
    encoded_batch[key] = [encode_nested_example(self[key], obj) for obj in column]
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/datasets/features/features.py", line 853, in encode_nested_example
    return schema.encode_example(obj)
  File "/home/le/torch_tutorial/venv/lib/python3.9/site-packages/datasets/features/features.py", line 297, in encode_example
    return int(value)
TypeError: only size-1 arrays can be converted to Python scalars

```

Do you have any ideas on what can be causing it? I have not changed anything in my code except for adding the compute\_metrics function (like in the tutorial) and adding the compute\_metrics argument in Trainer (before this addition everything was working perfectly):

```auto
def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return metric.compute(predictions=predictions, references=labels)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=lm_datasets["train"],
    eval_dataset=lm_datasets["validation"],
    compute_metrics=compute_metrics,
)

```

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [December 9, 2021, 1:39pm UTC](https://discuss.huggingface.co/t/error-when-fine-tuning-with-the-trainer-api/12660/2 "2021-12-09T13:39:08Z")

</div>

What I would do in this case is simply add a print statement within the `compute_metrics` function to view your logits and labels, their shapes, et.

---

<div class="post-metadata">

**Author:** ![Lenn](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lenn](https://discuss.huggingface.co/u/Lenn)\
**Post date:** [December 9, 2021, 3:12pm UTC](https://discuss.huggingface.co/t/error-when-fine-tuning-with-the-trainer-api/12660/3 "2021-12-09T15:12:07Z")

</div>

I did print the shapes of the variables inside of compute\_metrics but they seem to be fine (at least they have the same shape):

```auto
Shape logits: (148, 128, 50265)
Shape labels: (148, 128)
Shape predictions: (148, 128)

```

Or is it something off here?

---

<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 9, 2021, 5:27pm UTC](https://discuss.huggingface.co/t/error-when-fine-tuning-with-the-trainer-api/12660/4 "2021-12-09T17:27:16Z")

</div>

You didn’t tell us which metric you are using so it’s hard to help you without that info. It might expect something flat for instance (if it’s accuracy).

---

<div class="post-metadata">

**Author:** ![Lenn](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lenn](https://discuss.huggingface.co/u/Lenn)\
**Post date:** [December 10, 2021, 8:22am UTC](https://discuss.huggingface.co/t/error-when-fine-tuning-with-the-trainer-api/12660/5 "2021-12-10T08:22:56Z")

</div>

I was trying to use accuracy indeed but did not know that it expects flat values, so thanks!
