# How to calculate perplexity properly

**URL:** <https://discuss.huggingface.co/t/how-to-calculate-perplexity-properly/11121>\
**Category:** Beginners\
**Created:** [October 27, 2021, 12:26pm UTC](https://discuss.huggingface.co/t/how-to-calculate-perplexity-properly/11121 "2021-10-27T12:26:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felipehonorato](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/felipehonorato/32/48418_2.png) [@Felipehonorato](https://discuss.huggingface.co/u/Felipehonorato)\
**Post date:** [October 27, 2021, 12:26pm UTC](https://discuss.huggingface.co/t/how-to-calculate-perplexity-properly/11121/1 "2021-10-27T12:26:58Z")

</div>

Hey guys, i’m trying to evaluate my model through it’s perplexity on my test set and started to read this guide: [Perplexity of fixed-length models — transformers 4.11.3 documentation](https://huggingface.co/transformers/perplexity.html#example-calculating-perplexity-with-gpt-2-in-transformers)

However, i don’t understand why joining our texts like this would not damage my models predictions:

```auto
from datasets import load_dataset
test = load_dataset('wikitext', 'wikitext-2-raw-v1', split='test')
encodings = tokenizer('\n\n'.join(test['text']), return_tensors='pt')

```

How can my model predict properly if my contexts are mixed up?  
I’m dealing with short sentences tho

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](https://discuss.huggingface.co/u/BramVanroy)\
**Post date:** [October 27, 2021, 12:38pm UTC](https://discuss.huggingface.co/t/how-to-calculate-perplexity-properly/11121/2 "2021-10-27T12:38:13Z")

</div>

It allows the model to generalize across sentence or document boundaries, which is typically what you want in generative models. This is not a requirement, by the way, but combining it with a strided window this is quite powerful.

---

<div class="post-metadata">

**Author:** ![Felipehonorato](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/felipehonorato/32/48418_2.png) [@Felipehonorato](https://discuss.huggingface.co/u/Felipehonorato)\
**Post date:** [October 27, 2021, 1:17pm UTC](https://discuss.huggingface.co/t/how-to-calculate-perplexity-properly/11121/3 "2021-10-27T13:17:51Z")

</div>

Hey Bram! Thanks for your reply. I got it.
