Skip to content

Instantly share code, notes, and snippets.

@pszemraj
Last active November 9, 2022 16:45
Show Gist options
  • Save pszemraj/00de5608703bd55ef4f90c222d1462a1 to your computer and use it in GitHub Desktop.
Save pszemraj/00de5608703bd55ef4f90c222d1462a1 to your computer and use it in GitHub Desktop.
basic huggingface evaluate-based perplexity resources. All credit to https://huggingface.co/spaces/evaluate-metric/perplexity

How to Use

The metric takes a list of text as input, as well as the name of the model used to compute the metric:

from evaluate import load
perplexity = load("perplexity", module_type="metric")
results = perplexity.compute(predictions=predictions, model_id='gpt2')

Inputs

  • model_id (str): model used for calculating Perplexity. NOTE: Perplexity can only be calculated for causal language models.
  • predictions (list of str): input text, where each separate text snippet is one list entry.
  • batch_size (int): the batch size to run texts through the model. Defaults to 16.
  • add_start_token (bool): whether to add the start token to the texts, so the perplexity can include the probability of the first word. Defaults to True.
  • device (str): device to run on, defaults to cuda when available

Output Values

This metric outputs a dictionary with the perplexity scores for the text input in the list, and the average perplexity. If one of the input texts is longer than the max input length of the model, then it is truncated to the max length for the perplexity computation.

{'perplexities': [8.182524681091309, 33.42122268676758, 27.012239456176758], 'mean_perplexity': 22.871995608011883}

The range of this metric is [0, inf). A lower score is better.

Values from Popular Papers

Examples

Calculating perplexity on predictions defined here:

perplexity = evaluate.load("perplexity", module_type="metric")
input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]
results = perplexity.compute(model_id='gpt2',
                             add_start_token=False,
                             predictions=input_texts)
print(list(results.keys()))
print(round(results["mean_perplexity"], 2))
print(round(results["perplexities"][0], 2))

Calculating perplexity on predictions loaded in from a dataset:

perplexity = evaluate.load("perplexity", module_type="metric")
input_texts = datasets.load_dataset("wikitext",
                                    "wikitext-2-raw-v1",
                                    split="test")["text"][:50]
input_texts = [s for s in input_texts if s!='']
results = perplexity.compute(model_id='gpt2',
                             predictions=input_texts)
print(list(results.keys()))
print(round(results["mean_perplexity"], 2))
print(round(results["perplexities"][0], 2))

Limitations and Bias

Note that the output value is based heavily on what text the model was trained on. This means that perplexity scores are not comparable between models or datasets. See Meister and Cotterell, "Language Model Evaluation Beyond Perplexity" (2021) for more information about alternative model evaluation strategies.

Citation

@article{jelinek1977perplexity,
title={Perplexity—a measure of the difficulty of speech recognition tasks},
author={Jelinek, Fred and Mercer, Robert L and Bahl, Lalit R and Baker, James K},
journal={The Journal of the Acoustical Society of America},
volume={62},
number={S1},
pages={S63--S63},
year={1977},
publisher={Acoustical Society of America}
}

Further References

perplexity = evaluate.load("perplexity", module_type="metric")
input_texts = datasets.load_dataset("wikitext",
"wikitext-2-raw-v1",
split="test")["text"][:50]
input_texts = [s for s in input_texts if s!='']
results = perplexity.compute(model_id='gpt2',
predictions=input_texts)
print(list(results.keys()))
#>>>['perplexities', 'mean_perplexity']
print(round(results["mean_perplexity"], 2))
print(round(results["perplexities"][0], 2))
perplexity = evaluate.load("perplexity", module_type="metric")
input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]
results = perplexity.compute(model_id='gpt2',
add_start_token=False,
predictions=input_texts)
print(list(results.keys()))
# >>>['perplexities', 'mean_perplexity']
print(round(results["mean_perplexity"], 2))
print(round(results["perplexities"][0], 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment