Skip to main content

A package to conveniently compute surprisals for text sequences and subsequences

Project description

surprisal

Compute surprisal from language models!

surprisal supports most Causal Language Models (GPT2- and GPTneo-like models) from Huggingface or local checkpoint, as well as GPT3 models from OpenAI using their API! We also support KenLM N-gram based language models using the KenLM Python interface.

Masked Language Models (BERT-like models) are in the pipeline and will be supported at a future time (see #9).

Docs

Visit https://aalok-sathe.github.io/surprisal/surprisal.html.

Usage

The snippet below computes per-token surprisals for a list of sentences

from surprisal import AutoHuggingFaceModel, KenLMModel

sentences = [
    "The cat is on the mat",
    "The cat is on the hat",
    "The cat is on the pizza",
    "The pizza is on the mat",
    "I told you that the cat is on the mat",
    "I told you the cat is on the mat",
]

m = AutoHuggingFaceModel.from_pretrained('gpt2')
m.to('cuda') # optionally move your model to GPU!

k = KenLMModel(model_path='./literature.arpa')

for result in m.surprise(sentences):
    print(result)
for result in k.surprise(sentences):
    print(result)

and produces output of this sort (gpt2):

       The       Ġcat        Ġis        Ġon       Ġthe       Ġmat  
     3.276      9.222      2.463      4.145      0.961      7.237  
       The       Ġcat        Ġis        Ġon       Ġthe       Ġhat  
     3.276      9.222      2.463      4.145      0.961      9.955  
       The       Ġcat        Ġis        Ġon       Ġthe     Ġpizza  
     3.276      9.222      2.463      4.145      0.961      8.212  
       The     Ġpizza        Ġis        Ġon       Ġthe       Ġmat  
     3.276     10.860      3.212      4.910      0.985      8.379  
         I      Ġtold       Ġyou      Ġthat       Ġthe       Ġcat        Ġis        Ġon       Ġthe       Ġmat 
     3.998      6.856      0.619      2.443      2.711      7.955      2.596      4.804      1.139      6.946 
         I      Ġtold       Ġyou       Ġthe       Ġcat        Ġis        Ġon       Ġthe       Ġmat  
     3.998      6.856      0.619      4.115      7.612      3.031      4.817      1.233      7.033 

extracting surprisal over a substring

A surprisal object can be aggregated over a subset of tokens that best match a span of words or characters. Word boundaries are inherited from the model's standard tokenizer, and may not be consistent across models, so using character spans when slicing is the default and recommended option. Surprisals are in log space, and therefore added over tokens during aggregation. For example:

>>> [s] = m.surprise("The cat is on the mat")
>>> s[3:6, "word"] 
12.343366384506226
Ġon Ġthe Ġmat
>>> s[3:6, "char"]
9.222099304199219
Ġcat
>>> s[3:6]
9.222099304199219
Ġcat

You can use Surprisal.lineplot() to visualize the surprisals:

from matplotlib import pyplot as plt
f, a = None, None
for result in m.surprise(sentences):
    f, a = result.lineplot(f, a)

plt.show()

surprisal has a minimal CLI:

python -m surprisal -m distilgpt2 "I went to the train station today."
      I      Ġwent        Ġto       Ġthe     Ġtrain   Ġstation     Ġtoday          . 
  4.984      5.729      0.812      1.723      7.317      0.497      4.600      2.528 

python -m surprisal -m distilgpt2 "I went to the space station today."
      I      Ġwent        Ġto       Ġthe     Ġspace   Ġstation     Ġtoday          . 
  4.984      5.729      0.812      1.723      8.425      0.707      5.182      2.574

Installing

Because surprisal is used by people from different communities for different purposes, by default, core dependencies related to language modeling are marked optional. Depending on your use case, install surprisal with the appropriate extras.

Installing from GitHub release or PyPI (latest stable release)

Use a command like pip install surprisal[optional] or pip install git+https://github.com/aalok-sathe/surprisal.git[optoinal], replacing [optional] with whatever optional support you need. For multiple optional extras, use a comma-separated list:

pip install surprisal[kenlm,transformers]

Possible options include: transformers, kenlm, petals.

If you use uv for your existing project, use the -E option to add surprisal together with the desired optional dependencies:

uv add surprisal[transformers,kenlm]

Installing from source (bleeding edge)

The -e flag allows an editable install, so you can make changes to surprisal.

git clone https://github.com/aalok-sathe/surprisal.git
pip install .[transformers] -e

Acknowledgments

Inspired from the now-inactive lm-scorer; thanks to folks from CPLlab and EvLab for comments and help.

License

MIT License. (C) 2022-25, contributors.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

surprisal-0.1.7.tar.gz (89.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

surprisal-0.1.7-py3-none-any.whl (89.5 MB view details)

Uploaded Python 3

File details

Details for the file surprisal-0.1.7.tar.gz.

File metadata

  • Download URL: surprisal-0.1.7.tar.gz
  • Upload date:
  • Size: 89.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.5

File hashes

Hashes for surprisal-0.1.7.tar.gz
Algorithm Hash digest
SHA256 dfc304fc8de06af8791977ce51dc9c1cbc193d2527f1b4c2878bb9ec4a8026ba
MD5 c20b75b059bfd46ad2d69ea4c375cdfb
BLAKE2b-256 8912c211474d29ede1ec85f18188b9e9c575d28bbe1b858216735e6b5b2ac851

See more details on using hashes here.

File details

Details for the file surprisal-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: surprisal-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 89.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.5

File hashes

Hashes for surprisal-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c5aa611b578c84f493c44b47df3427cbb56b57e7db97b066b08df82732adb5ba
MD5 2f57e496cf2d5513c364b9daac5cfd8d
BLAKE2b-256 95aeba95e968fe2018aa39ab2844722a50b41cd828dcdd7232acb88031abd3b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page