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 . Output (whatever the task demands: a single logit for binary, logits for -class).
A single hidden layer with units computes
with weight matrices and , biases and , and a pointwise activation applied entry-by-entry. For classification we then push 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
3. The general -layer MLP
Generalising the one-hidden-layer case to layers:
with , , and each a pointwise nonlinearity (or the identity for a final regression head). The full network is the composition
Read it out loud once. Every neural network in this course — convolutional, recurrent, Transformer — is structurally this composition, with extra structure imposed on each (sparsity, weight-sharing, attention).[2]
4. Notation we'll keep for the rest of the course
- — number of layers (depth).
- — width of layer ( is the input dimension, the output dimension).
- — pre-activation at layer .
- — activation at layer . .
- — weight matrix mapping layer to layer . Rows index outputs, columns index inputs.
- — bias of layer .
- — pointwise activation (sigmoid, ReLU, GELU, identity, …).
The parameters of the network are , totalling
For a moderate net with and , this is on the order of parameters; for a Transformer LM it grows to . Same notation throughout.
5. The two "linear" layer types coming later
Every layer in this chapter is fully connected: is dense, every output unit sees every input unit. Two cousins we'll meet later:
- Convolutional (Chapter 4). is a structured sparse matrix that re-uses the same small filter across spatial positions. Many fewer parameters for the same .
- Self-attention (Chapter 6). depends on the input itself, — 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.
- Each layer is a learned feature map. 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).
- 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.
- Nonlinearity is essential. Without the 's, every reduces to a single linear map , and we'd be back to the perceptron of Chapter 1. The composition only buys expressive power when at least one 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: .
- The notation carries through the entire course.
- Without nonlinearities, depth gains nothing; XOR is the canonical witness.
(a) For an MLP with widths (MNIST), , , (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 — is equivalent to a single affine layer. Find the equivalent in terms of the per-layer parameters.
(c) Write the one-hidden-layer MLP that solves XOR (use the construction from 1.6 §4 with sigmoids and a large gain). What are the entries of ?
(d) A common mistake: applying the activation 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]
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]
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).
↩