How french-paragon/bayesianmnist works
This repository is a small, self-contained research/demo project for Bayesian neural networks on MNIST. Its architecture is centered around one experiment script that wires together data preparation and a variational-inference model.
1. Overall structure
The code is split into three functional units: dataset.py for data handling, viModel.py for the Bayesian model, and viExperiment.py for experiment execution. This is a single-process Python/PyTorch application, not a multi-service system.
2. Experiment orchestration
viExperiment.py is the entry point. It parses command-line arguments, builds the dataset and model, runs training/evaluation, and likely handles result reporting or plotting. This file is the top-level integration boundary for the whole repo.
3. Data subsystem
dataset.py encapsulates MNIST-specific dataset preparation. The README says it can filter out one digit during training so the model can later be tested on a digit it has not seen. That makes data filtering a deliberate experimental feature, not just preprocessing.
4. Model subsystem
viModel.py contains the variational-inference layers and the Bayesian neural network used for classification. This is the core modeling boundary: it owns the probabilistic weights / uncertainty-aware forward pass rather than a standard deterministic network.
5. Data flow
The runtime flow is: MNIST data is loaded and optionally filtered in dataset.py, the filtered tensors are fed into the Bayesian model from viModel.py, and viExperiment.py coordinates training and later evaluation on held-out or withheld digits. The experiment script is the bridge between dataset policy and model behavior.
6. Experimental concern: out-of-distribution exposure
A key architectural idea is the “withhold one digit, then present it later” setup. This means the repository is designed to study how uncertainty-aware classification behaves when the model encounters a class excluded from training.
7. Technology stack
The implementation depends on Python 3.6+, NumPy, PyTorch, torchvision, and matplotlib, plus standard-library argparse and os. PyTorch and torchvision imply the model and MNIST dataset loading are built around the PyTorch data/model ecosystem.
8. External dependency boundary
The only meaningful external data source is MNIST via torchvision. There are no databases, APIs, message queues, or cloud services. That keeps the architecture local and reproducible.
9. Output/inspection path
Matplotlib is included, so the experiment likely produces visualizations of training or predictive behavior. In architectural terms, this is the observation layer: it turns model outputs into human-readable experimental results.
10. What is not present
There is no separate package structure, no web interface, no persistent storage layer, and no deployment topology. The repo is best diagrammed as a simple pipeline: command-line experiment runner → filtered MNIST data → variational BNN → metrics/plots.
Main entry points: viExperiment.py Experiment script · dataset.py Dataset module · viModel.py Model module
Components 16
Experiment
Runtime orchestration and execution flow
- Experiment script · Command-line driver that wires data, model, training, and evaluation
viExperiment.py - Arg parsing · Reads experiment settings and run parameters
- Train loop · Coordinates fitting over epochs and batches
- Evaluation · Runs held-out or withheld-digit testing
Data
MNIST preparation and filtering
- Dataset module · MNIST dataset preparation and class filtering
dataset.py - Digit filter · Optionally removes one digit from training data
- Train/test split · Separates training data from evaluation data
Model
Bayesian neural network and variational inference
- Model module · Variational Bayesian network implementation
viModel.py - VI layers · Probabilistic weight layers and uncertainty-aware parameters
- Bayesian net · MNIST classifier with stochastic forward pass
- Objective · Optimization target for variational training
- Uncertainty output · Predictive uncertainty or confidence behavior
External
Library and dataset dependencies
- MNIST data · Source image and label data loaded through torchvision
- PyTorch stack · PyTorch and torchvision runtime for tensors, models, and MNIST loading
- NumPy · Array and numeric support
Observation
Metrics and visualization of results
- Plots · Matplotlib-based result inspection and figures
Connections 20
- Experiment script → Arg parsing: parses · Reads command-line options before execution
- Experiment script → Dataset module: builds · Creates the dataset pipeline for the run
- Experiment script → Model module: builds · Instantiates the Bayesian model
- Experiment script → Train loop: drives · Starts training coordination
- Train loop → Objective: optimizes · Uses variational objective during fitting
- Train loop → Evaluation: hands off · Transitions from training to evaluation
- Dataset module → MNIST data: loads · Pulls MNIST data via torchvision
- Dataset module → Digit filter: applies · Optionally withholds one digit from training
- Dataset module → Train/test split: prepares · Produces training and evaluation partitions
- Digit filter → Train/test split: feeds · Filtered data flows into split preparation
- Model module → VI layers: contains · Defines variational parameterized layers
- VI layers → Bayesian net: compose · Builds the classifier from probabilistic layers
- Bayesian net → Objective: trained by · Model parameters are updated against the objective
- Bayesian net → Uncertainty output: emits · Produces predictions with uncertainty behavior
- Evaluation → Uncertainty output: measures · Checks behavior on withheld or unseen digits
- Evaluation → Plots: reports · Visualizes experimental outcomes
- Plots → Uncertainty output: renders · Turns model outputs into human-readable figures
- Dataset module → PyTorch stack: uses · Relies on PyTorch/torchvision ecosystem
- Model module → PyTorch stack: uses · Implements the Bayesian network in PyTorch
- Experiment script → NumPy: uses · Uses NumPy for numeric utilities