AI / ML Theory
Chapter 2 · 18 min

2.1 — From perceptrons to layers: the MLP as composition

Goal: Write the MLP as a composition of affine maps and pointwise nonlinearities, with clean layer-wise notation.

1. Why this session exists

Chapter 1 built single-layer models. Chapter 2 builds multilayer models — every neural network in this course will be an instance of the structure we write down here. The whole engine is one diagram and one composition: stack affine maps and pointwise nonlinearities, alternating, and you have an MLP.

The hard part isn't the definition; it's notation. We're going to fix a single, consistent notation now and use it for the rest of the course.[1]


2. One-hidden-layer MLP

Inputs x∈Rd0x \in \mathbb{R}^{d_0}. Output y^∈RdL\hat y \in \mathbb{R}^{d_L} (whatever the task demands: a single logit for binary, KK logits for KK-class).

A single hidden layer with d1d_1 units computes

z(1)=W(1)x+b(1)∈Rd1,a(1)=g(z(1))∈Rd1,y^=W(2)a(1)+b(2)∈Rd2,\begin{aligned} z^{(1)} &= W^{(1)} x + b^{(1)} \in \mathbb{R}^{d_1}, \\ a^{(1)} &= g\bigl(z^{(1)}\bigr) \in \mathbb{R}^{d_1}, \\ \hat y &= W^{(2)} a^{(1)} + b^{(2)} \in \mathbb{R}^{d_2}, \end{aligned}

with weight matrices W(1)∈Rd1×d0W^{(1)} \in \mathbb{R}^{d_1 \times d_0} and W(2)∈Rd2×d1W^{(2)} \in \mathbb{R}^{d_2 \times d_1}, biases b(1)∈Rd1b^{(1)} \in \mathbb{R}^{d_1} and b(2)∈Rd2b^{(2)} \in \mathbb{R}^{d_2}, and a pointwise activation gg applied entry-by-entry. For classification we then push y^\hat y through a sigmoid or softmax; for regression we leave it linear.

For multi-class classification specifically, the final output of the network and the prediction in probability space are

z(L)∈RK(logits),y^=softmax⁡(z(L))∈ΔK−1.z^{(L)} \in \mathbb{R}^K \quad \text{(logits)}, \qquad \hat y = \operatorname{softmax}(z^{(L)}) \in \Delta^{K-1}.

3. The general LL-layer MLP

Generalising the one-hidden-layer case to LL layers:

