
Lecture 6: Neural Networks II
Intro to Machine Learning

layer
linear combo
activations
layer
input
neuron
learnable weights
Engineers choose:
- activation \(f\) in each layer
- # of layers
- # of neurons in each layer
hidden
output
Recall:
(aka, multi-layer perceptrons)
a (fully-connected, feed-forward) neural network
\(\dots\)
Forward pass: evaluate, given the current parameters
- the model outputs
- the loss incurred on the current data \(\mathcal{L}(g^{(i)}, y^{(i)})\)
- the training error \(J = \frac{1}{n} \sum_{i=1}^{n}\mathcal{L}(g^{(i)}, y^{(i)})\)
linear combination
loss function
(nonlinear) activation
Recall:
\(f =\sigma(\cdot)\)
\(f(\cdot) \) identity function
Recall:



\(-3(\sigma_1 +\sigma_2)\)
(The demo won't embed in PDF. The direct link below gets to the demo.)
Outline
-
Backward pass (to learn parameters/weights)
- Backpropagation (gradient descent & the chain rule)
- Recursive reuse of computation
- Practical gradient issues and remedies
stochastic gradient descent to learn a linear regressor
- Randomly pick a data point \((x^{(i)}, y^{(i)})\)
- Evaluate the gradient \(\nabla_{w} \mathcal{L}(g^{(i)},y^{(i)})\)
- Update the weights \(w \leftarrow w - \eta \nabla_w \mathcal{L}(g^{(i)},y^{(i)})\)
for simplicity, say training data set is just\((x,y)\) and squared loss
walk-through on board
Slightly more interesting:
walk-through on board
Slightly more interesting:
walk-through on board
The middlemen
one partial derivative per hop,
traced backwards along the path, from the loss to the variable of interest
“\(z\) tells \(w\) how to change”
\(\dfrac{\partial z}{\partial w} = x = 3\)
“the guess tells \(z\) how to change”
\(\dfrac{\partial g}{\partial z} = \text{ReLU}'(z) = 1\)
“the loss tells the guess how to change”
\(\dfrac{\partial \mathcal{L}}{\partial g} = 2(g - y) = 12\)
Outline
-
Backward pass (to learn parameters/weights)
- Backpropagation (gradient descent & the chain rule)
- Recursive reuse of computation
- Practical gradient issues and remedies
- Randomly pick a data point \((x^{(i)}, y^{(i)})\)
- Evaluate the gradient \(\nabla_{W^2} \mathcal{L}(g^{(i)},y^{(i)})\)
- Update the weights \(W^2 \leftarrow W^2 - \eta \nabla_{W^2} \mathcal{L}(g^{(i)},y^{(i)})\)
\(\dots\)
\(\nabla_{W^2} \mathcal{L}(g^{(i)},y^{(i)})\)
Backward pass: run SGD to update all parameters
e.g. to update \(W^2\)
\(\dots\)
how to find
?
\(\dots\)
how to find
?
Previously, we found
\(\dots\)
how to find
Now
backpropagation: reuse of computation
\(\dots\)
\(\dots\)
backpropagation: reuse of computation
Training a neural network: the full loop
Initialize all weights \(W^1, W^2, \dots, W^L\) randomly
Repeat until stopping criterion:
- Forward pass: pick a data point \((x^{(i)}, y^{(i)})\), compute \(Z^1, A^1, Z^2, A^2, \dots, g^{(i)}\)
- Evaluate loss: compute \(\mathcal{L}(g^{(i)}, y^{(i)})\)
- One full backward pass: compute \(\nabla_{W^\ell} \mathcal{L}(g^{(i)}, y^{(i)})\) for every \(\ell = L, \dots, 1\), reusing cached results
In practice, mini-batches rather than pure SGD; in PyTorch, loss.backward() then optimizer.step().
e.g., three layers:
- Update all weights at once: \(W^\ell \leftarrow W^\ell - \eta\, \nabla_{W^\ell} \mathcal{L}\) for all \(\ell\)
Outline
-
Backward pass (to learn parameters/weights)
- Backpropagation (gradient descent & the chain rule)
- Recursive reuse of computation
- Practical gradient issues and remedies
The flat branch
on the flat branch the ReLU's slope is 0, and one zero factor zeroes the whole product
at \(w = 4\) (the middlemen slide): \(3 \cdot 1 \cdot 12 = 36\)
“\(z\) tells \(w\) how to change”
\(\dfrac{\partial z}{\partial w} = x = 3\)
“the guess tells \(z\) how to change”
\(\dfrac{\partial g}{\partial z} = \text{ReLU}'(-3) = 0\)
“the loss tells the guess how to change”
\(\dfrac{\partial \mathcal{L}}{\partial g} = 2(g - y) = -12\)
\(x = 3\)
\(y = 6\)
\(W^1_1 = 4\)
\(W^1_2 = 2\)
\(W^2_1 = 1\)
\(W^2_2 = 1\)
\(\mathcal{L}(g, y) = (g - y)^2\)
More neurons, more layers
forward pass: evaluation
\(x = 3\)
\(y = 6\)
\(W^1_1 = 4\)
\(W^1_2 = 2\)
\(W^2_1 = 1\)
\(W^2_2 = 1\)
\(\mathcal{L}(g, y) = (g - y)^2\)
\(12\)
\(12\)
\(6\)
\(6\)
\(18\)
\(18\)
\(144\)
More neurons, more layers
forward pass: evaluation
\(x = 3\)
\(y = 6\)
\(W^1_1 = 4\)
\(W^1_2 = 2\)
\(W^2_1 = 1\)
\(W^2_2 = 1\)
\(12\)
\(12\)
\(6\)
\(6\)
\(18\)
\(18\)
The backward pass
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial g}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial g}{\partial z^2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 1 \cdot 24\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#6aa84f}{24}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial W^2_1}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial W^2_1}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 12 \cdot \textcolor{#6aa84f}{24}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 288\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial W^2_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial W^2_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 6 \cdot \textcolor{#6aa84f}{24}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 144\)
\(\begin{aligned} \dfrac{\partial \mathcal{L}}{\partial g} &= 2(g - y) \\[2pt] &= 2(18 - 6) \\[2pt] &= 24 \end{aligned}\)
gradient w.r.t. 2nd layer weights:
\(x = 3\)
\(y = 6\)
\(W^1_1 = 4\)
\(W^1_2 = 2\)
\(W^2_1 = 1\)
\(W^2_2 = 1\)
\(12\)
\(12\)
\(6\)
\(6\)
\(18\)
\(18\)
The backward pass
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{24}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial a^1_1}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial a^1_1}{\partial z^1_1}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^1_1}{\partial W^1_1}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial W^1_1}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 3 \cdot 1 \cdot 1 \cdot \textcolor{#6aa84f}{24}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 72\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial a^1_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial a^1_2}{\partial z^1_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^1_2}{\partial W^1_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial W^1_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 3 \cdot 1 \cdot 1 \cdot \textcolor{#6aa84f}{24}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 72\)
gradient w.r.t. 1st layer weights:
\(x = 3\)
\(y = 6\)
\(W^1_1 = 4\)
\(W^1_2 = -1\)
\(W^2_1 = 1\)
\(W^2_2 = 1\)
\(12\)
\(12\)
\(-3\)
\(0\)
\(12\)
\(12\)
if ReLU input is negative, some weights won't update
\(\begin{aligned} \dfrac{\partial \mathcal{L}}{\partial g} &= 2(g - y) \\[2pt] &= 2(12 - 6) \\[2pt] &= 12 \end{aligned}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial g}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial g}{\partial z^2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 1 \cdot 12\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#6aa84f}{12}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial a^1_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial a^1_2}{\partial z^1_2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
👻 dead ReLU
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^1_2}{\partial W^1_2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial \mathcal{L}}{\partial W^1_2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 3 \cdot \textcolor{#cc0000}{0} \cdot 1 \cdot \textcolor{#6aa84f}{12}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#cc0000}{0}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial \mathcal{L}}{\partial W^2_2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial z^2}{\partial W^2_2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#cc0000}{0} \cdot \textcolor{#6aa84f}{12}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#cc0000}{0}\)
\(W^2_2 = 1\)
\(W^1_2 = -1\)
gradient w.r.t. red weights:
\(x = 3\)
\(y = 6\)
\(W^1_1 = 4\)
\(W^1_2 = 2\)
\(W^2_1 = -1\)
\(W^2_2 = -1\)
\(12\)
\(12\)
\(6\)
\(6\)
\(-18\)
\(0\)
if \(z^2 < 0\), no weights get updated
\(\begin{aligned} \dfrac{\partial \mathcal{L}}{\partial g} &= 2(g - y) \\[2pt] &= 2(0 - 6) \\[2pt] &= -12 \end{aligned}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial g}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial g}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#cc0000}{0} \cdot (-12)\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#cc0000}{0}\)
👻 dead ReLU
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial \mathcal{L}}{\partial W^2_k}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial W^2_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= a^1_k \cdot \textcolor{#cc0000}{0}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#cc0000}{0}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial \mathcal{L}}{\partial W^1_k}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^1_k}{\partial W^1_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial a^1_k}{\partial z^1_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial a^1_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#cc0000}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 3 \cdot 1 \cdot (-1) \cdot \textcolor{#cc0000}{0}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#cc0000}{0}\)
\(W^2_2 = -1\)
\(W^2_1 = -1\)
\(W^1_2 = 2\)
\(W^1_1 = 4\)
gradient w.r.t. all weights:
\(k = 1\) or \(2\), the hidden neuron the weight connects to, so each row covers both weights of its layer
\(x = 3\)
\(y = 6\)
\(W^1_1 = 4\)
\(W^1_2 = 2\)
\(W^2_1 = -1\)
\(W^2_2 = -1\)
\(12\)
\(12\)
\(6\)
\(6\)
\(-18\)
\(-18\)
Residual (skip) connection
\(W^2_2 = -1\)
\(W^2_1 = -1\)
\(W^1_2 = 2\)
\(W^1_1 = 4\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial g}{\partial z^2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 1 + \text{ReLU}'(-18)\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 1\)
\(\begin{aligned} \dfrac{\partial \mathcal{L}}{\partial g} &= 2(g - y) \\[2pt] &= 2(-18 - 6) \\[2pt] &= -48 \end{aligned}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial g}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial g}{\partial z^2}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 1 \cdot (-48)\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= \textcolor{#6aa84f}{-48}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial W^2_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial W^2_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= a^1_k \cdot (\textcolor{#6aa84f}{-48})\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial \mathcal{L}}{\partial W^1_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}=\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^1_k}{\partial W^1_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial a^1_k}{\partial z^1_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\dfrac{\partial z^2}{\partial a^1_k}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\cdot\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}\textcolor{#6aa84f}{\dfrac{\partial \mathcal{L}}{\partial z^2}}\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 3 \cdot 1 \cdot (-1) \cdot (\textcolor{#6aa84f}{-48})\)
\(\vphantom{\dfrac{\partial \mathcal{L}}{\partial W^1_1}}= 144\)
even if \(z^2 < 0\), with skip connection, weights in earlier layers can still get updated
gradient w.r.t. all weights:
Going deep: vanishing and exploding gradients
backpropagation computes gradients via the chain rule — a product of many factors:
e.g. \(\displaystyle \quad \frac{\partial \mathcal{L}}{\partial W^1} \;=\; \frac{\partial Z^1}{\partial W^1} \cdot \underbrace{\frac{\partial A^1}{\partial Z^1} \cdot \frac{\partial Z^2}{\partial A^1} \cdots \frac{\partial A^{L-1}}{\partial Z^{L-1}} \cdot \frac{\partial Z^L}{\partial A^{L-1}}}_{{L-1}\text{ layers of multiplicative factors}} \cdot \frac{\partial g}{\partial Z^L} \cdot \frac{\partial \mathcal{L}}{\partial g}\)
Factors below 1: the product shrinks fast with depth, so gradients vanish
Factors above 1: the product grows fast with depth, so gradients explode
Common remedies:
- residual connections
- gradient clipping (if \(\|\nabla\| > \tau\), rescale \(\nabla \leftarrow \tau \frac{\nabla}{\|\nabla\|}\), keeping the direction)
- careful initialization, normalization: both keep factors near 1
Summary
backpropagation backward passbackpropagationchain rule
practical issues dead ReLUskip connectionvanishing / exploding gradient
Backpropagation is gradient descent plus the chain rule: each “tells” is one partial derivative, and a weight’s gradient multiplies the factors between it and the loss.
One backward sweep caches \(\partial \mathcal{L}/\partial Z\) at each layer, and every earlier weight reuses it.
One training step: forward pass, loss, backward pass, then all weights update at once (as in PyTorch).
On its flat branch a ReLU passes zero gradient: a dead neuron freezes its edges, and a dead output freezes the whole network.
Skip connections keep a slope-1 path for the gradient; long products of factors can still vanish or explode (clipping and careful initialization help).
6.390 IntroML (Fall26) - Lecture 6 Neural Networks II
By Shen Shen
6.390 IntroML (Fall26) - Lecture 6 Neural Networks II
- 84