Stochastic variational inference with sparsity inducing priors in PyTorch.
Project description
VariationalSparseBayes
A PyTorch library for stochastic variational inference with sparsity inducing priors.
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:
- sparse polynomial regression
- sparse Bayesian neural networks.
- sparse learning with precomputed features
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file variationalsparsebayes-0.3.1.tar.gz.
File metadata
- Download URL: variationalsparsebayes-0.3.1.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Darwin/23.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e683a3b48f520d288a3446a6f4bed93a4d1be88fb421a1aa8bb90b7eef1d8a9e
|
|
| MD5 |
48c2a91d09da62332ec62e067e19bf81
|
|
| BLAKE2b-256 |
f611344b5cc117a66602cd01b28ecd5d657c7b7a151469bce12f6ebf0f5d668f
|
File details
Details for the file variationalsparsebayes-0.3.1-py3-none-any.whl.
File metadata
- Download URL: variationalsparsebayes-0.3.1-py3-none-any.whl
- Upload date:
- Size: 23.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Darwin/23.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6db4e45a65700f8a5a7cbf635b1bf850b3d2a684968749b4759d7eb15b582ae7
|
|
| MD5 |
8dc8dd824dec4e0516fd078c8e06fb30
|
|
| BLAKE2b-256 |
6b026ecd8dcd277f951613f4b7a3e8673b5d917cfce83252805fc47f481f3ac1
|