001 Notes
Neural Network Tutor: Replay, Gradients, And Ablation
one example
-> forward pass
-> prediction
-> loss
-> backward pass
-> gradients
-> update preview
-> weight change
-> new prediction
one neuron
-> many neurons in one layer
-> wider layer
-> deeper stack
-> dense connectivity
-> bottleneck
-> activation removed
-> neuron ablated
-> layer frozen
1. One Neuron Is A Learned Detector
z = w1*x1 + w2*x2 + ... + wn*xn + b
a = activation(z)
z = w dot x + b
a = f(z)
| Symbol | Intuition |
|---|---|
x | input features |
w | what the neuron pays attention to |
b | threshold shift |
z | raw match score |
f | activation/gate |
a | signal passed to next layer |
z = w1*x1 + w2*x2 + b
w1*x1 + w2*x2 + b = 0
one neuron
-> one learned boundary
-> one detector
b larger -> boundary shifts one way
b smaller -> boundary shifts the other way
2. A Single Neuron Worked Example
x1 = 2.0
x2 = -1.0
w1 = 0.30
w2 = -0.20
b = 0.10
z = x1*w1 + x2*w2 + b
z = 2.0*0.30 + (-1.0)*(-0.20) + 0.10
z = 0.60 + 0.20 + 0.10
z = 0.90
a = z = 0.90
prediction = 0.90
target = 1.50
error = prediction - target
error = 0.90 - 1.50
error = -0.60
loss = 0.5 * (prediction - target)^2
loss = 0.5 * (-0.60)^2
loss = 0.18
d/dp [0.5 * (p - y)^2] = p - y
3. Backprop For One Weight
loss = 0.5 * (prediction - target)^2
prediction = z
z = x1*w1 + x2*w2 + b
dLoss/dw1
= dLoss/dprediction * dprediction/dz * dz/dw1
dLoss/dprediction = prediction - target = -0.60
dprediction/dz = 1.0
dz/dw1 = x1 = 2.0
dLoss/dw1 = -0.60 * 1.0 * 2.0
dLoss/dw1 = -1.20
dLoss/dw2 = -0.60 * 1.0 * (-1.0)
dLoss/dw2 = 0.60
dLoss/db = -0.60 * 1.0 * 1.0
dLoss/db = -0.60
new_weight = old_weight - learning_rate * gradient
| Parameter | Before | Gradient | Delta | After |
|---|---|---|---|---|
w1 | 0.30 | -1.20 | +0.12 | 0.42 |
w2 | -0.20 | 0.60 | -0.06 | -0.26 |
b | 0.10 | -0.60 | +0.06 | 0.16 |
negative gradient:
gradient descent subtracts a negative
parameter increases
positive gradient:
gradient descent subtracts a positive
parameter decreases
the output was too low
x1 was positive, so increasing w1 increases output
x2 was negative, so decreasing w2 increases output
4. Activation Functions Change Neuron Behavior
| Activation | Formula | Behavior |
|---|---|---|
| identity | a = z | passes raw value |
| sigmoid | a = 1 / (1 + exp(-z)) | squashes to 0..1 |
| tanh | a = tanh(z) | squashes to -1..1 |
| ReLU | a = max(0, z) | zeroes negative values |
| softmax | exp(z_i) / sum(exp(z_j)) | turns logits into class probabilities |
z <= 0 -> neuron silent
z > 0 -> neuron active
z = -0.7
relu(z) = 0
d relu(z) / dz = 0 when z <= 0
d relu(z) / dz = 1 when z > 0
neuron z: -0.7
activation: 0
activation derivative: 0
gradient through this neuron: blocked for this example
5. Why Nonlinearity Makes Depth Useful
y = W3(W2(W1x))
y = (W3 * W2 * W1)x
input -> dense -> dense -> dense -> output
input -> one dense linear transform -> output
y = W3 * relu(W2 * relu(W1x + b1) + b2) + b3
layer 1 gates the input space
layer 2 combines those gated regions
layer 3 combines combinations
Model A: 3 dense layers with ReLU
Model B: same 3 dense layers, activation removed
| Observation | Meaning |
|---|---|
| Model B cannot solve nonlinear data | depth without activation collapsed |
| Model B trains but plateaus early | linear capacity is insufficient |
| Model A improves | nonlinear gates are doing useful work |
6. Dense Layer Math
a = activation(Wx + b)
W shape = H x D
b shape = H
parameter_count = H*D + H
input_dim = 20
hidden_width = 64
parameters = 20*64 + 64
parameters = 1,344
W =
[
neuron_1 weights
neuron_2 weights
neuron_3 weights
...
]
z1 = w1 dot x + b1
z2 = w2 dot x + b2
...
zH = wH dot x + bH
a1 = f(z1)
a2 = f(z2)
...
aH = f(zH)
7. Width: More Detectors In Parallel
narrow layer:
8 neurons
medium layer:
64 neurons
wide layer:
512 neurons
one neuron:
one boundary
many neurons:
many boundaries
many gated features
many ways to separate the data
layer output lives in H-dimensional feature space
| Width | Effect |
|---|---|
| too narrow | underfitting, lost information |
| moderate | enough detectors for useful patterns |
| very wide | high capacity, more compute, overfit risk |
D -> H
parameters = D*H + H
H1 -> H2
parameters = H1*H2 + H2
512 -> 512 dense layer
= 512*512 + 512
= 262,656 parameters
4096 -> 4096 dense layer
= 4096*4096 + 4096
= 16,781,312 parameters
8. Depth: Composition Over Time
input
-> layer 1
-> layer 2
-> layer 3
-> output
pixels
-> edges
-> corners/textures
-> object parts
-> full objects
characters/tokens
-> local patterns
-> phrases
-> sentence meaning
-> task answer
raw features
-> pairwise interactions
-> higher-order interactions
-> decision
dLoss/dh0 =
dLoss/dhL
* dhL/dhL-1
* dhL-1/dhL-2
* ...
* dh1/dh0
0.5 * 0.5 * 0.5 * 0.5 * 0.5 = 0.03125
2 * 2 * 2 * 2 * 2 = 32
layer 1 gradient norm: 0.00003
layer 2 gradient norm: 0.00210
layer 3 gradient norm: 0.09100
output gradient norm: 0.84000
9. Shallow Narrow Networks
input -> small hidden layer -> output
20 -> 8 -> 1
simple tabular problems
small datasets
linear-ish relationships
fast educational demos
complex nonlinear boundaries
high-dimensional input
many independent patterns
image or language tasks
training loss high
validation loss high
model underfits
increase width from 8 to 64
keep depth fixed
compare training loss
10. Shallow Wide Networks
input -> very wide hidden layer -> output
20 -> 512 -> 1
many parallel nonlinear detectors
problems where one hidden layer is enough
fast experiments with simple optimization
small datasets with noise
raw high-dimensional input
tasks that need hierarchical composition
shallow wide:
memorize many local patches of the input space
deep:
build reusable intermediate features
training accuracy: 99%
validation accuracy: 65%
train widths: 32, 128, 512, 2048
plot train loss and validation loss
training loss keeps improving
validation loss improves then gets worse
11. Deep Narrow Networks
input -> narrow layer -> narrow layer -> narrow layer -> output
20 -> 16 -> 16 -> 16 -> 1
compositional structure
parameter-efficient experiments
forcing intermediate abstractions
tasks needing many independent features at once
optimization without normalization/residual help
datasets where early compression destroys information
input has 100 independent useful features
first hidden layer has 4 neurons
20 -> 4 -> 4 -> 4 -> output
20 -> 16 -> 16 -> 16 -> output
20 -> 64 -> 64 -> 64 -> output
training loss
validation loss
activation saturation
gradient norms
examples whose predictions change
12. Deep Wide Networks
input -> wide -> wide -> wide -> output
512 -> 1024 -> 1024 -> 1024 -> 10
large datasets
complex functions
many feature interactions
high-capacity research experiments
small data
weak regularization
limited memory
slow local training
educational clarity if everything is hidden
512 -> 1024: 512*1024 + 1024 = 525,312
1024 -> 1024: 1,049,600
1024 -> 1024: 1,049,600
1024 -> 10: 10,250
total: about 2.63M parameters
halve width
remove one layer
freeze first layer
compare validation loss and gradient flow
high capacity is useful only when data, optimization, and regularization support it
13. Bottlenecks
128 -> 16 -> 128
D -> K -> D
rank <= K
input has 100 features
only 10 are useful
many features are noise
100 -> 16 -> 64 -> output
input contains many independent useful facts
small differences matter
512 -> 8 -> 512
hidden width 128
hidden width 32
hidden width 8
which classes collapse together
which examples become confused
whether training loss itself gets stuck
14. Dense Connectivity
output_j = activation(sum_i input_i * weight_j_i + bias_j)
params = input_dim * output_dim + output_dim
20 tabular features -> 64 hidden units
params = 20*64 + 64 = 1,344
224 * 224 * 3 image pixels -> 4096 hidden units
input_dim = 150,528
params = 150,528 * 4096 + 4096
params = 616,566,784
dense layers are flexible for compact features
dense layers are expensive for raw high-dimensional inputs
15. Sparse, Local, And Residual Connections
each neuron sees only part of previous layer
small filter reused across image positions
output = layer(input) + input
gradient has a shortcut path backward
signal has a shortcut path forward
deep network can learn modifications instead of everything from scratch
16. Case Study: Linear Boundary
classify points separated by one straight line
approve if income - debt > threshold
z = w1*income + w2*debt + b
output = sigmoid(z)
| Model | Behavior |
|---|---|
| one neuron | learns well |
| one hidden layer | also learns, extra capacity unused |
| deep network | may learn, but unnecessary |
| deep network without enough data | may overfit or train slower |
train one neuron
train 3-layer network
compare validation loss and parameter count
If the true boundary is simple, depth is extra machinery.
17. Case Study: XOR
x1 XOR x2
x1 | x2 | output |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
no single straight line separates the 1s from the 0s
2 inputs -> 2 hidden nonlinear neurons -> output
hidden neuron A detects one region
hidden neuron B detects another region
output combines the regions
| Model | Expected result |
|---|---|
| no hidden layer | cannot solve XOR |
| hidden width 1 | usually insufficient |
| hidden width 2 | can solve |
| remove activation | fails again |
width plus nonlinearity creates multiple regions.
18. Case Study: Circle Inside Square
classify points inside a circle as 1
outside as 0
many ReLU/sigmoid neurons
-> many half-spaces
-> polygon-like approximation of circle
4 neurons -> rough diamond/square boundary
8 neurons -> rough octagon
32 neurons -> smoother circle-like boundary
hidden width: 2, 4, 8, 32
same depth: 1 hidden layer
compare decision boundary
wider hidden layer creates more boundary pieces
19. Case Study: Image Recognition
recognize cats in images
pixel_1, pixel_2, pixel_3, ..., pixel_150528
all pixels -> cat/not cat
early layers:
edges, colors, simple textures
middle layers:
corners, fur textures, eye-like patterns
later layers:
ears, face shapes, object parts
output:
class decision
| Model | Expected weakness |
|---|---|
| raw pixels -> dense output | underfits or overfits badly |
| shallow huge dense model | enormous parameter count |
| deep model with local filters | better hierarchy |
| deep model without activation | loses nonlinear composition |
Depth helps when useful features are built from smaller reusable features.
20. Case Study: Small Tabular Dataset
predict house price from 20 features and 2,000 examples
20 -> 64 -> 1
20 -> 512 -> 512 -> 512 -> 1
training loss falls
validation loss rises
predictions become brittle
small input changes create unstable outputs
reduce width
remove one layer
add weight decay/dropout
compare validation loss
data size limits useful model capacity.
21. Case Study: Noisy Labels
classify examples where 15% of labels are wrong
early training:
model learns broad real pattern
late training:
model starts fitting noisy exceptions
training accuracy keeps rising
validation accuracy peaks then falls
high-confidence wrong predictions increase
train narrow model
train wide model
train wide model with early stopping
train wide model with weight decay
more capacity can fit truth first and noise later.
22. Case Study: Too Deep Without Support
simple classification, plain 30-layer MLP
gradients must pass through many layers
loss plateaus
early-layer gradients near zero
some layers saturate
training unstable
3 layers
10 layers
30 layers
30 layers with residual connections
30 layers with normalization
depth adds representational power
but optimization needs architecture support
layer 1: 0.000001
layer 2: 0.000004
layer 3: 0.000020
...
output: 0.810000
23. Case Study: Bad Bottleneck
classify objects where small details matter
512 -> 8 -> 512 -> output
training loss remains high
classes with subtle differences collapse
hidden activations look similar across examples
bottleneck width 8
bottleneck width 32
bottleneck width 128
24. Case Study: Good Bottleneck
input has many noisy features but few real factors
100 -> 16 -> 64 -> output
training loss slightly worse than huge model
validation loss better
predictions less sensitive to noisy features
no bottleneck
wide bottleneck
narrow bottleneck
compression can regularize when signal is lower-dimensional than input.
25. Mini-Batches And Architecture
batch_gradient =
average(example_1_gradient,
example_2_gradient,
...
example_n_gradient)
example A gradient for w1: -1.20
example B gradient for w1: 0.40
example C gradient for w1: -0.10
batch average: -0.30
selected weight: w1
example contributions:
A: increase strongly
B: decrease mildly
C: increase mildly
batch update:
increase slightly
26. Optimizers Change Motion
w = w - lr * gradient
velocity = momentum * velocity + gradient
w = w - lr * velocity
first moment: average gradient direction
second moment: average squared gradient scale
same network
same data
same initialization
train with SGD, momentum, Adam
compare update paths
architecture defines capacity
optimizer defines how parameters move through that capacity
27. Architecture Ablation Lab
| Ablation | Question |
|---|---|
| remove one neuron | did this detector matter? |
| zero one weight | was this connection important? |
| freeze one layer | does this layer need learning? |
| widen a layer | does capacity help? |
| narrow a layer | does compression hurt? |
| add depth | does composition help? |
| remove activation | was nonlinearity essential? |
| remove bias | did thresholds matter? |
| add bottleneck | does compression regularize? |
| change optimizer | was motion, not capacity, the issue? |
| shuffle labels | can the model memorize noise? |
parameter count
training loss
validation loss
gradient norm per layer
activation sparsity
examples changed most
confidence distribution
runtime cost
changed depth, width, optimizer, learning rate, and dataset split
same data
same seed
same optimizer
same training steps
only width changed
28. Replay UI For Architecture
incoming weights
bias
z value
activation value
activation derivative
examples where it activates
examples where it stays silent
gradient received
update magnitude
width
parameter count
activation distribution
dead/inactive neurons
gradient norm
input/output shape
role in architecture
before weight
input activation
upstream error
gradient
learning rate
delta
after weight
shallow narrow
shallow wide
deep narrow
deep wide
bottlenecked
activation removed
layer frozen
did it learn faster?
did it generalize better?
which examples changed?
which neurons activated?
which gradients disappeared?
which parameters mattered?
29. Implementation Blueprint
architecture id
dataset split id
training step
batch ids
layer shapes
parameter counts
forward activations
pre-activation z values
loss
gradients
optimizer state where relevant
before parameters
after parameters
validation metrics
ablation id if active
always store:
loss
accuracy
gradient norms
activation sparsity
parameter counts
store on demand:
full activation vectors
per-weight gradients
per-example contribution
training and replay generation run off the UI thread
large diagrams render lazily
only selected detail panes compute heavy traces
generated replay artifacts are visible and deletable
30. What Was Done And Why
31. What Was Used, Removed, Or Deferred
step-by-step replay
single-neuron inspection
forward-pass arithmetic
chain-rule traces
weight update tables
activation behavior views
width/depth architecture comparisons
parameter-count visibility
gradient norm inspection
activation sparsity inspection
architecture ablation experiments
train-vs-validation comparisons
example-level prediction deltas
teaching neural networks only as equations
showing only final accuracy
hiding gradients behind charts
generic network diagrams with no numbers
claiming depth is always better
claiming width is always better
ignoring parameter count
changing multiple experiment variables at once
large-model training
GPU-heavy training runtime
automatic architecture search
full tracing for every weight in large networks
over-general visual framework for every model family
Comments