6.5.2 · D3Research Frontiers & Practice

Worked examples — Implementing models from scratch

3,895 words18 min readBack to topic

This page pushes on the machinery from the parent note: the forward pass (), the backward pass (chain rule → gradients), and the update (). Instead of new theory, we drill every case a single dense layer can throw at you: positive and negative activations, dead neurons, zero gradients, a full batch, and the exam-style "predict the shape" twist.

Before the first line, three plain-word reminders so nobody is lost:

One more operation earns its keep on every single forward pass, so we define it now:

Everything below is a single layer computing , sometimes followed by ReLU. We will hand-crank the arithmetic — no PyTorch.


The scenario matrix

Here is every distinct kind of situation this topic can hand you. Each worked example below is tagged with the cell it fills.

# Case class What makes it different Example
A Single sample, all positive baseline forward, no sign trouble Ex 1
B Single sample, some negatives one negative pre-activation feeds ReLU Ex 2
C Zero / degenerate input input is the zero vector Ex 3
D Dead neuron (zero gradient) ReLU gate blocks the gradient Ex 4
E Full mini-batch forward broadcasting bias across columns Ex 5
F Backward: weight gradient , sum over batch Ex 6
G Backward: bias gradient sum across batch dimension Ex 6
H One SGD step (real-world word problem) numbers actually move downhill Ex 7
I Limiting value (learning rate too big) update overshoots, loss rises Ex 8
J Exam twist: shape-only reasoning no numbers, just dimensions Ex 9
K All neurons dead every pre-activation Ex 10
Recall Which cells involve NO calculus at all?

Cases A, B, C, E, J, K ::: they are pure forward-pass or shape bookkeeping — matrix multiply plus a max, or just counting dimensions.


Setup used by all numeric examples

To keep arithmetic checkable by hand we fix a tiny layer: 2 inputs → 2 outputs.

Read row by row: neuron 1 computes , neuron 2 computes . The bias then shifts each: for neuron 1, for neuron 2.

The wiring diagram below makes this concrete: two purple input nodes on the left send weighted wires (orange for neuron 1, teal for neuron 2) into the two output neurons. Notice the dotted grey wire labelled — that is the in the first column of row 2, meaning neuron 2 completely ignores . Trace the orange wires and you can read off neuron 1's formula; trace the teal ones for neuron 2. Keep this picture in mind for every forward pass below.

Figure — Implementing models from scratch
Figure 1 — The 2→2 dense layer. Purple = inputs, orange wires build neuron 1, teal wires build neuron 2, the dotted wire is a weight that carries nothing.


Case A — single sample, all positive


Case B — single sample, some negatives


Case C — zero / degenerate input


Case D — the dead neuron (zero gradient)

To do backprop we need a loss. Use the simplest: suppose the next layer sent back the gradient of the loss with respect to the activations, ReLU then decides which of these gradients survive. Recall (from the parent, and 6.2.03-Backpropagation-Algorithm): where is if true, if false — the ReLU gate.

Before we compute, one piece of notation we will lean on:

The figure below shows why a gate of or is exactly the right thing to with the incoming gradient. The orange curve is ReLU itself; the dashed teal curve is its slope — flat at for negative inputs, flat at for positive inputs, with the corner at the origin marked. The two purple markers sit at our example pre-activations (slope , gradient blocked) and (slope , gradient passes). Read the dashed line under each marker to see whether that neuron's gradient survives.

Figure — Implementing models from scratch
Figure 2 — ReLU (orange) passes positives unchanged; its slope (dashed teal) is the gate you multiply the backward gradient by. At the gate is (gradient dies); at the gate is (gradient flows); at the origin corner the slope is undefined and we choose .


Case E — a full mini-batch forward

Now feed three samples at once, stacked as columns of :


Cases F & G — weight and bias gradients over the batch

Suppose the upstream gradient at this layer's pre-activations is The parent gives and .


Case H — one real gradient-descent step (word problem)


Case I — limiting value: learning rate too big


Case J — exam twist: reason with shapes only


Case K — every neuron dead


Recall checkpoint

Recall Why does the zero input NOT give zero output?

Because the layer is affine, not linear — the bias is added after , so zero input yields . ::: (Ex 3)

Recall In backprop, why is the ReLU gate applied with

rather than matrix multiply? Each neuron's gate affects only its own gradient — there is no mixing across neurons — so it is a pointwise (element-wise) operation. ::: (Ex 4)

Recall What does ReLU's gradient do exactly at z = 0, and why?

The derivative does not truly exist at the corner; by convention we send the gate to 0 (the strict-inequality branch) because a neuron sitting at 0 output contributed nothing forward, so it fairly contributes nothing backward. ::: (Ex 4, Ex 10)

Recall What single check tells you your transposes in

and are right? The gradient must have the same shape as the quantity it differentiates ( like , like ). ::: (Ex 6, Ex 9)

See also 6.4.01-PyTorch-Basics for how these hand-cranked steps become one loss.backward() call, and 6.5.01-Reading-Research-Papers for turning a paper's equations into exactly this kind of worked layer.