๐Ÿค– AI/ML

Perplexity

Perplexity

A measure of a language model's predictive uncertainty. Lower is better.

๐Ÿ“– Detailed Explanation

Perplexity is used in two senses. First, it is an evaluation metric that measures the predictive ability of a language model. It indicates how "perplexed" the model is by the next token, and a lower value means a better model. Second, it refers to Perplexity AI, an AI search engine company.

As an evaluation metric, Perplexity originates from information theory. Mathematically, it is defined as the exponential of the cross-entropy. GPT-2 achieved a PPL of 17.48 on WikiText-103, and GPT-3 improved on this significantly. Perplexity AI was founded in 2022 and provides an LLM-based search service.

The core principle of the Perplexity metric is to measure the difference between the probability distribution predicted by the model and the actual distribution. It is calculated as PPL = 2^(H(p,q)), where H(p,q) is the cross-entropy. A perfect prediction has PPL=1, and a uniform distribution prediction with a vocabulary size of V has PPL=V.

In practice, Perplexity is used to compare language models, evaluate fine-tuning, and determine whether domain adaptation has succeeded. The Perplexity AI service, which provides AI search using GPT-4, Claude, and others, is drawing attention as an alternative to existing search engines.

๐Ÿ’ป Code Example

# Perplexity ๊ณ„์‚ฐ ์˜ˆ์ œ
import torch
import math
from transformers import GPT2LMHeadModel, GPT2Tokenizer

# 1. ๋ชจ๋ธ ๋ฐ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model.eval()

def calculate_perplexity(text, model, tokenizer):
    """ํ…์ŠคํŠธ์˜ Perplexity ๊ณ„์‚ฐ"""
    encodings = tokenizer(text, return_tensors="pt")
    input_ids = encodings.input_ids

    with torch.no_grad():
        outputs = model(input_ids, labels=input_ids)
        # Cross-Entropy Loss
        loss = outputs.loss
        # Perplexity = e^(loss)
        perplexity = math.exp(loss.item())

    return perplexity

# 2. ์˜ˆ์ œ ํ…์ŠคํŠธ๋กœ PPL ๊ณ„์‚ฐ
text1 = "The quick brown fox jumps over the lazy dog."
text2 = "Purple elephants dance quantum mechanics pizza."

ppl1 = calculate_perplexity(text1, model, tokenizer)
ppl2 = calculate_perplexity(text2, model, tokenizer)

print(f"์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ์žฅ PPL: {ppl1:.2f}")  # ๋‚ฎ์Œ
print(f"๋น„์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ์žฅ PPL: {ppl2:.2f}")  # ๋†’์Œ

# 3. Hugging Face evaluate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
from evaluate import load
perplexity_metric = load("perplexity", module_type="metric")

# ๋ฐ์ดํ„ฐ์…‹ ์ „์ฒด PPL ๊ณ„์‚ฐ
texts = ["Hello world.", "AI is transforming the world."]
results = perplexity_metric.compute(
    predictions=texts,
    model_id="gpt2"
)
print(f"ํ‰๊ท  PPL: {results['mean_perplexity']:.2f}")

๐Ÿ“Š Performance & Cost

As of January 2025 - Perplexity AI service pricing plans

PlanPriceKey features
Free$0Basic search, with daily limits
Pro$20/month ($200/year)300+ Pro searches/day, GPT-4/Claude access, $5 API credit
Max$200/month ($2,000/year)Unlimited Pro searches, advanced model access
Enterprise Pro$40/user/monthTeam features, SSO, management tools
Enterprise Max$325/user/monthEnterprise security, compliance

API pricing: Sonar model $1/1M tokens (input and output), Search API $5/1,000 requests

๐Ÿ—ฃ๏ธ Say It Like This in Practice

๐Ÿ’ฌ In a Meeting
"After fine-tuning on domain data, Perplexity dropped from 45 to 23. This can be seen as a significant improvement in the model's understanding of that domain."
๐Ÿ’ฌ In an Interview
"Perplexity is the exponential of the cross-entropy loss, and it indicates how many choices the model is deliberating among on average. PPL=10 means that the model is deliberating among about 10 candidates when predicting each token."
๐Ÿ’ฌ In a Technical Discussion
"It is difficult to evaluate model quality with Perplexity alone. A low PPL does not necessarily mean good generation quality, so BLEU, ROUGE, and human evaluation should be used together."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

โš ๏ธ Common Mistakes & Cautions

โŒ
Comparing PPL across models with different tokenizers

If the vocabulary size and tokenization method differ, PPL cannot be compared directly. Compare only under the same dataset and the same tokenizer conditions.

โŒ
Measuring PPL on training data

PPL measured on training data comes out low due to overfitting. Always measure it on a separate test set.

โœ…
The right way

Measure on standard benchmarks (WikiText, PTB, etc.) and compare with models under the same conditions. Also evaluate downstream task performance.

๐Ÿ”— Related Terms

๐Ÿ“š Learn More