Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Unravel the mathematical core of deep learning: computing analytical gradients via the multivariable Chain Rule and stepping down loss landscapes.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Current weights | Where we are on the mountain | |
| Learning rate | Step size | |
| Gradient vector | Slope of steepest ascent |
Current weight w = 5.0, gradient dL/dw = 4.0, learning rate alpha = 0.1.
Result: Weight moves from 5.0 down to 4.6
Compute predictions and evaluate scalar loss L.
Traverse graph in reverse, computing partial derivatives dL/dw for every weight.
Update parameters using gradient descent or Adam step.
Reset accumulated gradient buffers before the next iteration.
Problem: Optimizing 175 billion weights simultaneously across GPU clusters.
Solution: Distributed AdamW optimizer synchronizes gradients across thousands of tensor cores.
Interactive activity component
Witness PyTorch calculate exact analytical derivatives automatically using computational graphs.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install torch1 import torch
import torch → PyTorch autograd engine
1 # Define a learnable weight tensor w with requires_grad=True 2 w = torch.tensor(3.0, requires_grad=True) 3 4 # Define a computational function: Loss = w^3 - 2*w + 5 5 loss = w**3 - 2*w + 5 6 print("Forward Pass Loss Value:", loss.item()) 7 8 # Trigger Backpropagation via the Chain Rule! 9 loss.backward() 10 11 # Calculus check: dLoss/dw = 3*w^2 - 2 = 3*(3^2) - 2 = 27 - 2 = 25 12 print("PyTorch Autograd Computed Gradient dLoss/dw:", w.grad.item()) 13 14 # Take one Gradient Descent step with learning rate 0.01: 15 with torch.no_grad(): 16 w -= 0.01 * w.grad 17 print("Updated Weight w:", round(w.item(), 3))
# Define a learnable weight tensor w with requires_grad=True
Track gradients
w = torch.tensor(3.0, requires_grad=True)
Variable w
# Define a computational function: Loss = w^3 - 2*w + 5
Function
loss = w**3 - 2*w + 5
Compute forward pass
print("Forward Pass Loss Value:", loss.item())
Loss = 3^3 - 6 + 5 = 26
# Trigger Backpropagation via the Chain Rule!
Backward pass
loss.backward()
Calculates dLoss/dw automatically!
# Calculus check: dLoss/dw = 3*w^2 - 2 = 3*(3^2) - 2 = 27 - 2 = 25
Analytical derivative
print("PyTorch Autograd Computed Gradient dLoss/dw:", w.grad.item())
Print gradient
# Take one Gradient Descent step with learning rate 0.01:
Optimizer step
with torch.no_grad():
Disable tracking during update
w -= 0.01 * w.grad
w = 3.0 - (0.01 * 25) = 2.75
print("Updated Weight w:", round(w.item(), 3))
Print updated weight
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # PyTorch Automatic Differentiation (Autograd) 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install torch 8 9 # Step 2: Import libraries 10 import torch 11 12 # Step 3: Run Demonstration 13 # Define a learnable weight tensor w with requires_grad=True 14 w = torch.tensor(3.0, requires_grad=True) 15 16 # Define a computational function: Loss = w^3 - 2*w + 5 17 loss = w**3 - 2*w + 5 18 print("Forward Pass Loss Value:", loss.item()) 19 20 # Trigger Backpropagation via the Chain Rule! 21 loss.backward() 22 23 # Calculus check: dLoss/dw = 3*w^2 - 2 = 3*(3^2) - 2 = 27 - 2 = 25 24 print("PyTorch Autograd Computed Gradient dLoss/dw:", w.grad.item()) 25 26 # Take one Gradient Descent step with learning rate 0.01: 27 with torch.no_grad(): 28 w -= 0.01 * w.grad 29 print("Updated Weight w:", round(w.item(), 3))
PyTorch dynamically constructs computational DAG linking w to loss.
loss.backward() evaluates reverse-mode gradient: 3*(3^2) - 2 = 25.0.
Subtracts alpha * gradient, stepping weight from 3.0 down to 2.75 toward minimum.
Scalar variable w = 3.0
Forward Pass Loss Value: 26.0 PyTorch Autograd Computed Gradient dLoss/dw: 25.0 Updated Weight w: 2.75
PyTorch Autograd computes exact calculus derivatives across arbitrary complex graphs without manual derivation.
CNN, MNIST dataset, backpropagation, loss functions, and optimization.
Test your understanding with these questions. Remember, this is for learning, not examination!