PyTorch Continued &�Backpropagation
Lecture 15
Backpropagation – computing gradients easily and efficiently!
EECS 189/289, Fall 2025 @ UC Berkeley
Joseph E. Gonzalez and Narges Norouzi
EECS 189/289, Fall 2025 @ UC Berkeley
Joseph E. Gonzalez and Narges Norouzi
Join at slido.com�#3940975
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
3940975
Implement Gradient Descent with Optimizer
In PyTorch you implement your own gradient descent loop but you can use built in update rules (e.g., Adam) called optimizers.
loader = DataLoader(tr_data, batch_size=32, shuffle=True)
optimizer = torch.optim.Adam(model.parameters(),eta)
for epoch in range(nepochs):
for (x, t) in loader:
optimizer.zero_grad()
loss = loss_fn(model(x), t)
loss.backward()
optimizer.step()
Minibatch SGD DataLoader
Adam optimizer
Epoch is one pass over data
Load one minibatch at a time
Compute the gradient of the loss
Use optimizer to update all the parameters (1-step)
3940975
Demo
3940975
Recap: Stochastic Gradient Descent
3940975
Simple Running Example
Consider the following single hidden layer classification network
Inputs
Hidden�Layer
Output
Matrix Notation
2
3
3
1
1
No bias at each layer in this example.
3940975
Defining the Error Function
Inputs
Hidden�Layer
Output
Likelihood of the data.
Algebra!
3940975
Computing the Gradient
Inputs
Hidden�Layer
Output
2
3
3
1
3940975
What is the shape of the gradient?
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
3940975
Computing the Gradient
Inputs
Hidden�Layer
Output
2
3
3
1
3940975
Computing the Gradient
Inputs
Hidden�Layer
Output
3940975
Numerical Differentiation
Proposal: Numerical Differentiation
3940975
Proposal: Numerical Differentiation
3940975
Cost of Numerical Differentiation
3940975
Symbolic Differentiation
Proposal: Symbolic Differentiation
3940975
Symbolic Computer Algebra Systems
Computer algebra systems like SymPy can be used to automate the differentiation process.
3940975
Demo
SymPy
3940975
Symbolic Computer Algebra Systems
Computer algebra systems like SymPy can be used to automate the differentiation process.
Advantages:
Disadvantages:
3940975
Expression Swell
3940975
Automatic Differentiation
Automatic Differentiation (Autodiff)
3940975
Forward Tracing the Computation Graph
We construct the graph by evaluating the forward computation:
Program:
0: v1 = x1
1: v2 = x2
2: v3 = v1 * v2
3: v4 = exp(v3)
4: v5 = sin(v2)
5: v6 = v4 + v3
6: v7 = v6 – v5
In this program we have broken each expression into a separate line.
3940975
Compute this using the �same process
3940975
Compute these using the �same process
3940975
Base Case!
3940975
Backward Propagation (Recursion)
Recursion:
Parents
Children
3940975
Backward Autodiff Example
3940975
3940975
3940975
3940975
3940975
3940975
3940975
3940975
3940975
3940975
What is the value of the adjoint variable a_3.
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
3940975
3940975
3940975
3940975
3940975
3940975
Obtaining The Gradient!
3940975
Cost Analysis
Backpropagation is efficient when there is a single output and many inputs.
Need to run backpropagation repeatedly for multiple outputs.
Often reuse computation from the forward step.
Only need to maintain single adjoint variable at each step.
3940975
Demo
You will do this in your homework.
3940975
A
B
D
E
F
G
C
Hint:
The adjoint at B can’t be computed until the adjoint at D is computed (topological ordering) but does not depend on the adjoint at C (an inactive path).
3940975
Additional Reading
3940975
PyTorch Continued &�Backpropagation
Lecture 15
Reading: Chapter 8 in Bishop Deep Learning Textbook
Forward Differentiation
Bonus Material
Chain Rule on The Computation Graph
3940975
Chain Rule on The Computation Graph
3940975
Forward Autodiff Example
Derivation
3940975
Forward Autodiff Example
Derivation
3940975
Forward Autodiff Example
Derivation
3940975
Forward Autodiff Example
3940975
Support multiple outputs
Additional function output can reuse computation.
Essentially, we computed the gradient at every step!
?
3940975
Expensive for Lots of Variables
Need to maintain full gradient at every intermediate step.
For models with billions of parameters, need to store and update billion-dimension vector for every arithmetic operation.
3940975
Adjoint vs Tangent Variables
We have now defined tangent and adjoint variables for forward and backward autodiff respectively:
Notice how they build from one side of the graph to the other:
Tangent Variables
Adjoint Variables
Parents
Children
3940975