“In 1‑D the derivative is a number. In many‑D it becomes a vector pointing where the function rises fastest.”
For the gradient is
Concept | Practical impact |
---|---|
Back‑propagation | Each weight update uses the gradient of the loss. |
Gradient norm | Signals vanishing/exploding gradients. |
Visual debugging | Plotting ∇ on a surface reveals saddle points, plateaus, etc. |
A perfect “bowl”: every gradient arrow points away from the origin.
# calc-06-gradients/quiver_xy2.py
import numpy as np
import matplotlib.pyplot as plt
import torch
# ---- build grid -------------------------------------------------------
xv = np.linspace(-2, 2, 21)
yv = np.linspace(-2, 2, 21)
X, Y = np.meshgrid(xv, yv)
# analytical gradient
U = 2 * X # ∂f/∂x
V = 2 * Y # ∂f/∂y
# ---- contour + quiver plot -------------------------------------------
F = X**2 + Y**2
plt.figure(figsize=(6,6))
plt.contour(X, Y, F, levels=10, cmap="gray", linewidths=0.6)
plt.quiver(X, Y, U, V, color="tab:blue", alpha=0.8, scale=40)
plt.title(r"Gradient field $\nabla f(x,y)$ for $f=x^2+y^2$")
plt.gca().set_aspect("equal")
plt.xlabel("x"); plt.ylabel("y")
plt.tight_layout(); plt.show()
# calc-06-gradients/autograd_xy2.py
import torch
torch.set_default_dtype(torch.float64)
def f(xy):
x, y = xy
return x**2 + y**2
point = torch.tensor([1.5, -0.8], requires_grad=True)
val = f(point)
val.backward()
print(f"f({point.tolist()}) = {val.item():.3f}")
print("∇f =", point.grad.tolist()) # should be [2*1.5, 2*(-0.8)]
Output:
f([1.5, -0.8]) = 3.09
∇f = [3.0, -1.6]
Matches the analytic gradient .
w = torch.tensor([0.7, -1.2], requires_grad=True)
x = torch.tensor([1.0, 2.0])
loss = (w @ x) ** 2 # scalar
loss.backward()
print("gradient wrt weights =", w.grad)
Because , autograd returns . Exactly the multi‑dimensional chain rule in action.
torch.autograd.grad
with create_graph=True
to
compute the Jacobian of ∇f (a.k.a. the Hessian) and confirm it’s the constant
matrix [[2,0],[0,2]]
.Put solutions in calc-06-gradients/
and tag v0.1
.
Next: Calculus 7 – Jacobian & Hessian: Second‑Order Insights for Faster Learning.