<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rotimi Ajigboye</title>
    <description>The latest articles on DEV Community by Rotimi Ajigboye (@rotimi_ajigboye).</description>
    <link>https://dev.to/rotimi_ajigboye</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3392161%2Fed64f726-df12-4d78-b95f-1fb81090f5f1.jpeg</url>
      <title>DEV Community: Rotimi Ajigboye</title>
      <link>https://dev.to/rotimi_ajigboye</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rotimi_ajigboye"/>
    <language>en</language>
    <item>
      <title>Perplexity as a determinant of text quality.</title>
      <dc:creator>Rotimi Ajigboye</dc:creator>
      <pubDate>Tue, 29 Jul 2025 21:43:59 +0000</pubDate>
      <link>https://dev.to/rotimi_ajigboye/perplexity-as-a-determinant-of-text-quality-2i5b</link>
      <guid>https://dev.to/rotimi_ajigboye/perplexity-as-a-determinant-of-text-quality-2i5b</guid>
      <description>&lt;p&gt;In Natural Language Processing, perplexity is a measure of how well a language model predicts a text sequence starting from the first token in the sequence. &lt;/p&gt;

&lt;p&gt;Consider a Text sequence that starts with "Elephants". After the word "Elephants" there are a ton of possible options for the next word in the sequence. A few examples are below:&lt;/p&gt;

&lt;p&gt;Elephants are...&lt;br&gt;
Elephants do...&lt;br&gt;
Elephants eat...&lt;br&gt;
Elephants weigh...&lt;/p&gt;

&lt;p&gt;Based on the data the language model has been trained with, each possible next word such as "are", "do", "eat", "weigh" has a probability assigned to it as the next word in the sequence. The higher the probability of this word, the more confident the language model is in that word being the next in the sequence.&lt;/p&gt;

&lt;p&gt;Now let us assume that a piece of text begins with "Elephants eat". Again there are tons of possible next words such as "grass", "vegetables", "meat". Each option has a probability assigned to it and obviously "meat" would be less probable than "Grass" and "Vegetables".&lt;/p&gt;

&lt;p&gt;Elephants Eat Grass - Most Probable&lt;br&gt;
Elephants Eat Vegetables - Second Most Probable&lt;br&gt;
Elephants Eat Meat - Least Probable&lt;/p&gt;

&lt;p&gt;Simply put, perplexity is a measure of the overall probability of a text sequence. Mathematically it is calculated as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcy80mlkgznikhrkpqfr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcy80mlkgznikhrkpqfr4.png" alt="Mathematical Calculation of Perplexity" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This shows an inverse relationship between perplexity and sequence probabilities, indicating that the more probable a sequence is, the less confusing it is to the model. The lower the perplexity of a text sequence, the more confident the model is in its prediction of every token in the sequence.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Calculating Perplexity of texts in python
&lt;/h2&gt;

&lt;p&gt;Import the required libraries&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initiate your language model and its corresponding tokenizer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define a function to calculate perplexity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def Perplexity_Calc(text):
    input_ids = tokenizer.encode(text, return_tensors="pt", truncation=True, max_length=1024)
    with torch.no_grad():
        outputs = model(input_ids, labels=input_ids)
        loss = outputs.loss
        perplexity = torch.exp(loss)
        print(f"Perplexity score: {perplexity.item()}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define your piece of text&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;text = "The quick brown fox jumps over the lazy dog"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calculate Perplexity of the defined text&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Perplexity_Calc(text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgp9ys9r3cucmgytx8f8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgp9ys9r3cucmgytx8f8.png" alt="Examples" width="451" height="267"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Cases
&lt;/h2&gt;

&lt;p&gt;Now that we understand what perplexity is, how can it be used in practice?&lt;/p&gt;

&lt;p&gt;One interesting way I have used perplexity is as a selection criterion in an ensemble Optical Character Recognition (OCR) system.&lt;/p&gt;

&lt;p&gt;There are numerous OCR systems out there, each with its own strengths and weaknesses. Some OCR systems perform exceptionally well on clean, high-resolution scanned documents with structured layouts and consistent fonts. Others are designed to handle more complex or variable inputs, such as handwritten notes, forms, or low-quality images, where text may be irregular, skewed, or overlapping with other elements. Each OCR model tends to have its own biases and limitations depending on the type of training data and preprocessing used.&lt;/p&gt;

&lt;p&gt;In the example below, the same image is fed to four different OCR models and different results are obtained from each of the models.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssc48lb97j5490uye9ti.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssc48lb97j5490uye9ti.jpg" alt="Performance of four different OCR models on the same image" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the results it is clear that Azure Cognitive Services and AWS textract both produced the best results while Tesseract and Google vision performed poorly.&lt;/p&gt;

&lt;p&gt;Rather than relying on a single OCR model which could perform poorly in certain scenarios, I built an ensemble system that takes outputs from four different OCR models and uses perplexity to decide which one is likely the most accurate. Normally, evaluating the accuracy of OCR outputs requires comparing the outputs to the known original text. By using perplexity, the system can estimate which output makes the most sense linguistically, without ever seeing or knowing the ground truth (correct text). It simply picks the output with the lowest perplexity as the final result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8trxqoev3evz3g932ks7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8trxqoev3evz3g932ks7.jpg" alt="Model Architecture" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The outcome of this is an ensemble that produces better results over a diverse set of input images, than any of the individual models could achieve on their own.&lt;/p&gt;

&lt;p&gt;So that's one way I've used perplexity. How would you use perplexity in your own projects?&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
