Abstract
We introduce TyXe, a Bayesian neural network library built on top of PyTorch and Pyro. Our leading design principle is to cleanly separate architecture, prior, inference, and likelihood specification, allowing for a flexible workflow where users can quickly iterate over combinations of these components. In contrast to existing packages, TyXe does not implement any layer classes and instead relies on architectures defined in generic PyTorch code. TyXe then provides modular choices for canonical priors, variational guides, inference techniques, and layer selections for a Bayesian treatment of the specified architecture. Sampling tricks for variance reduction, such as local reparameterization or flipout, are implemented as effect handlers, which can be applied independently of other specifications. We showcase the ease of use of TyXe to explore Bayesian versions of popular models from various libraries: toy regression with a pure PyTorch neural network; large-scale image classification with torchvision ResNets; graph neural networks based on DGL; and Neural Radiance Fields built on top of PyTorch3D. Finally, we provide convenient abstractions for variational continual learning. In all cases, the change from a deterministic to a Bayesian neural network comes with minimal modifications to existing code, offering a broad range of researchers and practitioners alike practical access to uncertainty estimation techniques.
Introduction
Deep learning's rapid advancement is largely due to the flexibility of frameworks like PyTorch, which allow for easy architectural experimentation. However, incorporating Bayesian principles—enabling models to quantify uncertainty—remains technically challenging. Traditionally, creating a Bayesian version of a standard model (like a ResNet) required replacing standard layers with specialized Bayesian counterparts (e.g., nn.Linear to BayesianLinear). This approach is brittle, difficult to maintain, and often fails to incorporate essential variance-reduction techniques like local reparameterization or flipout. TyXe addresses this "software gap" by providing a bridge between PyTorch's architectural flexibility and Pyro's robust probabilistic inference.
Methodology: A Layer-Free Approach
TyXe (pronounced like the Greek word for "chance") introduces a "layer-free" design. Rather than providing new building blocks, it leverages Pyro’s poutine library to use effect handlers that modify the behavior of standard PyTorch layers at runtime. This allows TyXe to "monkey-patch" functions like F.linear or F.conv2d during training, implementing complex transformations as global program modifications rather than hard-coded layer logic.
The TyXe workflow decouples four core components:
- Architecture: Any standard
nn.Module. - Prior: A distribution over weights (e.g., standard Normal).
- Likelihood: The relationship between model output and data (e.g., Categorical).
- Guide: The strategy for posterior approximation (e.g., Variational Inference or MCMC).
This decoupling allows for "Selective Bayesianism," where users can easily choose which parts of a network to treat as Bayesian. For instance, keeping BatchNorm layers deterministic while placing priors on fully connected layers often improves stability. Furthermore, because the prior is a first-class object, TyXe naturally supports Variational Continual Learning (VCL) by using the posterior of a previous task as the prior for the next.
Experimental Evaluation
The authors evaluated TyXe across diverse domains, demonstrating its versatility and effectiveness in uncertainty estimation. Key benchmarks included CIFAR-10 for standard testing, SVHN for Out-of-Distribution (OOD) detection, and Split-MNIST for continual learning.
Key Results
- Calibration and OOD Detection: On CIFAR-10, a Bayesian ResNet-18 using TyXe’s Mean-Field (MF) inference achieved an Expected Calibration Error (ECE) of 0.97%, significantly lower than the 4.10% of standard Maximum Likelihood (ML) training. When tested on OOD data (SVHN), Bayesian models were far more effective at signaling uncertainty, whereas standard models were often confidently incorrect.
- Robust 3D Rendering: In Neural Radiance Fields (NeRF) tasks using PyTorch3D, the Bayesian version handled unseen camera angles with fewer artifacts and provided uncertainty maps highlighting areas of model struggle.
- Continual Learning: In Split-MNIST benchmarks, TyXe’s VCL implementation successfully mitigated "catastrophic forgetting," maintaining high accuracy across sequential tasks where deterministic networks failed.
Conclusion and Future Work
TyXe simplifies the transition from deterministic to Bayesian deep learning, making it a plug-and-play experience for the PyTorch ecosystem. While currently focused on weight-space priors, future work aims to support more scalable MCMC methods like SGLD, implement Monte Carlo Dropout as an effect handler, and explore moment propagation to further accelerate inference. The library is available at https://github.com/TyXe-BDL/TyXe.
Summary updated