Neural Networks

March 25, 2026 · View on GitHub

Overview & Motivation

A neural network is a parameterized function fθ:RnRmf_\theta: \mathbb{R}^n \to \mathbb{R}^m 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 LL layers, the network computes:

a0=xa_0 = x z=Wa1+b,=1,,Lz_\ell = W_\ell \, a_{\ell-1} + b_\ell, \quad \ell = 1, \ldots, L a=f(z)a_\ell = f_\ell(z_\ell) y^=aL\hat{y} = a_L

where WRn×n1W_\ell \in \mathbb{R}^{n_\ell \times n_{\ell-1}} are weights, bRnb_\ell \in \mathbb{R}^{n_\ell} are biases, and ff_\ell is the activation function for layer \ell.

Loss Function

Training minimizes a scalar loss L(y^,y)\mathcal{L}(\hat{y}, y) that measures how far the prediction y^\hat{y} is from the target yy. Common choices:

LossFormulaUse Case
MSE1m(y^iyi)2\frac{1}{m}\sum(\hat{y}_i - y_i)^2Regression
BCE[yilogy^i+(1yi)log(1y^i)]-\sum[y_i \log \hat{y}_i + (1-y_i)\log(1-\hat{y}_i)]Binary classification
CCEyilogy^i-\sum y_i \log \hat{y}_iMulti-class classification

Backpropagation

Backpropagation efficiently computes θL\nabla_\theta \mathcal{L} via the chain rule, working from the output layer backward:

δL=aLLfL(zL)\delta_L = \nabla_{a_L}\mathcal{L} \odot f_L'(z_L) δ=(W+1Tδ+1)f(z)\delta_\ell = (W_{\ell+1}^T \delta_{\ell+1}) \odot f_\ell'(z_\ell)

The gradients with respect to parameters are:

LW=δa1T,Lb=δ\frac{\partial \mathcal{L}}{\partial W_\ell} = \delta_\ell \, a_{\ell-1}^T, \qquad \frac{\partial \mathcal{L}}{\partial b_\ell} = \delta_\ell

Parameter Update

An optimizer uses the gradients to update the parameter vector θ\theta:

θt+1=θtηθL\theta_{t+1} = \theta_t - \eta \, \nabla_\theta \mathcal{L}

where η\eta is the learning rate. More sophisticated optimizers (momentum, Adam) modify this basic rule.

Complexity Analysis

PhaseTimeSpace
Forward passO ⁣(=1Lnn1)O\!\left(\sum_{\ell=1}^L n_\ell \cdot n_{\ell-1}\right)O ⁣(n)O\!\left(\sum n_\ell\right) activations
Backward passSame as forwardSame + gradient storage
Parameter updateO(P)O(P)O(P)O(P) optimizer state

where P=(nn1+n)P = \sum_\ell (n_\ell \cdot n_{\ell-1} + n_\ell) is the total parameter count. For small embedded networks (P<10,000P < 10{,}000), 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 x=[1,0]Tx = [1, 0]^T, target y=1y = 1:

StepComputationResult
Hidden pre-activationz1=W1x+b1z_1 = W_1 x + b_1[0.3,0.1]T[0.3, -0.1]^T
Hidden activationa1=ReLU(z1)a_1 = \text{ReLU}(z_1)[0.3,0.0]T[0.3, 0.0]^T
Output pre-activationz2=W2a1+b2z_2 = W_2 a_1 + b_2[0.15][0.15]
Output activationy^=σ(z2)\hat{y} = \sigma(z_2)[0.537][0.537]
LossL=(ylogy^+(1y)log(1y^))\mathcal{L} = -(y\log\hat{y} + (1-y)\log(1-\hat{y}))$0.621$

Epoch 0 — Backward pass:

StepComputationResult
Output gradientδ2=y^y\delta_2 = \hat{y} - y[0.463][-0.463]
W2\nabla W_2δ2a1T\delta_2 \cdot a_1^T[0.139,0][-0.139, 0]
Hidden gradientδ1=W2Tδ2ReLU(z1)\delta_1 = W_2^T \delta_2 \odot \text{ReLU}'(z_1)[0.231,0]T[-0.231, 0]^T
W1\nabla W_1δ1xT\delta_1 \cdot x^T[[0.231,0],[0,0]][[-0.231, 0], [0, 0]]

Update: WW0.1WW \leftarrow W - 0.1 \cdot \nabla W. 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 [1,1)[-1, 1). Scale inputs and learning rates accordingly.
  • Learning rate sensitivity. Too high → divergence; too low → no progress. Start with η=0.01\eta = 0.01 and adjust.
  • Overfitting. Small embedded datasets are easily memorized. Apply regularization (L2 weight decay).

Variants & Generalizations

VariantKey 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
TransformerAttention-based; no recurrence; state-of-the-art for sequences
Quantized Neural NetworkWeights 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
ComponentRelationship
Dense LayerThe fundamental building block; computes affine transformations
Activation FunctionsIntroduce non-linearity after each layer
Loss FunctionsDefine the training objective
OptimizerDrives parameter updates via gradient descent
RegularizationPenalizes complexity to prevent overfitting
ModelComposes layers into a trainable pipeline
Linear RegressionSpecial 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.