Looking inside a network
Read four numbers per layer (spread, dead units, gradient norm, update-to-weight ratio), collect them in NumPy, then with forward and tensor hooks, and diagnose three planted faults.
What it covers6
Why it matters
The loss is one number for the whole network. It can tell you that training is going wrong. It cannot tell you where. Professionals read the layers too: how big each layer's outputs are, how many of its units are dead, how big its gradients are, and how big its steps are against its weights. A table of four numbers per layer finds a bad initialization, a dead layer or a learning rate that is too high in one run, before you spend an hour on a long one.
What you'll be able to do
- Name the four numbers to read for each layer, and say what a healthy and a sick value looks like
- Collect them with a plain loop over a NumPy network, healthy and with a bad initialization
- Capture them in PyTorch with a forward hook and a tensor hook, and remove the hooks afterward
- Read a one-screen report and name the layer and the fault
The idea: four numbers for each layer
This lesson rests on Debugging training, which reads a run from its loss, and on Weight initialization, which says why the size of each layer's output matters. The loss tells you a run is sick. This lesson finds the layer.
Show the answer
About 2.30, which is ln 10
An untrained model spreads its guess about evenly, so each class gets a chance near 1 ÷ 10. The loss for that is −ln(0.1) = ln 10 = 2.30. A first loss far from it points to a setup problem. A first loss at it says nothing about which layer is sick.
A layer's activations are its outputs: one number for each example and each unit. Four numbers describe how a layer is doing.
- Mean and spread of the activations. The spread is the standard deviation. In a healthy network the spread stays about the same from layer to layer. If it shrinks by a factor each layer, the signal is dying. If it grows, it is blowing up.
- Share of dead units. A unit is dead when it outputs 0 for every example in the batch. A ReLU outputs 0 whenever its input is negative, and its slope there is 0 too, so a dead unit passes no gradient and cannot recover.
- Gradient norm. The length of the layer's weight gradient: the square root of the sum of the squares of its entries. Nearly 0 means the layer is not learning.
- Update-to-weight ratio. How big one step is next to the weights it changes: the learning rate times the gradient norm, divided by the weight norm. About 0.001 is a common starting guess, a rule of thumb and not a law. A ratio of 0.1 or more means the steps are very large. A ratio of 0.00001 means the weights barely move.
Take one layer's output for 3 examples (rows) and 4 units (columns).
The lit cells are the dead units' columns. Zero in a row or two is normal. Zero in every row is dead.
Now the picture for a whole network: four layers, the same input, two ways of starting the weights. The bars show the spread of each layer's activations. The worked example in the From scratch part prints every number in them.
Write a sentence or two, then compare it with a good answer in the lesson.
From scratch: collect them in a loop
Each of the four numbers is a line or two of NumPy. The stepper computes the first three on the grid from the picture, then the ratio on a pair of small weight arrays whose norms are easy to read: the gradient's is 0.5 and the weights' is 5.
import numpy as np a = np.array([[0, 2, 0, 1], [0, 3, 0, 0], [0, 1, 0, 2]]) print((a == 0).mean())
Show the answer
0.5833333333333334
Without .all(axis=0), the line averages every entry: 7 of 12 are zero, 7 ÷ 12 = 0.583. That is the share of zero entries, which is not the share of dead units. It is the easy mistake to make.
Now a whole network. The worked example below builds one with four layers (20, 32, 32, 32 and 10 units) in NumPy, runs one forward pass and one backward pass on 256 examples, and fills the table for two starts: He initialization, and weights drawn too small (spread 0.05). A rate of 0.01 is used for the ratio. The first three layers have a ReLU after them, and the fourth gives the logits, so it has no dead share.
Worked example: the table for two starts
Run it, then change 0.05 to 0.5 and run it again: the spread should grow instead of shrink.
Setup (runs before every cell) · 51 lines
The lesson runs this once, before its first cell, and every cell, drill and example starts with what it defines. A cell that changes a setup object changes it for the cells after it, until the page reloads.
import torch import torch.nn as nn torch.manual_seed(0) X = torch.randn(256, 20) y = (X @ torch.randn(20, 10)).argmax(1) # 10 classes from a hidden rule def build_net(fault=None): torch.manual_seed(1) model = nn.Sequential(nn.Linear(20, 32), nn.ReLU(), nn.Linear(32, 32), nn.ReLU(), nn.Linear(32, 32), nn.ReLU(), nn.Linear(32, 10)) for m in model: if isinstance(m, nn.Linear): if fault == "small": nn.init.normal_(m.weight, std=0.05) else: nn.init.kaiming_normal_(m.weight, nonlinearity="relu") nn.init.zeros_(m.bias) if fault == "dead": with torch.no_grad(): model[2].bias.fill_(-4.0) # pushes most units of L2 below 0 return model def layer_report(model, lr): parts = list(model) linears = [m for m in parts if isinstance(m, nn.Linear)] rows, handles = {}, [] def watch(name, module, has_relu): def hook(module, inputs, output): a = output.detach() dead = (a.sum(0) == 0).float().mean().item() if has_relu else None rows[name] = {"mean": a.mean().item(), "std": a.std().item(), "dead": dead} handles.append(module.register_forward_hook(hook)) for k, lin in enumerate(linears): last = k == len(linears) - 1 watch(f"L{k + 1}", lin if last else parts[parts.index(lin) + 1], not last) model.zero_grad() loss = nn.functional.cross_entropy(model(X), y) loss.backward() for h in handles: h.remove() print(f"loss {loss.item():.3f}") print("layer mean std dead grad ratio") for k, lin in enumerate(linears): r, grad = rows[f"L{k + 1}"], lin.weight.grad.norm().item() ratio = lr * grad / lin.weight.norm().item() dead = " -" if r["dead"] is None else f"{r['dead']:5.0%}" print(f" L{k + 1} {r['mean']:6.3f} {r['std']:6.3f} {dead} " f"{grad:8.4f} {ratio:8.5f}")
import numpy as np import matplotlib.pyplot as plt rng = np.random.default_rng(0) Xn = rng.normal(size=(256, 20)) yn = (Xn @ rng.normal(size=(20, 10))).argmax(axis=1) # a hidden rule, 10 classes sizes, lr = [20, 32, 32, 32, 10], 0.01 def inspect(scale): rng = np.random.default_rng(1) Ws = [rng.normal(size=(a, b)) * (scale or np.sqrt(2 / a)) for a, b in zip(sizes, sizes[1:])] acts, gates = [Xn], [] for i, W in enumerate(Ws): # forward z = acts[-1] @ W gates.append(z > 0) acts.append(np.maximum(z, 0) if i < 3 else z) p = np.exp(acts[-1] - acts[-1].max(axis=1, keepdims=True)) p /= p.sum(axis=1, keepdims=True) dz = p.copy() dz[np.arange(256), yn] -= 1 dz /= 256 # slope at the logits rows = [] for i in (3, 2, 1, 0): # backward dW = acts[i].T @ dz a = acts[i + 1] dead = (a == 0).all(axis=0).mean() if i < 3 else None ratio = lr * np.linalg.norm(dW) / np.linalg.norm(Ws[i]) rows.append((i + 1, a.mean(), a.std(), dead, np.linalg.norm(dW), ratio)) if i: dz = (dz @ Ws[i].T) * gates[i - 1] # back through weights and gate return rows[::-1] for name, scale in [("He init", None), ("too small (0.05)", 0.05)]: rows = inspect(scale) print(name) print("layer mean std dead grad ratio") for layer, mean, std, dead, grad, ratio in rows: d = " -" if dead is None else f"{dead:5.0%}" print(f" L{layer} {mean:6.3f} {std:6.3f} {d} {grad:8.4f} {ratio:8.5f}") plt.plot([r[0] for r in rows], [r[2] for r in rows], marker="o", label=name) plt.yscale("log") plt.xlabel("layer") plt.ylabel("spread of the activations") plt.legend()
The He start keeps the spread near 1 at every layer (0.807, 0.903, 1.084, 1.781), no unit is dead, and the ratios sit between 0.0008 and 0.0059, within a few times of the usual 0.001. The small start loses a factor of 4 to 5 at every layer: 0.128, 0.029, 0.007 and 0.002. Its ratios are around 0.00003, so the steps are 30 times smaller than usual. Nothing in the code errors, and the small start has a loss near ln 10 = 2.30, because its logits are almost all 0. Only the per-layer table shows how the signal fades.
The share of dead units
Write dead_share(a): the share of units (columns) of the 2-D array a that are exactly 0 for every example (row).
Uses the setup above, 51 lines.
import numpy as np def dead_share(a): return float((a == 0).mean())
The update-to-weight ratio
Write update_ratio(lr, grad, weight): the learning rate times the norm of grad, divided by the norm of weight. Both are NumPy arrays.
Uses the setup above, 51 lines.
import numpy as np def update_ratio(lr, grad, weight): return lr * grad.sum() / weight.sum()
Write a sentence or two, then compare it with a good answer in the lesson.
In PyTorch: hooks
Purpose. In NumPy you wrote the forward and backward passes, so the numbers were yours to read. In PyTorch they are hidden inside model(x) and loss.backward(). A hook is a function PyTorch calls for you at a chosen moment. You attach it to a layer, run the network as usual, and the hook sees what passed through. This page runs a teaching subset of PyTorch: the same code, CPU only, and seeded numbers that differ from a real install.
A forward hook is called after a layer's forward pass, with the layer, its inputs and its output. register_forward_hook returns a handle, and handle.remove() takes the hook off again.
Uses the setup above, 51 lines.
import torch import torch.nn as nn layer = nn.Linear(4, 3) seen = [] def hook(module, inputs, output): seen.append(tuple(output.shape)) handle = layer.register_forward_hook(hook) layer(torch.randn(8, 4)) layer(torch.randn(8, 4)) print(seen) handle.remove() layer(torch.randn(8, 4)) print(len(seen))
The hook ran twice, once for each call, and not at all after remove(). A hook you forget to remove keeps running and keeps its data alive: remove it when the run you are measuring is over.
import torch import torch.nn as nn layer = nn.Linear(2, 2) calls = [] handle = layer.register_forward_hook(lambda m, i, o: calls.append(1)) layer(torch.zeros(1, 2)) handle.remove() layer(torch.zeros(1, 2)) layer(torch.zeros(1, 2)) print(len(calls))
Show the answer
1
The hook is on for the first call only. After remove(), the next two calls do not run it, so calls holds one entry.
A tensor hook, tensor.register_hook(fn), is called during backward(), with the gradient that reaches that tensor. It is how you read a slope that is not stored anywhere. Here y = 3x, and the loss is the sum of the squares of y.
Uses the setup above, 51 lines.
import torch x = torch.tensor([1.0, 2.0], requires_grad=True) mid = x * 3 mid.register_hook(lambda g: print("gradient arriving at y:", g.tolist())) (mid ** 2).sum().backward() print("gradient of x:", x.grad.tolist())
y is [3, 6], and the slope of the sum of squares with respect to y is 2y = [6, 12]. That is what the hook sees. It then goes back through the × 3 to give x a gradient of [18, 36]. The weights' gradients need no hook, since they are stored in .grad.
Now the report. The setup above this cell holds build_net, a four-layer network like the NumPy one, and layer_report: it puts a forward hook on each layer's output, runs one batch, calls backward(), removes the hooks and prints one row for each layer. The rows are the four numbers from the first part. Run it on the healthy network, with a learning rate of 0.01.
Uses the setup above, 51 lines.
layer_report(build_net(), lr=0.01)
It is the table from the NumPy cell: a spread near 1 in every layer, no dead units, and ratios within a few times of 0.001. Now three runs that each have one fault planted. They are in a mixed-up order, and nothing is labeled. Read the three tables and decide which fault each run has.
Uses the setup above, 51 lines.
for run, (fault, lr) in enumerate([("dead", 0.01), (None, 1.0), ("small", 0.01)], 1): print(f"run {run}") layer_report(build_net(fault), lr)
Show the answer
Run 1, in layer L2
Run 1 shows a dead share of about 0.72 in L2, and every later layer's spread is far below its healthy value. The bias of the second linear layer was set to −4, which pushes most of its outputs below zero, where a ReLU gives 0.
Show the answer
Run 2, in the ratio column: 0.05 to 0.4 against the usual 0.001
Run 2's activations, dead shares and gradients match the healthy table. Only the ratio is off, by a factor of 100, because the learning rate was 1.0 and not 0.01. The rate is not in any of the first three columns. That is why the ratio is the fourth number.
Show the answer
Weights that start too small
A spread that falls by a similar factor at every layer, with no dead units, is the weights starting too small. The loss at ln 10 says the logits are nearly 0, so the network guesses evenly. The fix is a proper initialization, such as He for ReLU layers.
On the job
When a run misbehaves, read in this order. First the loss and the first batch, as the debugging lesson does. Then one batch through a table like this one: it takes seconds and points at a layer. Then the fix is usually one line: a different initialization, a lower rate, a smaller bias, a norm layer. Three habits help. Run the report before a long run and keep its output with the run. Log the four numbers for a few layers next to the loss, so a slow drift is visible. And remove your hooks, or build a fresh model, before a long run: a hook left on keeps a reference to every batch's activations, and the memory use climbs.
Goes deeper in
- Debugging training: reading a run from the loss, and the standard sanity checks.
- Weight initialization: why the spread should stay level, and the He formula.
- Vanishing and exploding gradients: how a gradient fades or grows through layers.
- Optimizers: what the step does with the rate.
Write a sentence or two, then compare it with a good answer in the lesson.
Practice what you just read
- 5 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 9 review cards for spaced repetition
Try a quiz question
When is a ReLU unit called dead?
Before this lesson
Where this lesson leads.
575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.
Phase 4 · about 24 hours of study
Deep learning
Neural networks, backprop and training them well
31 lessons 1 free
- The artificial neuron FreeWeighted sum plus a nonlinearity.35 min
- Activation functionsReLU, sigmoid, GELU, and why nonlinearity matters.35 min
- Multi-layer networksStack layers to learn complex functions.35 min
- The forward passFollow numbers through a network to a prediction.36 min
- Computation graphsEvery calculation as a graph of small operations.40 min
- BackpropagationThe chain rule applied backwards to get every gradient.42 min
- Build an autograd engineWrite backprop yourself in ~100 lines (micrograd).42 min
- PyTorch tensorsNumPy-like arrays that run on GPUs.38 min
- PyTorch autograd and modulesLet PyTorch compute gradients; build models with nn.Module.42 min
- The training loopForward, loss, backward, step, repeat.40 min
- Mini-batches and SGDTrain on small random batches instead of everything at once.40 min
- OptimizersMomentum and Adam.41 min
- Learning-rate schedules in PyTorchWarmup then cosine by hand, as a function of the step, then LambdaLR and where scheduler.step() goes; digits trained with and without warmup.25 min
- Vanishing and exploding gradientsWhy deep networks were hard to train.40 min
- Weight initializationStart weights at the right scale.33 min
- Normalization layersBatch and layer norm keep training stable.40 min
- Residual connectionsSkip connections that let gradients flow.40 min
- DropoutRandomly switch off neurons to prevent overfitting.35 min
- Debugging trainingRead a failing run from its numbers, and fix what's wrong.40 min
- Looking inside a networkRead four numbers per layer (spread, dead units, gradient norm, update-to-weight ratio), collect them in NumPy, then with forward and tensor hooks, and diagnose three planted faults.30 min
- Transfer learningReuse a trained network: freeze the backbone, train a new head, fine-tune with two learning rates.42 min
- Convolutional networksPattern detectors for images.44 min
- Embedding layersLearned lookup tables that turn tokens into vectors.38 min
- Recurrent networksProcess sequences step by step, and their limits.43 min
- LSTMs and GRUs, built and trainedBuild an LSTM and a GRU from a linear layer, train them on the adding problem beside a plain RNN, read what the gates learned, measure the gradient's path, and carry state across a stream.40 min
- Sequence to sequence, and the invention of attentionBuild an encoder and decoder, measure the bottleneck of a fixed-size handoff, add attention in three lines, read an attention map with care, and see how one mistake compounds after teacher forcing.45 min
- AutoencodersEncoder, bottleneck and decoder, reconstruction error for anomalies, denoising, and the latent space image generators use.42 min
- GPUs and computeWhy deep learning runs on graphics cards.40 min
- Training at scale: precision, accumulation, data and many GPUsGradient accumulation and what it really saves, scaling the learning rate with the batch, mixed precision and loss scaling, a data loader that starves the GPU, and why eight GPUs aren't eight times as fast.45 min
- A training script, start to finishConfig, seed, data, model, schedule, loop and checkpoint as one script: what a checkpoint must hold, a resume that matches an unbroken run, and buffers, collate and torch.save in PyTorch.40 min
- Capstone: train a seam-tear spotterTrain and compare three models on fabric images, choose the alarm level from costs, and score once.45 min
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write