|
| 1 | +# Architecture & Design (Python) |
| 2 | + |
| 3 | +This document describes the high-level architecture of the `gate_learner` Python package. |
| 4 | + |
| 5 | +## Module Overview |
| 6 | + |
| 7 | +The project is structured as a decoupled library with a Typer CLI wrapper. This separation ensures that the core mathematical engine remains independent of I/O, CLI parsing, and plotting logic. |
| 8 | + |
| 9 | +```text |
| 10 | ++-----------------------------------------------------------------------+ |
| 11 | +| CLI Module | |
| 12 | +| (gate_learner/cli.py, main) | |
| 13 | ++-----------------------------------+-----------------------------------+ |
| 14 | + | |
| 15 | + v |
| 16 | ++-----------------------------------+-----------------------------------+ |
| 17 | +| Dataset Generator | |
| 18 | +| (gate_learner/dataset.py) | |
| 19 | ++-----------------------------------+-----------------------------------+ |
| 20 | + | |
| 21 | + v |
| 22 | ++-----------------------------------+-----------------------------------+ |
| 23 | +| Core ML Engine | |
| 24 | +| (gate_learner/core.py) | |
| 25 | +| +---------------------------------------------------------------+ | |
| 26 | +| | MultilayerPerceptron | | |
| 27 | +| | - Forward Pass (Sigmoid / ReLU Activation) | | |
| 28 | +| | - Gradient Accumulation (BCE Loss) | | |
| 29 | +| +-------------------------------+-------------------------------+ | |
| 30 | +| | | |
| 31 | +| v | |
| 32 | +| +---------------------------------------------------------------+ | |
| 33 | +| | AdamOptimizer | | |
| 34 | +| | - Parameter Update (L2 Regularisation & Bias Correction) | | |
| 35 | +| +-------------------------------+-------------------------------+ | |
| 36 | ++-----------------------------------+-----------------------------------+ |
| 37 | + | |
| 38 | + +-----------------------+ |
| 39 | + | | |
| 40 | + v v |
| 41 | ++-----------------------------------+---+ +---+--------------------+ |
| 42 | +| Storage Module | | Plot Module (Feature) | |
| 43 | +| (gate_learner/storage.py) | | (gate_learner/plot.py) | |
| 44 | ++---------------------------------------+ +------------------------+ |
| 45 | +``` |
| 46 | + |
| 47 | +### 1. Core ML Engine (`core.py`) |
| 48 | +Contains the mathematical representation of the neural network. |
| 49 | +- **`Layer`**: Represents a single layer containing weights, biases, and an activation function. |
| 50 | +- **`MultilayerPerceptron`**: Manages a sequence of layers. It holds pre-allocated internal buffers for activations, deltas, and gradients to avoid list allocations during the training loop. |
| 51 | +- **`AdamOptimizer`**: Implements the Adam optimization algorithm. It maintains its own state buffers (first and second moments) matching the network's architecture. |
| 52 | + |
| 53 | +### 2. Dataset Generator (`dataset.py`) |
| 54 | +Generates truth tables for $N$-input OR and XOR gates. It includes safety checks to prevent memory exhaustion by limiting $N < 20$ (since dataset size scales exponentially as $2^N$). |
| 55 | + |
| 56 | +### 3. Storage Module (`storage.py`) |
| 57 | +Handles JSON serialisation and deserialisation of the trained model weights and training history. |
| 58 | + |
| 59 | +### 4. Plot Module (`plot.py`) |
| 60 | +Gated under optional `matplotlib` import. It renders a dual-axis PNG chart showing the training loss and accuracy curves over epochs. |
| 61 | + |
| 62 | +### 5. CLI Module (`cli.py` & `main.py`) |
| 63 | +Parses command-line arguments using `typer`, coordinates the training loop, manages early stopping, and handles output generation. |
| 64 | + |
| 65 | +## Key Architectural Decisions |
| 66 | + |
| 67 | +### Memory Management & Allocation |
| 68 | +To achieve high performance in pure Python, list allocations are avoided inside the training loop. |
| 69 | +- The `MultilayerPerceptron` allocates its activation, delta, and gradient buffers once during initialization via `ensure_buffers()`. |
| 70 | +- During the forward and backward passes, these buffers are mutated in-place. |
| 71 | + |
| 72 | +### Concurrency & Serialization |
| 73 | +The `MultilayerPerceptron` and `Layer` classes are designed to be fully picklable (serialisable). This allows the model to be safely transferred across processes using Python's `multiprocessing` module for parallel hyperparameter grid search. |
0 commit comments