Soft Entropy Estimator
Implementation of the soft entropy estimator from Conklin (2025), as used in Learning is Forgetting: LLM Training as Lossy Compression (Conklin et al., ICLR 2026).
Installation
pip install soft-entropy
JAX and PyTorch are included by default. To also install Jupyter:
pip install soft-entropy[nb]
On Google Colab, where Jupyter is pre-installed, the default install works as-is.
To install from source:
git clone https://github.com/...
pip install -e .
You can run a tutorial estimating entropies on MNIST locally using the notebook in this repo, or on google collab here
How it works
A visual depiction of this process can be found here
Soft Entropy estimates the entropy of a distribution over embeddings without storing all embeddings in memory. The procedure for a set of embeddings Z of dimensionality d:
- Normalise each embedding onto the unit sphere.
- Sample
nreference pointsWuniformly from the unit sphere (once, by drawing from a standard normal and normalising). - Compute Distances comparing each
zto all the reference pointsW. Usually cosine similarites are used. - Soft-assign
zto reference points by passing the distances through a softmax with a calibrated temperatureε*. By default,ε*is found by solvingKL(vMF(ε) ‖ Uniform) = log(n)via bracketed root-finding (calibration="find_eps"), which ensures the soft assignments are well-spread across bins at any dimensionality. A closed-form leading-order approximationε* = 1 / sqrt(2d · log(n))is also available viacalibration="leading_order". This gives each embedding a probability vector over thenbins. - Aggregate & Compute Entropy we average the probability vectors over the batch to get a single distribution
P(Z)over bins. Computing the Shannon entropyH(Z)is then just-Σ P(Z) log P(Z). In practice we usually normalise this by maximum entropylog(n)so it lives in[0, 1](called efficiency).
Conditioning on a label x is done by repeating step 5 on only the embeddings where the label equals x. Mutual information then follows as:
I(X; Z) = H(Z) - Σ_x P(X=x) · H(Z | X=x)
The temperature calibration ensures estimates are directly comparable across models with different hidden dimensionalities.
Package structure
soft_entropy/
├── pytorch.py # PyTorch implementation
├── numpy.py # NumPy implementation
├── jax.py # JAX implementation
├── accumulator.py # Backend-agnostic batch accumulator
└── temp_calibration.py # Temperature calibration (find_eps, sphere_temp_calibration)
soft_entropy.R # R implementation
examples_MNIST.ipynb
Single-batch functions
Each backend module exposes two functions with the same interface:
soft_entropy(z, n_bins=100, seed=0, calibration="find_eps") -> scalar
soft_mutual_information(z, labels, n_bins=100, seed=0, calibration="find_eps") -> scalar
z: embeddings[batch, d]labels: 1-D integer array of class labels[batch]n_bins: number of reference points (default 100; robust to this choice)seed: random seed used to sample the reference pointsWcalibration:"find_eps"(default) solvesKL(vMF(ε) ‖ Uniform) = log(n)for the temperature;"leading_order"uses the closed-form approximationε* = 1 / sqrt(2d · log(n))
The seed is the key to cross-model comparability. Because entropy is measured relative to the same fixed reference points, two calls with the same seed, d and n_bins are operating in the same "coordinate system" — so their H(Z) and I(X;Z) values are directly comparable even if the embeddings come from different models, layers, or datasets. Always use the same seed when comparing estimates.
These are appropriate when all embeddings fit in memory at once.
PyTorch:
from soft_entropy.pytorch import soft_entropy, soft_mutual_information
h = soft_entropy(z)
mi = soft_mutual_information(z, labels)
NumPy:
from soft_entropy.numpy import soft_entropy, soft_mutual_information
h = soft_entropy(z_np)
mi = soft_mutual_information(z_np, labels_np)
JAX:
from soft_entropy.jax import soft_entropy, soft_mutual_information
h = soft_entropy(z_jax)
mi = soft_mutual_information(z_jax, labels_jax)
Accumulator — large datasets and multiple label sets
SoftEntropyAccumulator accumulates soft assignment counts across batches. The reference points W are sampled once at construction and reused every batch, so counts are consistent across the whole dataset.
When using the torch backend, the accumulator automatically selects the best available device (CUDA → MPS → CPU) and moves all tensors and incoming batches there. No manual device management is required.
from soft_entropy import SoftEntropyAccumulator
acc = SoftEntropyAccumulator(d=768, n_bins=100, seed=0, backend='torch')
backend is one of 'torch', 'numpy', or 'jax'. The input arrays passed to update() must match.
Basic usage — single label set
for z, labels in dataloader:
acc.update(z, labels)
print(acc.entropy()) # float — H(Z)
print(acc.mutual_information()) # dict — {"labels": float}
print(acc.results()) # flat summary dict
Multiple label sets in one pass
Pass a dict to labels. The soft assignments are computed once per batch; accumulating extra label sets costs only cheap indexing.
for z, token_ids, bigram_ids, pref_labels in dataloader:
acc.update(z, labels={
"token": token_ids,
"bigram": bigram_ids,
"preference": pref_labels,
})
acc.entropy()
# 0.863
acc.mutual_information()
# {"token": 0.12, "bigram": 0.31, "preference": 0.04}
acc.conditional_entropy()
# {"token": {0: 0.71, 1: 0.69, ...},
# "bigram": {(0,1): 0.58, ...},
# "preference": {0: 0.82, 1: 0.79}}
acc.results()
# {"H(Z)": 0.863,
# "I(X;Z)/token": 0.12, "regularity/token": 0.14,
# "I(X;Z)/bigram": 0.31, "regularity/bigram": 0.36,
# "I(X;Z)/preference": 0.04, "regularity/preference": 0.05}
Label structure
Labels are always flat 1-D integer arrays of length batch. What constitutes a label is up to the caller:
| Use case | Label value |
|---|---|
| Token-level back-off | Token id at each position |
| Bigram back-off | Integer encoding of (prev_token, token) pair |
| Preference | Binary 0 (rejected) / 1 (preferred) |
| Language id | Integer language code |
| Digit class | Class integer 0–9 |
For n-gram back-off as used in the paper, encode each n-gram as a single label (e.g. (current_token,prev_token) for bigrams) and pass one label set per n-gram width.
Resetting between runs
acc.reset() # clears all counts, keeps reference points W
Efficiency, Regularity, and Optimality
Efficiency is entropy normalised by its maximum possible value:
Efficiency = H(Z) / log(n)
All entropy and mutual information values returned by this library are already efficiency-normalised (i.e. in [0, 1]), so they are directly comparable across models with different embedding dimensionalities or numbers of reference points.
Regularity is mutual information normalised by entropy:
Regularity = I(X; Z) / H(Z)
acc.results() returns regularity for each label set under the key regularity/<label_set>.
Optimality is the ratio of expressivity to complexity:
Optimality = I(Y; Z) / I(X; Z)
where X is the input label (e.g. token) and Y is the output label (e.g. next token). This approaches 1.0 as representations converge to the Information Bottleneck bound. Computing optimality requires two separate label sets (input and output) and dividing their mutual information estimates.
Notebooks
| Notebook | Contents |
|---|---|
examples_MNIST.ipynb |
Examples on sklearn digits dataset verifying estimator behaviour |
Metadata
Release files for soft-entropy 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| soft_entropy-0.1.5.tar.gz | 17.5 kB | Without provenance | Details for soft_entropy-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| soft_entropy-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for soft_entropy-0.1.5-py3-none-any.whl |
Total release size: 35.8 kB
Release files / soft_entropy-0.1.5.tar.gz
| Download URL | soft_entropy-0.1.5.tar.gz |
|---|---|
| Size | 17.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c302dc5e88ddb46de90f9778f0b99e17188e9ffe857cfe84dcd827f8192b9603
|
|
BLAKE2b-256 checksum How to use checksums |
fcec4fc8d4d65167093f6bb8acafa133c53afe7beef0dabc2d14fbb651fbb3c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / soft_entropy-0.1.5-py3-none-any.whl
| Download URL | soft_entropy-0.1.5-py3-none-any.whl |
|---|---|
| Size | 18.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a4541f19b518b44e305e27a76ff1c66a79fa631f9b1bafa74eedbabd2f967562
|
|
BLAKE2b-256 checksum How to use checksums |
675ba98c95b54f96e63dc96d866c150272253b76bb4f8ebff47dd49c11ba5203
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Security for soft-entropy 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.