请解释一下Perplexity
- User
请解释一下Perplexity
- Assistant
Perplexity(困惑度)是什么?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
请解释一下Perplexity
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
数学定义
设文本串 𝐖 = w₁, w₂, …, w_N ,模型给出的条件概率为 P(w_i | w_{<i})。
则困惑度
其中 H(𝐖) 是以 2 为底的平均交叉熵。可直观理解为
❝模型在任何一步平均有 PP 个「等概率候选」在脑海里❞。
直观类比
• PP = 1 → 模型“百分百确信”下一个词是什么。
• PP = 10 → 相当于每一步在 10 个选项中随机挑 1 个,整体连贯度合理。
• PP = 100+ → 模型一脸茫然,基本是瞎猜。
与交叉熵的关系
CrossEntropy = −(1/N) Σ log P
Perplexity = exp(CrossEntropy)
所以两者只是 log vs exp 的换算;用哪个主要看习惯。
核心假设
若你用 GPT-2/GPT-3… 作为「评估模型」,那么同款模型写的文本往往更符合它自己的概率分布 → PP 值显著偏低;而人类写作由于用词多样、长尾表达多 → PP 更高。
检测流程(简化版)
输入文本 → 用基准 LM 逐 token 计算概率 → 得到 PP
↘ 与经验阈值比较(如 PP < 40 ⇒ AI 概率高)
可以再结合句子长度、burstiness(爆发度)做二元分类。
真实效果
• 英文 ≥500 词、基准模型≈生成模型时,AUC 可达 0.9 左右。
• 不同模型或轻度改写后,曲线明显下滑;PP 可能回到正常人范围。
from transformers import GPT2LMHeadModel, GPT2TokenizerFast
import torch, math
text = "Artificial intelligence is transforming the world."
model_id = "gpt2"
tok = GPT2TokenizerFast.from_pretrained(model_id)
model = GPT2LMHeadModel.from_pretrained(model_id).eval()
input_ids = tok(text, return_tensors="pt").input_ids
with torch.no_grad():
logits = model(input_ids).logits[:, :-1] # 预测每个位置
probs = torch.softmax(logits, dim=-1)
tgt = input_ids[:, 1:] # 真实下一个 token
p_next = probs.gather(-1, tgt.unsqueeze(-1)).squeeze(-1)
log_p = torch.log(p_next).sum()
pp = math.exp(-log_p / (tgt.numel()))
print("Perplexity =", pp)
| 误区 | 说明 |
|---|---|
| PP 越低越好? | 仅对“同域、同分词方案”的模型评测成立。人类稿件 PP 高并不代表差,只是风格不同。 |
| 不同语言通用阈值? | 中文分词粒度、英文 BPE 长度都会影响 PP 数值,阈值需单独校准。 |
| 可以 100% 检出? | 任何 paraphrase / 低温度+人工润色 都可能把 PP 拉高,从而逃过检测。 |
Perplexity = “模型觉得这段话好预测的程度”。
• 研究/测评:用 PP 较方便比较模型优劣。
• AI 检测:PP“异常低”可作为红旗,但需结合其它特征与人工复核。 🚦