Skip to main content

Probabilistic programming using pytorch.

Project description

Borch

pipeline status coverage report lifecycle Code style: black docs

Getting Started | Documentation | Contributing

Borch is a universal probabilistic programming language (PPL) framework developed by Desupervised, that uses and integrates with pytorch. Whit special attention to support Bayesian neural networks in a very native fashion.

It's designed to

  • Flexible and scalable framework
  • Support neural networks out of the box.
  • Have bells and whistles a universal PPL needs.

Install it simply with:

pip install borch

[[TOC]]

Usage

A full set of tutorial are available at https://borch.readthedocs.io/en/latest/tutorials/index.html

As a quick example here is how the neural network interface looks. The module borch.nn provides implementations of neural network modules that are used for deep probabilistic programming. It provides an interface almost identical to the torch.nn modules and in many cases it is possible to just switch

import torch.nn as nn

to

import borch.nn as nn

and a network defined in torch is now probabilistic, without any other changes in the model specification, one also need to change the loss function to infer.vi.vi_loss.

Example:

import torch
import torch.nn.functional as F
from borch import nn
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

Installation

Binaries

Pre-build binaries are available at https://pypi.org/project/borch/ and can be installed using pip.

pip install borch

Virtual environmenthttps://pypi.org/project/borch/

When installing borch we normally use virtual environment to manage the Python version dependencies. Two good ones are https://virtualenv.pypa.io/en/stable/ and https://docs.conda.io/en/latest/miniconda.html, look at them and pick one to use and follow their documentation to crate and activate an environment.

NB All installations of python packages should be placed in the correct environment. Installing packages in the global python interpreter can result in unexpected behavior, where global packages may be used in favor of local packages.

Install locally

Once an appropriate conda environment has been created, run

make install

to install a production version of borch with support for a GPU, or

ARCH=gpu make install

for a version that only supports a CPU.

To install in development mode on machine(with no gpu support) run, and all development dependencies.

ARCH=cpu make install-dev

and for GPU support use

make install-dev

Docker

Using pre-built images

We publish docker images, both cpu and gpu versions at https://gitlab.com/desupervised/borch/container_registry/

The latest cpu images can be used as

docker run registry.gitlab.com/desupervised/borch/cpu:master

Build

Currently, all borch docker images are based on Ubuntu 18.04. By setting --build-arg ARCH=gpu to either gpu, cpu it will install either install all dependencies needed to run on gpu or to only run on cpu. If not provided it will fall back to the standard pytorch installation.

The GPU image can be built using:

docker build --build-arg ARCH=gpu .

And the CPU image using:

docker build --build-arg ARCH=cpu .

Contributing

Please read the contribution guidelines in CONTRIBUTING.md.

Citation

If you use this software for your research or business please cite us and help the package grow!

@misc{borch,
  author = {Belcher, Lewis and Gudmundsson, Johan and Green, Michael},
  title = {Borch},
  howpublished = {https://gitlab.com/desupervised/borch},
  month        = "Apr",
  year         = "2021",
  note         = "v0.1.0",
  annote       = ""
}

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

borch-0.0.8.tar.gz (83.1 kB view details)

Uploaded Source

Built Distribution

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

borch-0.0.8-py3-none-any.whl (100.6 kB view details)

Uploaded Python 3

File details

Details for the file borch-0.0.8.tar.gz.

File metadata

  • Download URL: borch-0.0.8.tar.gz
  • Upload date:
  • Size: 83.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10

File hashes

Hashes for borch-0.0.8.tar.gz
Algorithm Hash digest
SHA256 a3e86671e2de9f642d6c81b5afcef656ef21e4640016cc01ed35d9b58c0b2c83
MD5 c23785e43c802c7a6ef584c436f5db5b
BLAKE2b-256 655933370d8624f833961d4b27e50989f714a02678b1c5572d796f0f7cf3388a

See more details on using hashes here.

File details

Details for the file borch-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: borch-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 100.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10

File hashes

Hashes for borch-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 14f1b3e1c4cf0a834665d160bc5a0a79a812d713b160f0d4cc0145d618343553
MD5 fdfae7429a5f9cc20cd0c25abd3a3450
BLAKE2b-256 78b27c7f9145ac4abf9e6c31f31b83ea1691fad5c98ccfc81971b7c2a2dd6222

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