MIT License

项目介绍

asahi417/lmppl 是一个托管在 GitHub 上的开源项目,主要使用 Python 编写,采用 MIT License 许可证,目前获得 168 个 Star、15 次 Fork。

仓库全名
asahi417/lmppl
开源许可证
MIT License
Star 数
168
Fork 数
15
未关闭 Issue
6

README 摘要

Language Model Perplexity (LM-PPL) Perplexity measures how predictable a text is by a language model (LM), and it is often used to evaluate fluency or proto-typicality of the text (lower the perplexity is, more fluent or proto-typical the text is). LM-PPL is a python library to calculate perplexity on a text with any types of pre-trained LMs. We compute an ordinary perplexity for recurrent LMs such as GPT3 (Brown et…

完整内容见 GitHub 原始 README 或本页 。

常见问题

lmppl 用什么语言开发?

主要使用 Python 编写,采用 MIT License 许可证,目前有 168 个 Star。

lmppl 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 lmppl 的源码?

可以直接克隆仓库:git clone https://github.com/asahi417/lmppl.git;安装与运行步骤见仓库 README。

lmppl 最近还在维护吗?

仓库最近一次更新为 2025年6月20日,当前有 6 个未关闭 Issue;Star 数为 168,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

Calculate perplexity on a text with pre-trained language models. Support MLM (eg. DeBERTa), recurrent LM (eg. GPT3), and encoder-decoder LM (eg. Flan-T5).

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
366
价值分
121
活跃状态
活跃

语言占比

Python

README

license PyPI version PyPI pyversions PyPI status

Language Model Perplexity (LM-PPL)

Perplexity measures how predictable a text is by a language model (LM), and it is often used to evaluate fluency or proto-typicality of the text (lower the perplexity is, more fluent or proto-typical the text is). LM-PPL is a python library to calculate perplexity on a text with any types of pre-trained LMs. We compute an ordinary perplexity for recurrent LMs such as GPT3 (Brown et al., 2020) and the perplexity of the decoder for encoder-decoder LMs such as BART (Lewis et al., 2020) or T5 (Raffel et al., 2020), while we compute pseudo-perplexity (Wang and Cho, 2018) for masked LMs.

Get Started

Install via pip.

pip install lmppl

Example

Let’s solve sentiment analysis with perplexity as an example! Remember the text with lower perplexity is better, so we compare two texts (positive and negative) and choose the one with lower perplexity as the model prediction.

  1. Recurrent LM including variants of GPT.
import lmppl

scorer = lmppl.LM('gpt2')
text = [
    'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.',
    'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.'
]
ppl = scorer.get_perplexity(text)
print(list(zip(text, ppl)))
>>> [
  ('sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.', 136.64255272925908),
  ('sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.', 139.2400838400971)
]
print(f"prediction: {text[ppl.index(min(ppl))]}")
>>> "prediction: sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy."
  1. Masked LM including variants of BERT.
import lmppl

scorer = lmppl.MaskedLM('microsoft/deberta-v3-small')
text = [
    'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.',
    'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.'
]
ppl = scorer.get_perplexity(text)
print(list(zip(text, ppl)))
>>> [
  ('sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.', 1190212.1699246117),
  ('sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.', 1152767.482071837)
]
print(f"prediction: {text[ppl.index(min(ppl))]}")
>>> "prediction: sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad."
  1. Encoder-Decoder LM including variants of T5 and BART.
import lmppl

scorer = lmppl.EncoderDecoderLM('google/flan-t5-small')
inputs = [
    'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee.',
    'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee.'
]
outputs = [
    'I am happy.',
    'I am sad.'
]
ppl = scorer.get_perplexity(input_texts=inputs, output_texts=outputs)
print(list(zip(outputs, ppl)))
>>> [
  ('I am happy.', 4138.748977714201),
  ('I am sad.', 2991.629250051472)
]
print(f"prediction: {outputs[ppl.index(min(ppl))]}")
>>> "prediction: I am sad."

Models

Below are some examples of popular models and the corresponding model type to use within the lmppl package.

Model HuggingFace ID Model Type
BERT google-bert/bert-base-uncased MaskedLM
Roberta roberta-large MaskedLM
GPT 2 gpt2-xl LM
flan-ul2 google/flan-ul2 EncoderDecoderLM
GPT-NeoX EleutherAI/gpt-neox-20b LM
OPT facebook/opt-30b LM
Mixtral mistralai/Mixtral-8x22B-v0.1 LM
Llama 3 meta-llama/Meta-Llama-3-8B LM

Tips

  • Max Token Length: Each LM has its own max-token length (max_length for recurrent/masked LMs, and max_length_encoder and max_length_decoder for encoder-decoder LMs). Limiting those max-token will reduce the time to process the text, but it may affect the accuracy of the perplexity, so please experiment on your texts and decide an optimal token length.

  • Batch Size: One can pass batch size to the function get_perplexity (eg. get_perplexity(text, batch_size=32)). As default, it will process all the text once, that may cause memory error if the number of texts is too large.

评论

暂无评论