6.5.2 · D1Research Frontiers & Practice

Foundations — Implementing models from scratch

1,969 words9 min readBack to topic

Everything on the parent page Implementing models from scratch is built out of a small pile of symbols. This page opens that pile and hands you each piece, in the order you need them. Nothing here assumes you have seen the notation before.


1. A number, a vector, a matrix

Before any layer, we must agree on what the letters even hold.

We write a vector in bold lowercase () and a matrix in bold uppercase (). A plain italic letter with a subscript, like , means "the -th number inside the vector ." So is the whole stack; is the second box.

Why the topic needs this: the input to a network (say, the 784 pixel-brightnesses of an image) is a vector. A layer's parameters are a matrix. Reading correctly is impossible until "vector" and "matrix" mean something concrete.

For a matrix we write : a grid with rows and columns. Read it left-to-right as "rows by columns," always.


2. Multiplying a matrix by a vector

This is the single most-used operation on the parent page. Let us build it from dots.

A matrix times a vector just does this dot product once per row. Row of dotted with produces entry of the answer.

Why the topic needs this: is the forward pass of one layer. If dot products are foggy, every equation downstream is fog. See 2.3.01-Matrix-Operations for the mechanical drill.


3. The transpose

Why the topic needs this: in backprop the parent writes . The forward pass sent information using ; to push the gradient back the other way, , we reuse the same numbers but with rows and columns swapped. The transpose is the "reverse gear" of a matrix multiply. It also fixes shapes: is , so is , which is exactly the shape that turns an -tall gradient back into an -tall one.


4. Adding a bias, and broadcasting

When we process many samples at once, the input becomes a matrix (one column per sample). We still want to add the same to every column. Copying automatically across all columns is called broadcasting.


5. The non-linearity

The little tick mark in means the derivative of — how steeply the curve rises at a point. We meet derivatives next.


6. The derivative and the gradient

Neural networks have millions of inputs (the parameters), not one. The multi-input version of a derivative is the gradient.

Here is the loss — a single number measuring how wrong the network's prediction is (bigger = worse). And (Greek "theta", bold because it's all the parameters bundled together) stands for every and in the network at once.

Why the topic needs this: learning is rolling downhill on the loss surface. The whole "backward pass" exists only to compute this gradient. Foundations in 4.2.02-Gradient-Descent.


7. Partial derivatives and the chain rule

The symbol (Greek "delta") is just a nickname for the gradient that has already flowed back to a layer's pre-activation, . And means element-wise multiply — multiply two equal-shaped grids box-by-box (no dot-product summing), used when a curve's local slope gates the incoming gradient.


8. Averaging, batches, and the learning rate


The prerequisite map

Vectors and matrices

Dot product

Matrix times vector

Affine map Wx plus b

Transpose

Backward pass

Forward pass

Activation sigma

Derivative and gradient

Chain rule

Gradient descent update

Learning rate alpha

Mini-batch average

Implementing models from scratch


Equipment checklist

Test yourself — reveal only after you have an answer.

What does tell you?
is a stack of 4 real numbers (a vector living in 4-dimensional space).
How do you compute entry of ?
Dot row of with : .
When is even defined?
When the number of columns of equals the number of entries of .
What does the transpose do and why does backprop use it?
Swaps rows and columns; it reverses the forward multiply so gradients flow with matching shapes.
Why must an activation sit between linear layers?
Without a bend, stacked affine maps collapse into one affine map, so the network could only draw straight boundaries.
What single question does a derivative answer?
If I nudge the input slightly, how much and which way does the output change?
What is the gradient in one phrase?
The list of all the loss's slopes, pointing toward steepest increase; we step the opposite way.
What does the chain rule say to do along a chain of dependencies?
Multiply the local slopes together.
What does mean and where does it appear?
Element-wise multiply; it gates the backward gradient by the activation's local slope .
What role does the learning rate play?
It scales the size of each downhill parameter step.
Why average the gradient over a mini-batch instead of the full dataset?
A small random sample gives a cheap, statistically reliable estimate of the true gradient.