Perplexity
Perplexity
A measure of a language model's predictive uncertainty. Lower is better.
Perplexity
A measure of a language model's predictive uncertainty. Lower is better.
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.
# 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}")
As of January 2025 - Perplexity AI service pricing plans
| Plan | Price | Key features |
|---|---|---|
| Free | $0 | Basic 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/month | Team features, SSO, management tools |
| Enterprise Max | $325/user/month | Enterprise security, compliance |
API pricing: Sonar model $1/1M tokens (input and output), Search API $5/1,000 requests
"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."
"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."
"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
If the vocabulary size and tokenization method differ, PPL cannot be compared directly. Compare only under the same dataset and the same tokenizer conditions.
PPL measured on training data comes out low due to overfitting. Always measure it on a separate test set.
Measure on standard benchmarks (WikiText, PTB, etc.) and compare with models under the same conditions. Also evaluate downstream task performance.