a(0):=x,{z(ℓ)=W(ℓ)a(ℓ−1)+b(ℓ),a(ℓ)=g(ℓ)(z(ℓ)),ℓ=1,…,L,a^{(0)} := x, \qquad \begin{cases} z^{(\ell)} = W^{(\ell)} a^{(\ell-1)} + b^{(\ell)}, \\ a^{(\ell)} = g^{(\ell)}\bigl(z^{(\ell)}\bigr), \end{cases} \quad \ell = 1, \dots, L,

with W(ℓ)∈Rdℓ×dℓ−1W^{(\ell)} \in \mathbb{R}^{d_\ell \times d_{\ell-1}}, b(ℓ)∈Rdℓb^{(\ell)} \in \mathbb{R}^{d_\ell}, and each g(ℓ)g^{(\ell)} a pointwise nonlinearity (or the identity for a final regression head). The full network is the composition

 f(x)  =  g(L) ⁣(W(L) g(L−1) ⁣(W(L−1)⋯g(1)(W(1)x+b(1))⋯+b(L−1))+b(L)). \boxed{\ f(x) \;=\; g^{(L)}\!\Bigl(W^{(L)}\, g^{(L-1)}\!\bigl(W^{(L-1)} \cdots g^{(1)}(W^{(1)} x + b^{(1)}) \cdots + b^{(L-1)}\bigr) + b^{(L)}\Bigr).\ }

Read it out loud once. Every neural network in this course — convolutional, recurrent, Transformer — is structurally this composition, with extra structure imposed on each W(ℓ)W^{(\ell)} (sparsity, weight-sharing, attention).[2]


4. Notation we'll keep for the rest of the course

  • LL — number of layers (depth).
  • dℓd_\ell — width of layer ℓ\ell (d0d_0 is the input dimension, dLd_L the output dimension).
  • z(ℓ)=W(ℓ)a(ℓ−1)+b(ℓ)z^{(\ell)} = W^{(\ell)} a^{(\ell-1)} + b^{(\ell)} — pre-activation at layer ℓ\ell.
  • a(ℓ)=g(ℓ)(z(ℓ))a^{(\ell)} = g^{(\ell)}(z^{(\ell)}) — activation at layer ℓ\ell. a(0)=xa^{(0)} = x.
  • W(ℓ)∈Rdℓ×dℓ−1W^{(\ell)} \in \mathbb{R}^{d_\ell \times d_{\ell-1}} — weight matrix mapping layer ℓ−1\ell - 1 to layer ℓ\ell. Rows index outputs, columns index inputs.
  • b(ℓ)∈Rdℓb^{(\ell)} \in \mathbb{R}^{d_\ell} — bias of layer ℓ\ell.
  • g(ℓ)g^{(\ell)} — pointwise activation (sigmoid, ReLU, GELU, identity, …).

The parameters of the network are θ={W(ℓ),b(ℓ)}ℓ=1L\theta = \{W^{(\ell)}, b^{(\ell)}\}_{\ell = 1}^L, totalling

#θ=∑ℓ=1L(dℓ dℓ−1+dℓ)scalars.\#\theta = \sum_{\ell = 1}^L \bigl(d_\ell\, d_{\ell - 1} + d_\ell\bigr) \quad \text{scalars}.

For a moderate net with L=6L = 6 and dℓ≈1000d_\ell \approx 1000, this is on the order of 6×1066 \times 10^6 parameters; for a Transformer LM it grows to 101010^{10}. Same notation throughout.


5. The two "linear" layer types coming later

Every layer in this chapter is fully connected: W(ℓ)W^{(\ell)} is dense, every output unit sees every input unit. Two cousins we'll meet later:

  • Convolutional (Chapter 4). W(ℓ)W^{(\ell)} is a structured sparse matrix that re-uses the same small filter across spatial positions. Many fewer parameters for the same dℓ,dℓ−1d_\ell, d_{\ell-1}.
  • Self-attention (Chapter 6). W(ℓ)W^{(\ell)} depends on the input itself, W(ℓ)=f(x)W^{(\ell)} = f(x) — a data-dependent linear layer. Same composition structure, dynamic weights.

The general MLP is the prototype. The rest are special cases.


6. The compositional perspective

Three takeaways from the composition view.

  1. Each layer is a learned feature map. a(ℓ)a^{(\ell)} is a representation of the input. Deeper layers tend to be more abstract; we'll formalise this in 2.2 (universal approximation) and in Chapter 5 (embeddings).
  2. The chain rule is the engine. All of backprop is the chain rule from 0.3 applied to this composition. Nothing new about derivatives; everything new about organising the derivative computation efficiently.
  3. Nonlinearity is essential. Without the g(ℓ)g^{(\ell)}'s, every ff reduces to a single linear map W=W(L)⋯W(1)W = W^{(L)} \cdots W^{(1)}, and we'd be back to the perceptron of Chapter 1. The composition only buys expressive power when at least one g(ℓ)g^{(\ell)} is nonlinear — the XOR result of 1.6 is the prototype.

7. Takeaways

  • An MLP is a composition of affine maps and pointwise nonlinearities.
  • Parameters: θ={W(ℓ),b(ℓ)}\theta = \{W^{(\ell)}, b^{(\ell)}\}.
  • The notation (z(ℓ),a(ℓ),W(ℓ),b(ℓ),g(ℓ))(z^{(\ell)}, a^{(\ell)}, W^{(\ell)}, b^{(\ell)}, g^{(\ell)}) carries through the entire course.
  • Without nonlinearities, depth gains nothing; XOR is the canonical witness.

Self-test

(a) For an MLP with widths d0=784d_0 = 784 (MNIST), d1=256d_1 = 256, d2=128d_2 = 128, d3=10d_3 = 10 (one hidden block of two layers + softmax head), count the total number of parameters layer by layer.

(b) Show that an MLP with no nonlinearities — every g(ℓ)=id⁡g^{(\ell)} = \operatorname{id} — is equivalent to a single affine layer. Find the equivalent (W,b)(W, b) in terms of the per-layer parameters.

(c) Write the one-hidden-layer MLP f(x)=W(2)σ(W(1)x+b(1))+b(2)f(x) = W^{(2)} \sigma(W^{(1)} x + b^{(1)}) + b^{(2)} that solves XOR (use the construction from 1.6 §4 with sigmoids and a large gain). What are the entries of W(1),b(1),W(2),b(2)W^{(1)}, b^{(1)}, W^{(2)}, b^{(2)}?

(d) A common mistake: applying the activation gg after the output of the last layer for a regression task. Why is that wrong, and what is the correct convention for regression heads?


References

  1. [1]

    Wang, S., & Kanwar, P. (2019). "BFloat16: The secret to high performance on Cloud TPUs". Google Cloud blog. cloud.google.com — bf16 design rationale (7.4 §2.1).

    ↩
  2. [2]

    Radford, A., Wu, J., Child, R., et al. (2019). "Language models are unsupervised multitask learners" (GPT-2). OpenAI technical report. — Pre-LN block, modified init, reversible tokenization referenced by GPT-3 §2.1 (7.14 §3.1).

    ↩