Bayesian neural network building blocks for PyTorch.
Project description
BayesNN
'bayesnn' provides a minimal set of PyTorch layers, functionals, and loss functions that make it easy to experiment with fully Bayesian neural networks. The package powers the demo notebooks/scripts in this repository and is now installable so you can reuse the same building blocks in your own projects.
Features
BayesianLinearlayer with reparameterized weight sampling.- Utility functionals for KL divergence and Monte Carlo log-likelihood estimates (Gaussian and categorical).
- Variational Bayes (
BNN_VBLoss) and α-divergence (BNN_AlphaDivergenceLoss) objectives with MC sampling.
Installation
pip install bayesnn
To install from source instead:
git clone https://github.com/PandaThr/BayesNN.git
cd BayesianNN
pip install .
For local development, use editable mode so changes are picked up immediately:
pip install -e .
Quickstart
import torch
from torch import nn
from bayesnn import BayesianLinear, BNN_VBLoss
class BayesianMLP(nn.Module):
def __init__(self, input_dim: int, hidden_dim: int, output_dim: int):
super().__init__()
self.net = nn.Sequential(
BayesianLinear(input_dim, hidden_dim),
nn.ReLU(),
BayesianLinear(hidden_dim, output_dim, output_noise=True),
)
# Fixed observation noise variance
self.register_buffer("noise_var", torch.tensor(1e-2))
def forward(self, x):
return self.net(x)
model = BayesianMLP(input_dim=2, hidden_dim=64, output_dim=1)
criterion = BNN_VBLoss(model, N=1000, mc=50, beta=1.0)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
x_batch = torch.randn(128, 2)
y_batch = torch.sin(x_batch[:, :1])
loss = criterion(x_batch, y_batch)
loss.backward()
optimizer.step()
Project structure
bayesnn/: Source package exposed when you install the library.
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 bayesnn-0.1.0.tar.gz.
File metadata
- Download URL: bayesnn-0.1.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74bd3664188c2f9e304210967ca24da44a5028a99142c8c4f79db08764ebf193
|
|
| MD5 |
5265f0b69106ba724cf9ec5301613392
|
|
| BLAKE2b-256 |
2716cf1af5b4f385cc0e7c6e45242b6b400774ecb569fda948467b303a2c28b5
|
File details
Details for the file bayesnn-0.1.0-py3-none-any.whl.
File metadata
- Download URL: bayesnn-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
138f4e942821700d582789a00727bfb01550143ec6f9d5dba8080d3413259ff3
|
|
| MD5 |
4fdce333cdb97a674fce7b0ee2df13a1
|
|
| BLAKE2b-256 |
7170b591eb1f7bd7174e55b9cf3227be4513a315fac60b4e383291366c88fa5e
|