{"id":18753510,"url":"https://github.com/lucadellalib/bayestorch","last_synced_at":"2025-07-22T17:04:26.289Z","repository":{"id":62901967,"uuid":"560269783","full_name":"lucadellalib/bayestorch","owner":"lucadellalib","description":"Lightweight Bayesian deep learning library for fast prototyping based on PyTorch","archived":false,"fork":false,"pushed_at":"2023-02-24T17:38:34.000Z","size":106,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-16T21:45:57.572Z","etag":null,"topics":["bayes-by-backprop","bayesian-deep-learning","bayesian-inference","bayesian-neural-networks","deep-learning","machine-learning","markov-chain-monte-carlo","python","pytorch","stein-variational-gradient-descent","uncertainty-quantification","variational-inference"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucadellalib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-11-01T05:10:55.000Z","updated_at":"2025-04-24T22:21:00.000Z","dependencies_parsed_at":"2025-04-13T09:45:44.567Z","dependency_job_id":null,"html_url":"https://github.com/lucadellalib/bayestorch","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/lucadellalib/bayestorch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadellalib%2Fbayestorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadellalib%2Fbayestorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadellalib%2Fbayestorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadellalib%2Fbayestorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucadellalib","download_url":"https://codeload.github.com/lucadellalib/bayestorch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucadellalib%2Fbayestorch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265845802,"owners_count":23837893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayes-by-backprop","bayesian-deep-learning","bayesian-inference","bayesian-neural-networks","deep-learning","machine-learning","markov-chain-monte-carlo","python","pytorch","stein-variational-gradient-descent","uncertainty-quantification","variational-inference"],"created_at":"2024-11-07T17:26:04.393Z","updated_at":"2025-07-22T17:04:26.270Z","avatar_url":"https://github.com/lucadellalib.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BayesTorch\n\n[![Python version: 3.6 | 3.7 | 3.8 | 3.9 | 3.10](https://img.shields.io/badge/python-3.6%20|%203.7%20|%203.8%20|%203.9%20|%203.10-blue)](https://www.python.org/downloads/)\n[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/lucadellalib/bayestorch/blob/main/LICENSE)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat\u0026labelColor=ef8336)](https://github.com/PyCQA/isort)\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit\u0026logoColor=white)](https://github.com/pre-commit/pre-commit)\n![PyPI version](https://img.shields.io/pypi/v/bayestorch)\n[![](https://pepy.tech/badge/bayestorch)](https://pypi.org/project/bayestorch/)\n\nWelcome to `bayestorch`, a lightweight Bayesian deep learning library for fast prototyping based on\n[PyTorch](https://pytorch.org). It provides the basic building blocks for the following\nBayesian inference algorithms:\n\n- [Bayes by Backprop (BBB)](https://arxiv.org/abs/1505.05424)\n- [Markov chain Monte Carlo (MCMC)](https://www.cs.toronto.edu/~radford/ftp/thesis.pdf)\n- [Stein variational gradient descent (SVGD)](https://arxiv.org/abs/1608.04471)\n\n---------------------------------------------------------------------------------------------------------\n\n## 💡 Key features\n\n- Low-code definition of Bayesian (or partially Bayesian) models\n- Support for custom neural network layers\n- Support for custom prior/posterior distributions\n- Support for layer/parameter-wise prior/posterior distributions\n- Support for composite prior/posterior distributions\n- Highly modular object-oriented design\n- User-friendly and easily extensible APIs\n- Detailed API documentation\n\n---------------------------------------------------------------------------------------------------------\n\n## 🛠️️ Installation\n\n### Using Pip\n\nFirst of all, install [Python 3.6 or later](https://www.python.org). Open a terminal and run:\n\n```\npip install bayestorch\n```\n\n### From source\n\nFirst of all, install [Python 3.6 or later](https://www.python.org).\nClone or download and extract the repository, navigate to `\u003cpath-to-repository\u003e`, open a\nterminal and run:\n\n```\npip install -e .\n```\n\n---------------------------------------------------------------------------------------------------------\n\n## ▶️ Quickstart\n\nHere are a few code snippets showcasing some key features of the library.\nFor complete training loops, please refer to `examples/mnist` and `examples/regression`.\n\n### Bayesian model trainable via Bayes by Backprop\n\n```python\nfrom torch.nn import Linear\n\nfrom bayestorch.distributions import (\n    get_mixture_log_scale_normal,\n    get_softplus_inv_scale_normal,\n)\nfrom bayestorch.nn import VariationalPosteriorModule\n\n\n# Define model\nmodel = Linear(5, 1)\n\n# Define log scale normal mixture prior over the model parameters\nprior_builder, prior_kwargs = get_mixture_log_scale_normal(\n    model.parameters(),\n    weights=[0.75, 0.25],\n    locs=(0.0, 0.0),\n    log_scales=(-1.0, -6.0)\n)\n\n# Define inverse softplus scale normal posterior over the model parameters\nposterior_builder, posterior_kwargs = get_softplus_inv_scale_normal(\n    model.parameters(), loc=0.0, softplus_inv_scale=-7.0, requires_grad=True,\n)\n\n# Define Bayesian model trainable via Bayes by Backprop\nmodel = VariationalPosteriorModule(\n    model, prior_builder, prior_kwargs, posterior_builder, posterior_kwargs\n)\n```\n\n### Partially Bayesian model trainable via Bayes by Backprop\n\n```python\nfrom torch.nn import Linear\n\nfrom bayestorch.distributions import (\n    get_mixture_log_scale_normal,\n    get_softplus_inv_scale_normal,\n)\nfrom bayestorch.nn import VariationalPosteriorModule\n\n\n# Define model\nmodel = Linear(5, 1)\n\n# Define log scale normal mixture prior over `model.weight`\nprior_builder, prior_kwargs = get_mixture_log_scale_normal(\n    [model.weight],\n    weights=[0.75, 0.25],\n    locs=(0.0, 0.0),\n    log_scales=(-1.0, -6.0)\n)\n\n# Define inverse softplus scale normal posterior over `model.weight`\nposterior_builder, posterior_kwargs = get_softplus_inv_scale_normal(\n    [model.weight], loc=0.0, softplus_inv_scale=-7.0, requires_grad=True,\n)\n\n# Define partially Bayesian model trainable via Bayes by Backprop\nmodel = VariationalPosteriorModule(\n    model, prior_builder, prior_kwargs,\n    posterior_builder, posterior_kwargs, [model.weight],\n)\n```\n\n### Composite prior\n\n```python\nfrom torch.distributions import Independent\nfrom torch.nn import Linear\n\nfrom bayestorch.distributions import (\n    CatDistribution,\n    get_laplace,\n    get_normal,\n    get_softplus_inv_scale_normal,\n)\nfrom bayestorch.nn import VariationalPosteriorModule\n\n\n# Define model\nmodel = Linear(5, 1)\n\n# Define normal prior over `model.weight`\nweight_prior_builder, weight_prior_kwargs = get_normal(\n    [model.weight],\n    loc=0.0,\n    scale=1.0,\n    prefix=\"weight_\",\n)\n\n# Define Laplace prior over `model.bias`\nbias_prior_builder, bias_prior_kwargs = get_laplace(\n    [model.bias],\n    loc=0.0,\n    scale=1.0,\n    prefix=\"bias_\",\n)\n\n# Define composite prior over the model parameters\nprior_builder = (\n    lambda **kwargs: CatDistribution([\n        Independent(weight_prior_builder(**kwargs), 1),\n        Independent(bias_prior_builder(**kwargs), 1),\n    ])\n)\nprior_kwargs = {**weight_prior_kwargs, **bias_prior_kwargs}\n\n# Define inverse softplus scale normal posterior over the model parameters\nposterior_builder, posterior_kwargs = get_softplus_inv_scale_normal(\n    model.parameters(), loc=0.0, softplus_inv_scale=-7.0, requires_grad=True,\n)\n\n# Define Bayesian model trainable via Bayes by Backprop\nmodel = VariationalPosteriorModule(\n    model, prior_builder, prior_kwargs, posterior_builder, posterior_kwargs,\n)\n```\n\n---------------------------------------------------------------------------------------------------------\n\n## 📧 Contact\n\n[luca.dellalib@gmail.com](mailto:luca.dellalib@gmail.com)\n\n---------------------------------------------------------------------------------------------------------\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucadellalib%2Fbayestorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucadellalib%2Fbayestorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucadellalib%2Fbayestorch/lists"}