§ Gradients · Interactive Graduate Primer
Tensor by Tensor · expanded

Gradient descent

Learn how changing several inputs changes one output. Begin with partial derivatives, then explore gradient descent, backpropagation, and optimizers. The later sections include proofs and deeper mathematical detail.
A first reading path

Start with sections 2–4, then section 7. Return to the directional-derivative proof after you understand a gradient-descent update. An optimizer is a rule for updating the model’s adjustable numbers.

For L(a, b) = a² + 2b², the gradient at (1, 2) is [2, 8]. The first entry measures change along a while b stays fixed. The second measures change along b while a stays fixed. With step size 0.1, subtract [0.2, 0.8] to reach (0.8, 1.2). The loss falls from 9 to 3.52.

Check: why subtract the gradient?

The gradient points toward the steepest local increase under the usual Euclidean length measure. Subtracting it moves toward a local decrease when the step is small enough. A large step can still overshoot and increase the loss.

7. Gradient Descent

Introduction

For a bowl-shaped loss starting at parameter 3, a learning rate of 0.1 reduces loss from 4 to about 1.05 after three updates. Larger steps can move faster, oscillate, or diverge.

Learning goal

Perform several gradient-descent updates and choose stable learning rates using curvature and visual trajectory evidence.

Before you start

Gradient vectors, derivatives of quadratic functions, repeated updates, coordinate points, and basic algebra.

Lesson plan

  1. Calculate several one-dimensional updates and track parameter and loss values.
  2. Explore two-dimensional parameter trajectories where unequal curvature causes visible zig-zagging behavior.
  3. Derive a stable learning-rate bound and compare optimizer paths on Rosenbrock loss.

Calculate one step. If w=3, gradient is 4, and learning rate is 0.1, descent gives w=3−0.1×4=2.6. A positive gradient leads to a smaller parameter because descent moves opposite the increase direction.

The simplest optimiser: iterate

$$\boxed{\theta_{k+1} = \theta_k - \eta\,\nabla L(\theta_k)}$$

where $\eta>0$ is the learning rate. A first-order Taylor expansion gives $L(\theta - \eta\nabla L) = L(\theta) - \eta\|\nabla L\|^2 + o(\eta)$, so for small $\eta$ the loss drops by approximately $\eta\|\nabla L\|^2$.

7.1 One-dimensional demo

Experiment · $L(w)=(w-1)^2$, start at $w_0=3$
Adjust η and press Run.

Regimes: $\eta<0.5$ converges monotonically · $\eta=0.5$ converges in one step · $0.5<\eta<1.0$ oscillates with decay · $\eta\ge 1.0$ diverges. The threshold $\eta=1$ matches the theory $2/\lambda_{\max}=2/2=1$.

7.2 Two-dimensional ill-conditioned bowl

The loss $L(w,b)=(w-1)^2 + 4(b+2)^2$ has Hessian with eigenvalues $2$ and $8$, giving $\kappa=4$. The optimal step is $\eta < 2/\lambda_{\max}=0.25$. Larger $\eta$ causes the canonical zig-zag.

Experiment · trajectory on the bowl $L(w,b)=(w-1)^2+4(b+2)^2$

7.2.1 Learning-rate bound, derived

For $L(\theta)=\tfrac{1}{2}\theta^\top A\theta - b^\top\theta + c$ with $A\succ 0$, gradient descent becomes $\theta_{k+1}-\theta^\star = (I-\eta A)(\theta_k-\theta^\star)$, which converges iff $|1-\eta\lambda_i|<1$ for all eigenvalues $\lambda_i$. This forces

$$\boxed{0 < \eta < \frac{2}{\lambda_{\max}(A)}}.$$

Convergence rate is $(\kappa-1)/(\kappa+1)$ per step, so $\kappa\gg 1$ means slow convergence — the motivation for adaptive and second-order methods.

7.3 Optimiser race on Rosenbrock

The Rosenbrock function $f(x,y)=(1-x)^2+100(y-x^2)^2$ has a narrow curved valley with minimum at $(1,1)$. It is the classic test for optimisers. Run them head-to-head on the same landscape.

Experiment · compare SGD, SGD+momentum, and Adam
SGD (η=0.0005) SGD + momentum (η=0.0005, μ=0.9) Adam (η=0.02)