Foundations to Frontier
Tests
E. Deep Learning › E0 Core Concepts (concept pages)

Forward Propagation

Forward propagation is how a neural network turns an input into an output: each layer multiplies the previous layer's values by a weight matrix, adds a bias, and applies an activation, and this repeats until the last layer. Written for a whole batch at once, every layer is one matrix product, which is why GPUs run networks so well. This page covers the layer equations and their shapes, batching and the batch-first convention, counting parameters and floating-point operations, caching activations for the backward pass, and the numerics of computing in float32 and float16. In the exercises you implement the batched forward pass of a multilayer perceptron, check it against a neuron-by-neuron loop and count its cost, then measure how rounding error and overflow in low precision depend on depth and weight scale.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

An assembly line for numbers

Forward propagation passes an input through the network like a product along an assembly line. Each station (layer) takes what the previous one produced, mixes it with its own weights, applies its activation and hands the result on. The last station’s output is the prediction.

Forward propagation: values flow layer by layer
0.50-0.301.120.000.000.420.310.000.600.68input (B×2)ReLU (B×4)ReLU (B×3)σ (B×1)
output over x1, x2 ∈ [−2, 2]
z = 0.50×1.65 + -0.30×-0.26 + 0.21 = 1.12 → ReLU → 1.116
Each layer takes the previous layer's values, forms weighted sums plus biases (one matrix product for the whole batch), and applies its activation. ReLU units with a negative sum output exactly 0 (grey), and their outgoing edges then carry nothing. Every input point gets one output, and together they paint the map on the right: the network's function is built entirely from these sums and kinks.
Why engineers care

Inference on a robot, a satellite or a drone is pure forward propagation, repeated many times per second under tight power and timing budgets. Knowing its shapes, its cost in operations and memory, and how it behaves in reduced precision is what decides whether a model fits on the target hardware.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Forward propagation computes…
  2. 2. Each layer combines its inputs using…
  3. 3. For a batch of examples, one layer is computed as…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Layer equations and shapes

One layer. H_l = φ(H_(l−1) W_l + b_l). With batch size B:

  • H_(l−1) is B × n_(l−1);
  • W_l is n_(l−1) × n_l;
  • b_l has length n_l and is added to every row (broadcasting);
  • H_l is B × n_l.

Example. For inputs X = [[1, 2], [0, −1]], weights W = [[1, 0, 2], [−1, 1, 0]] and bias b = (0.5, 0, −1), XW + b = [[−0.5, 2, 1], [1.5, −1, −1]]. ReLU then sets the negatives to 0.

Shapes for 784 → 256 → 10 with batch 32: (32, 784) → (32, 256) → (32, 10). The batch dimension stays first throughout.

Cost.

  • A layer with n_in inputs and n_out outputs needs n_in·n_out multiply–adds (2 FLOPs each) plus n_out bias additions, per example.
  • For 784 → 256 → 128 → 10 that is 469 898 FLOPs per example, with 235 146 parameters.

The output layer is usually linear (no activation), and it is followed by a task-specific function: softmax for classes, identity for regression.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Shape of W for a layer from 128 to 64 units (batch-first)?
  2. 2. FLOPs per example for a 10 → 5 layer?
  3. 3. Broadcasting adds the bias…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Batching, caching and precision

Batching turns many matrix–vector products into one matrix–matrix product, which reuses each weight across the batch. The arithmetic intensity rises, so hardware runs near its peak (C5). The result is identical, up to rounding, to processing examples one by one.

Caching for training. Backpropagation (E0-3) needs each layer’s input H_(l−1) and pre-activation Z_l. Training therefore stores them all during the forward pass. This activation memory is B × Σ n_l numbers, and it often dominates GPU memory for large batches.

Exercise 1. A 4 → 8 → 8 → 3 MLP mapped a batch of 5 through the shapes (5, 4), (5, 8), (5, 8), (5, 3). The batched version matched a neuron-by-neuron loop to rounding error. A 784 → 256 → 128 → 10 network needed 469 898 FLOPs and 235 146 parameters per example.

Precision.

  • float32 keeps about 7 significant digits, up to about 3.4 × 10³⁸.
  • float16 keeps about 3 significant digits, and its largest finite value is only 65 504.
  • bfloat16 keeps float32’s range with only about 2 digits.

Mixed-precision training (C5-5) computes most of the forward pass in 16 bits.

