Neural Networks
March 25, 2026 · View on GitHub
Overview & Motivation
A neural network is a parameterized function built by composing simple, differentiable transformations called layers. Each layer applies an affine map followed by a non-linear activation, and the whole composition is trained end-to-end by gradient-based optimization.
Neural networks are powerful because of the universal approximation theorem: a single hidden layer with enough neurons can approximate any continuous function on a compact set to arbitrary accuracy. In practice, depth (many layers) is more parameter-efficient than width for learning hierarchical features.
This library provides a minimal, statically-sized neural network framework designed for embedded inference and on-device training — no heap allocation, no dynamic shapes, full compile-time dimension checking.
Mathematical Theory
Forward Propagation
Given layers, the network computes:
where are weights, are biases, and is the activation function for layer .
Loss Function
Training minimizes a scalar loss that measures how far the prediction is from the target . Common choices:
| Loss | Formula | Use Case |
|---|---|---|
| MSE | Regression | |
| BCE | Binary classification | |
| CCE | Multi-class classification |
Backpropagation
Backpropagation efficiently computes via the chain rule, working from the output layer backward:
The gradients with respect to parameters are:
Parameter Update
An optimizer uses the gradients to update the parameter vector :
where is the learning rate. More sophisticated optimizers (momentum, Adam) modify this basic rule.
Complexity Analysis
| Phase | Time | Space |
|---|---|---|
| Forward pass | activations | |
| Backward pass | Same as forward | Same + gradient storage |
| Parameter update | optimizer state |
where is the total parameter count. For small embedded networks (), both passes complete in microseconds.
Step-by-Step Walkthrough
Network: 2 inputs → 2 hidden (ReLU) → 1 output (Sigmoid). Learning XOR.
Architecture:
graph LR
x1((x₁)) --> h1((h₁))
x1 --> h2((h₂))
x2((x₂)) --> h1
x2 --> h2
h1 --> y((ŷ))
h2 --> y
Epoch 0 — Forward pass with input , target :
| Step | Computation | Result |
|---|---|---|
| Hidden pre-activation | ||
| Hidden activation | ||
| Output pre-activation | ||
| Output activation | ||
| Loss | $0.621$ |
Epoch 0 — Backward pass:
| Step | Computation | Result |
|---|---|---|
| Output gradient | ||
| Hidden gradient | ||
Update: . After ~500 epochs, the network correctly classifies all four XOR inputs.
Pitfalls & Edge Cases
- Vanishing gradients. Deep networks with Sigmoid or Tanh activations suffer exponential gradient decay. Prefer ReLU-family activations in hidden layers.
- Exploding gradients. Large weights amplify gradients exponentially. Use proper weight initialization (Xavier/He) and gradient clipping.
- Dead neurons. ReLU neurons that receive only negative inputs output zero forever. LeakyReLU mitigates this.
- Fixed-point saturation. In Q15/Q31, activations and gradients must stay within . Scale inputs and learning rates accordingly.
- Learning rate sensitivity. Too high → divergence; too low → no progress. Start with and adjust.
- Overfitting. Small embedded datasets are easily memorized. Apply regularization (L2 weight decay).
Variants & Generalizations
| Variant | Key Difference |
|---|---|
| Convolutional Neural Network (CNN) | Layers share weights spatially; efficient for image/signal data |
| Recurrent Neural Network (RNN) | Layers share weights across time steps; models sequences |
| Residual Network (ResNet) | Skip connections mitigate vanishing gradients in very deep networks |
| Transformer | Attention-based; no recurrence; state-of-the-art for sequences |
| Quantized Neural Network | Weights and activations in low-bit integers; optimal for MCU deployment |
Applications
- Function approximation — Learning arbitrary input-output mappings from data.
- Classification — Mapping inputs to discrete categories (fault detection, gesture recognition).
- Regression — Predicting continuous values (sensor calibration, system identification).
- Control — Neural network policies for model-free or model-predictive control on embedded targets.
- Signal processing — Learned filters replacing hand-designed FIR/IIR chains.
Connections to Other Algorithms
graph TD
NN["Neural Network"]
Layer["Dense Layer"]
Act["Activation Functions"]
Loss["Loss Functions"]
Opt["Optimizer"]
Reg["Regularization"]
Model["Model"]
LR["Linear Regression"]
Layer --> NN
Act --> NN
Loss --> NN
Opt --> NN
Reg --> NN
NN --> Model
NN -.->|"single-layer, linear activation, MSE loss"| LR
| Component | Relationship |
|---|---|
| Dense Layer | The fundamental building block; computes affine transformations |
| Activation Functions | Introduce non-linearity after each layer |
| Loss Functions | Define the training objective |
| Optimizer | Drives parameter updates via gradient descent |
| Regularization | Penalizes complexity to prevent overfitting |
| Model | Composes layers into a trainable pipeline |
| Linear Regression | Special case: single layer, identity activation, MSE loss |
References & Further Reading
- Goodfellow, I., Bengio, Y., and Courville, A., Deep Learning, MIT Press, 2016 — Chapters 6–8.
- He, K. et al., "Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification", ICCV, 2015 — He initialization.
- Rumelhart, D.E., Hinton, G.E., and Williams, R.J., "Learning representations by back-propagating errors", Nature, 323, 1986.