# Using perplexity as metric during training

**URL:** <https://discuss.huggingface.co/t/using-perplexity-as-metric-during-training/42354>\
**Category:** Beginners\
**Created:** [June 7, 2023, 6:52am UTC](https://discuss.huggingface.co/t/using-perplexity-as-metric-during-training/42354 "2023-06-07T06:52:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirix](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mirix/32/16327_2.png) [@mirix](https://discuss.huggingface.co/u/mirix)\
**Post date:** [June 7, 2023, 6:52am UTC](https://discuss.huggingface.co/t/using-perplexity-as-metric-during-training/42354/1 "2023-06-07T06:52:37Z")

</div>

I have followed the tutorial AutoModelForSequenceClassification for fine-tuning using accuracy and other metrics and all went fine.

Now I am following the tutorial AutoModelForMaskedLM for domain adaptation and everything is good.

However, I am struggling to use a metric in the same way that I did before, so that it is reported after each epoch.

Of course, the following works, but it is only reported before and after training:

```auto
eval_results = trainer.evaluate()
print(f">>> Perplexity: {math.exp(eval_results['eval_loss']):.2f}")

```

However, the following training argument does not:

`	#metric_for_best_model='perplexity',`

I have also tried the following training argument:

`	#compute_metrics=compute_metrics,`

The question is, how do I define the function?

```auto
#def compute_metrics(eval_results):
#	return math.exp(eval_results['eval_loss'])

```

That does not work either.

Best,

Ed