import torch.nn as nn
model = nn.Sequential(nn.Linear(784, 256), nn.ReLU(), nn.Linear(256, 128), nn.ReLU(), nn.Linear(128, 10))
logits = model(x)                       # x: (batch, 784) → (batch, 10)
🐍 Exercise 1: batched forward propagation
Initialise an MLP, implement the batched forward pass with ReLU hidden layers (keeping activations), check it against an explicit per-neuron loop, and count shapes, parameters and FLOPs.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Training stores activations because…
  2. 2. float16's largest finite value is…
  3. 3. Batching speeds up forward propagation because…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Function composition. An L-layer network computes f(x) = f_L ∘ … ∘ f₁(x). Each f_l is affine followed by an elementwise nonlinearity. With ReLU, f is continuous and piecewise linear, and the number of linear pieces can grow exponentially with depth (Montúfar et al., 2014).

Positive homogeneity. ReLU satisfies max(cz, 0) = c·max(z, 0) for c > 0. So scaling all weights of a bias-free ReLU network by c scales its output by c^L. Exercise 2 uses this: doubling the weights left the relative rounding errors exactly unchanged, because multiplying by 2 is exact in binary floating point.

Rounding error. Each operation adds a relative error of up to the unit roundoff u (about 6 × 10⁻⁸ in float32 and 4.9 × 10⁻⁴ in float16). In a dot product of length n, worst-case errors grow like nu. In practice they grow more like √n·u, because the errors are random, and they accumulate only slowly over layers.

Range. Activations of a poorly scaled network grow or shrink geometrically with depth. Normalisation layers (E5), careful initialisation (E0-7) and loss scaling keep them inside the representable range.

Interview questions.

  • What limits the batch size at inference? Latency requirements and memory for the activations. Weights are shared across the batch.
  • Why use bfloat16 over float16? Same 8-bit exponent as float32, hence no overflow worries, at the cost of precision.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Scaling all weights of an L-layer bias-free ReLU network by c scales its output by…
  2. 2. Unit roundoff of float16 is about…
  3. 3. bfloat16 differs from float16 by…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How precision and scale limit the forward pass

History.

  • D. Rumelhart, G. Hinton and R. Williams (1986) popularised multilayer networks with the layer-by-layer forward/backward computation.
  • GPU training (Raina, Madhavan and Ng, 2009; Krizhevsky et al., 2012) made batched forward passes fast.
  • P. Micikevicius et al. (2018) introduced mixed-precision training with loss scaling.

Exercise 2. ReLU MLPs of width 128 and batch 64, with weights at 1, 1.5 or 2 times the He scale. The table compares float32 and float16 outputs with float64, averaged over 3 networks.

weight scale × He depth float32 relative error float16 relative error largest output (float64)
1.0 2 2.49 × 10⁻⁷ 4.53 × 10⁻⁴ 5.31
1.0 8 4.63 × 10⁻⁷ 8.91 × 10⁻⁴ 4.02
1.0 32 9.02 × 10⁻⁷ 1.86 × 10⁻³ 4.87
1.5 8 4.59 × 10⁻⁷ 8.86 × 10⁻⁴ 103
1.5 32 9.29 × 10⁻⁷ overflow 2.1 × 10⁶
2.0 8 4.63 × 10⁻⁷ 8.91 × 10⁻⁴ 1030
2.0 32 9.02 × 10⁻⁷ overflow 2.09 × 10¹⁰
  • Rounding error grew slowly. Going from 2 to 32 layers multiplied it by only about 4 in both precisions (roughly √depth), and even 32 float16 layers kept outputs accurate to about 0.2%.
  • Range, not rounding, was float16’s failure. With weights just 1.5 times too large, activations grew by about 1.5 per layer and passed 65 504 before layer 32. Every output became infinite, while float32 handled the same network without trouble.
  • The scale did not change relative error. For a bias-free ReLU network, scaling the weights by 2 gave identical relative errors to the 1.0 rows, since doubling is exact in binary arithmetic. Only the magnitude, and with it the overflow risk, changed.
🐍 Exercise 2 (research): precision, depth and scale
Run ReLU MLPs of depth 2–32 with weights at 1, 1.5 and 2 times the He scale in float64, float32 and float16; measure relative output error and detect overflow.

Forward propagation in AI, robotics and aerospace

  • Real-time inference: counting FLOPs and activation memory per frame decides whether a perception network meets a 30 Hz budget on embedded hardware.
  • Low-precision deployment: float16 or int8 inference saves power, provided that activation ranges are checked, as exercise 2 shows.
  • Determinism: batched computation and reduced precision change results slightly. Certification often fixes the precision, the batch size and the operation order.

References

  1. D. E. Rumelhart, G. E. Hinton, R. J. Williams (1986). “Learning representations by back-propagating errors.” Nature 323.
  2. G. Montúfar, R. Pascanu, K. Cho, Y. Bengio (2014). “On the number of linear regions of deep neural networks.” Advances in NeurIPS 27.
  3. P. Micikevicius et al. (2018). “Mixed precision training.” Proc. ICLR.
  4. N. J. Higham (2002). Accuracy and Stability of Numerical Algorithms, 2nd ed. SIAM.