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.
Metadata
Release files for bayesnn 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| bayesnn-0.1.0.tar.gz | 7.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bayesnn-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.6 kB
Release files / bayesnn-0.1.0.tar.gz
| Download URL | bayesnn-0.1.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
74bd3664188c2f9e304210967ca24da44a5028a99142c8c4f79db08764ebf193
|
|
BLAKE2b-256 checksum How to use checksums |
2716cf1af5b4f385cc0e7c6e45242b6b400774ecb569fda948467b303a2c28b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.13
|
Release files / bayesnn-0.1.0-py3-none-any.whl
| Download URL | bayesnn-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
138f4e942821700d582789a00727bfb01550143ec6f9d5dba8080d3413259ff3
|
|
BLAKE2b-256 checksum How to use checksums |
7170b591eb1f7bd7174e55b9cf3227be4513a315fac60b4e383291366c88fa5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.13
|
Security for bayesnn 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.