# french-paragon/bayesianmnist architecture

> Architecture diagram of the GitHub repository french-paragon/bayesianmnist, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/french-paragon/bayesianmnist
- Repository: https://github.com/french-paragon/bayesianmnist

## Overview

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`](https://github.com/french-paragon/bayesianmnist/blob/master/viExperiment.py) (Experiment script), [`dataset.py`](https://github.com/french-paragon/bayesianmnist/blob/master/dataset.py) (Dataset module), [`viModel.py`](https://github.com/french-paragon/bayesianmnist/blob/master/viModel.py) (Model module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_experiment["Experiment"]
  node_n_vi_exp["Experiment script<br/>entrypoint<br/>[viExperiment.py]"]
  node_n_cli["Arg parsing<br/>cli"]
  node_n_run(("Train loop<br/>orchestration"))
  node_n_eval(("Evaluation<br/>orchestration"))
end

subgraph group_g_data["Data"]
  node_n_dataset_py["Dataset module<br/>data-module<br/>[dataset.py]"]
  node_n_filter{{"Digit filter<br/>data-policy"}}
  node_n_split(("Train/test split<br/>data-prep"))
end

subgraph group_g_model["Model"]
  node_n_vi_model["Model module<br/>model-module<br/>[viModel.py]"]
  node_n_layers{{"VI layers<br/>variational-layer"}}
  node_n_bnn(("Bayesian net<br/>classifier"))
  node_n_loss["Objective"]
  node_n_uncertainty(("Uncertainty output<br/>prediction"))
end

subgraph group_g_external["External"]
  node_n_mnist[("MNIST data<br/>dataset")]
  node_n_torch["PyTorch stack"]
  node_n_np["NumPy"]
end

subgraph group_g_observe["Observation"]
  node_n_plot["Plots<br/>visualization"]
end

node_n_vi_exp -->|"parses"| node_n_cli
node_n_vi_exp -->|"builds"| node_n_dataset_py
node_n_vi_exp -->|"builds"| node_n_vi_model
node_n_vi_exp -->|"drives"| node_n_run
node_n_run -->|"optimizes"| node_n_loss
node_n_run -->|"hands off"| node_n_eval
node_n_dataset_py -->|"loads"| node_n_mnist
node_n_dataset_py -->|"applies"| node_n_filter
node_n_dataset_py -->|"prepares"| node_n_split
node_n_filter -->|"feeds"| node_n_split
node_n_vi_model -->|"contains"| node_n_layers
node_n_layers -->|"compose"| node_n_bnn
node_n_bnn -->|"trained by"| node_n_loss
node_n_bnn -->|"emits"| node_n_uncertainty
node_n_eval -->|"measures"| node_n_uncertainty
node_n_eval -->|"reports"| node_n_plot
node_n_plot -->|"renders"| node_n_uncertainty
node_n_dataset_py -->|"uses"| node_n_torch
node_n_vi_model -->|"uses"| node_n_torch
node_n_vi_exp -->|"uses"| node_n_np

click node_n_vi_exp "https://github.com/french-paragon/bayesianmnist/blob/master/viExperiment.py"
click node_n_dataset_py "https://github.com/french-paragon/bayesianmnist/blob/master/dataset.py"
click node_n_vi_model "https://github.com/french-paragon/bayesianmnist/blob/master/viModel.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_vi_exp,node_n_cli,node_n_run,node_n_eval toneBlue
class node_n_dataset_py,node_n_filter,node_n_split toneAmber
class node_n_vi_model,node_n_layers,node_n_bnn,node_n_loss,node_n_uncertainty toneMint
class node_n_mnist,node_n_torch,node_n_np toneRose
class node_n_plot toneIndigo
```

## Components

### Experiment

Runtime orchestration and execution flow

- [Experiment script](https://github.com/french-paragon/bayesianmnist/blob/master/viExperiment.py): entrypoint · Command-line driver that wires data, model, training, and evaluation · `viExperiment.py`
- **Arg parsing**: cli · Reads experiment settings and run parameters
- **Train loop**: orchestration · Coordinates fitting over epochs and batches
- **Evaluation**: orchestration · Runs held-out or withheld-digit testing

### Data

MNIST preparation and filtering

- [Dataset module](https://github.com/french-paragon/bayesianmnist/blob/master/dataset.py): data-module · MNIST dataset preparation and class filtering · `dataset.py`
- **Digit filter**: data-policy · Optionally removes one digit from training data
- **Train/test split**: data-prep · Separates training data from evaluation data

### Model

Bayesian neural network and variational inference

- [Model module](https://github.com/french-paragon/bayesianmnist/blob/master/viModel.py): model-module · Variational Bayesian network implementation · `viModel.py`
- **VI layers**: variational-layer · Probabilistic weight layers and uncertainty-aware parameters
- **Bayesian net**: classifier · MNIST classifier with stochastic forward pass
- **Objective**: training-objective · Optimization target for variational training
- **Uncertainty output**: prediction · Predictive uncertainty or confidence behavior

### External

Library and dataset dependencies

- **MNIST data**: dataset · Source image and label data loaded through torchvision
- **PyTorch stack**: library · PyTorch and torchvision runtime for tensors, models, and MNIST loading
- **NumPy**: library · Array and numeric support

### Observation

Metrics and visualization of results

- **Plots**: visualization · Matplotlib-based result inspection and figures

## Connections

- 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
