Skip to main content

Bayesian layers for NumPyro and Jax

Project description

codecov License version

BLayers

NOTE: BLayers is in alpha. Expect changes. Feedback welcome.

The missing layers package for Bayesian inference. Inspiration from Keras and Tensorflow Probability, but made specifically for Numpyro + Jax.

Easily build Bayesian models from parts, abstract away the boilerplate, and tweak priors as you wish.

Fit models either using Variational Inference (VI) or your sampling method of choice. Use BLayer's ELBO implementation to do either batched VI or sampling without having to rewrite models.

BLayers helps you write pure Numpyro, so you can integrate it with any Numpyro code to build models of arbitrary complexity. It also gives you a recipe to build more complex layers as you wish.

the starting point

The simplest non-trivial (and most important!) Bayesian regression model form is the adaptive prior,

lmbda ~ HalfNormal(1)
beta  ~ Normal(0, lmbda)
y     ~ Normal(beta * x, 1)

BLayers takes this as its starting point and most fundamental building block, providing the flexible AdaptiveLayer.

from blayers import AdaptiveLayer, gaussian_link_exp
def model(x, y):
    mu = AdaptiveLayer()('mu', x)
    return gaussian_link_exp(mu, y)

For you purists out there, we also provide a FixedPriorLayer for standard L1/L2 regression.

from blayers import FixedPriorLayer, gaussian_link_exp
def model(x, y):
    mu = FixedPriorLayer()('mu', x)
    return gaussian_link_exp(mu, y)

additional layers

factorization machines

Developed in Rendle 2010 and Rendle 2011, FMs provide a low-rank approximation to the x-by-x interaction matrix. For those familiar with R syntax, it is an approximation to y ~ x:x, excluding the x^2 terms.

To fit the equivalent of an r model like y ~ x*x (all main effects, x^2 terms, and one-way interaction effects), you'd do

from blayers import FMLayer, gaussian_link_exp
def model(x, y):
    mu = (
        AdaptiveLayer('x', x) +
        AdaptiveLayer('x2', x**2) +
        FMLayer(low_rank_dim=3)('xx', x)
    )
    return gaussian_link_exp(mu, y)

uv decomp

We also provide a standard UV deccomp for low rank interaction terms

from blayers import LowRankInteractionLayer, gaussian_link_exp
def model(x, z, y):
    mu = (
        AdaptiveLayer('x', x) +
        AdaptiveLayer('z', z) +
        LowRankInteractionLayer(low_rank_dim=3)('xz', x, z)
    )
    return gaussian_link_exp(mu, y)

links

We provide link functions as a convenience to abstract away a bit more Numpyro boilerplate.

We currently provide

  • gaussian_link_exp

batched loss

The default Numpyro way to fit batched VI models is to use plate, which confuses me a lot. Instead, BLayers provides Batched_Trace_ELBO which does not require you to use plate to batch in VI. Just drop your model in.

from blayers.infer import Batched_Trace_ELBO, svi_run_batched

svi = SVI(model_fn, guide, optax.adam(schedule), loss=loss_instance)

svi_result = svi_run_batched(
    svi,
    rng_key,
    num_steps,
    batch_size=1000,
    **model_data,
)

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

blayers-0.1.0a1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

blayers-0.1.0a1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file blayers-0.1.0a1.tar.gz.

File metadata

  • Download URL: blayers-0.1.0a1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for blayers-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 001582978560138004793f409eb4045f9feb2d6f7f0bab9db5b4a5a87dea7338
MD5 789d951d5e787c67dc181e23a42282cf
BLAKE2b-256 24429d872bc892e2f45f361fd049f7bc68184204e08a69f50d7686cf9f89825f

See more details on using hashes here.

File details

Details for the file blayers-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: blayers-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for blayers-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 4847ce19d5929034a84e52a566002d489e7639fe59771247c01d3dade44aa277
MD5 9051ea4131e014df73f20dc186bd7b20
BLAKE2b-256 6628ed19a3317a192402dc55692ba510d586ada8deb10c4442d6be396bf58c5b

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