Skip to main content

Stochastic variational inference with sparsity inducing priors in PyTorch.

Project description

VariationalSparseBayes

A PyTorch library for stochastic variational inference with sparsity inducing priors.

comparison-plot

A comparison plot for sparse Bayesian regression with a half-cauchy prior (L), support vector regression (C), and the relevance vector machine (R). We see the half-cauchy prior provides a more sparse solution and better error bars.

What is VariationalSparseBayes?

This package provides an implementation of the algorithm described in Louizos et. al. (2017) for use on a broad class of machine learning problems.

Installation

pip install variationalsparsebayes

Usage

The library provides a high-level interface with some prebuilt sparse Bayesian models and a low-level interface for building custom sparse Bayesian models.

High-level interface

The library provides a few sparse Bayesian models:

To implement your own linear model, you can inherit from the SparseFeaturesLibrary class. Note that I haven't implemented the "group" sparsity idea presented in Louizos et. al. (2017). Sparsification is performed at the parameter level (meaning far less computational savings).

Low-level interface

The most important class provided by the library is the SVIHalfCauchyPrior. The class inherits from nn.Module. The user is responsible for (i) transforming a batch of weights from the variational posterior into a batch of predictions and (ii) adding the KL-divergence provided by the prior onto the negative ELBO.

from torch import nn
from variationalsparsebayes import SVIHalfCauchyPrior

class MyModel(nn.Module):
    def __init__(self, num_params: int):
        super().__init__()
        # we initialize the prior with tau=1e-5 (see https://arxiv.org/pdf/1705.08665.pdf)
        self.prior = SVIHalfCauchyPrior(num_params, 1e-5)
        ...

    def forward(self, x, num_reparam_samples):
        w_samples = self.prior.get_reparam_weights(num_reparam_samples)
        sparse_index = self.prior.sparse_index
        # user transforms weights and inputs into predictions
        ...

    def elbo(self, x, y):
        return log_like(x, y) - self.prior.kl_divergence()

model = MyModel(num_params)
...

When it comes time to sparsify the approximate posterior run:

model.prior.update_sparse_index()
# get the index of all weights which remain after sparsification
model.prior.sparse_index

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

variationalsparsebayes-0.3.0.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

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

variationalsparsebayes-0.3.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file variationalsparsebayes-0.3.0.tar.gz.

File metadata

  • Download URL: variationalsparsebayes-0.3.0.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.10 Darwin/23.0.0

File hashes

Hashes for variationalsparsebayes-0.3.0.tar.gz
Algorithm Hash digest
SHA256 099141eda19b2b58e630f77168d8afb43af57d34c50373856ccbe09c723deca4
MD5 007ce45c32b992132e7df4b4470ed1de
BLAKE2b-256 cdfdb21700b00bed5ba51e1c0bcbee1b2e75fb2cd926fabb34401ede8ea785bc

See more details on using hashes here.

File details

Details for the file variationalsparsebayes-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for variationalsparsebayes-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b45944fb46bc05ec1b5bf1c089dcc2fb8e1468129f3759be5ddf0c83dce98c2b
MD5 8239577c58a0d535c946829ccd851f70
BLAKE2b-256 384fac899f9ee35a37590b6baa361337aefc3e78e23db7ea5841a1e81e612776

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