Skip to main content

Uncertainty quantification with PyTorch

Project description

logo

Installation | Quickstart | Methods | Friends | Contributing | Citation | Documentation | Paper

What is posteriors?

General purpose python library for uncertainty quantification with PyTorch.

  • Composable: Use with transformers, lightning, torchopt, torch.distributions, pyro and more!
  • Extensible: Add new methods! Add new models!
  • Functional: Easier to test, closer to mathematics!
  • Scalable: Big model? Big data? No problem!
  • Swappable: Swap between algorithms with ease!

Installation

posteriors is available on PyPI and can be installed via pip:

pip install posteriors

Quickstart

posteriors is functional first and aims to be easy to use and extend. Let's try it out by training a simple model with variational inference:

from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
from torch import nn, utils, func
import torchopt
import posteriors

dataset = MNIST(root="./data", transform=ToTensor())
train_loader = utils.data.DataLoader(dataset, batch_size=32, shuffle=True)
num_data = len(dataset)

classifier = nn.Sequential(nn.Linear(28 * 28, 64), nn.ReLU(), nn.Linear(64, 10))
params = dict(classifier.named_parameters())


def log_posterior(params, batch):
    images, labels = batch
    images = images.view(images.size(0), -1)
    output = func.functional_call(classifier, params, images)
    log_post_val = (
        -nn.functional.cross_entropy(output, labels)
        + posteriors.diag_normal_log_prob(params) / num_data
    )
    return log_post_val, output


transform = posteriors.vi.diag.build(
    log_posterior, torchopt.adam(), temperature=1 / num_data
)  # Can swap out for any posteriors algorithm

state = transform.init(params)

for batch in train_loader:
    state = transform.update(state, batch)

Observe that posteriors recommends specifying log_posterior and temperature such that log_posterior remains on the same scale for different batch sizes. posteriors algorithms are designed to be stable as temperature goes to zero.

Further, the output of log_posterior is a tuple containing the evaluation (single-element Tensor) and an additional argument (TensorTree) containing any auxiliary information we'd like to retain from the model call, here the model predictions. If you have no auxiliary information, you can simply return torch.tensor([]) as the second element. For more info see torch.func.grad (with has_aux=True) or the documentation.

Check out the tutorials for more detailed usage!

Methods

posteriors supports a variety of methods for uncertainty quantification, including:

With full details available in the API documentation.

posteriors is designed to be easily extensible, if you're favorite method is not listed above, raise an issue and we'll see what we can do!

Friends

Interfaces seamlessly with:

The functional transform interface is strongly inspired by frameworks such as optax and blackjax.

As well as other UQ libraries fortuna, laplace, numpyro, pymc and uncertainty-baselines.

Contributing

You can report a bug or request a feature by creating a new issue on GitHub.

If you want to contribute code, please check the contributing guide.

Citation

If you use posteriors in your research, please cite the library using the following BibTeX entry:

@article{duffield2024scalable,
  title={Scalable Bayesian Learning with posteriors},
  author={Duffield, Samuel and Donatella, Kaelan and Chiu, Johnathan and Klett, Phoebe and Simpson, Daniel},
  journal={arXiv preprint arXiv:2406.00104},
  year={2024}
}

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

posteriors-0.0.5.tar.gz (38.5 kB view details)

Uploaded Source

Built Distribution

posteriors-0.0.5-py3-none-any.whl (47.7 kB view details)

Uploaded Python 3

File details

Details for the file posteriors-0.0.5.tar.gz.

File metadata

  • Download URL: posteriors-0.0.5.tar.gz
  • Upload date:
  • Size: 38.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for posteriors-0.0.5.tar.gz
Algorithm Hash digest
SHA256 6d0a9c418b695a5c8c2bd952e513eb5a5984fbdae5c8a4a004c106ba37facc55
MD5 2a845cc40c5164bd9c235f9e7ec8898f
BLAKE2b-256 e6db2ee6d23a3cb376688d61804219ad5f5d529792b97d07c3021ac2cde66773

See more details on using hashes here.

Provenance

The following attestation bundles were made for posteriors-0.0.5.tar.gz:

Publisher: publish_pypi.yaml on normal-computing/posteriors

Attestations:

File details

Details for the file posteriors-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: posteriors-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 47.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for posteriors-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 606b2a49b24b5050e783c7a4f4f49f0581bc1c51b64e585bef3ba5e02244b863
MD5 1dc5dc0aa3f6114b73098faa029649e2
BLAKE2b-256 947d1a9b5f15eb8730d38ce7260682394cc89bbf19f9db66921c71a756ee433a

See more details on using hashes here.

Provenance

The following attestation bundles were made for posteriors-0.0.5-py3-none-any.whl:

Publisher: publish_pypi.yaml on normal-computing/posteriors

Attestations:

Supported by

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