Lecture 4 · Animations
Training models
Three models from today, each trained in front of you one step at a time. Press Step to advance a single iteration and read what happened; press Play to let it run. Every number in the side panel is recomputed at every step.
All data on this page is synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture.
1Gradient descent on the cost surface
A straight line $\hat y = \theta_0 + \theta_1 x$ fitted by descent. On the left, the data and the current line; on the right, the cost $J(\theta_0, \theta_1)$ as a contour map, with every step the algorithm has taken. The green cross is the normal-equation answer $\hat{\boldsymbol\theta}$ — where descent is heading.
The cost is the mean squared error, and its gradient is
$$\nabla J(\boldsymbol\theta) = \frac{2}{m}\,\mathbf{X}^{\intercal}\bigl(\mathbf{X}\boldsymbol\theta - y\bigr).$$Each step moves against it: $\boldsymbol\theta^{(k+1)} = \boldsymbol\theta^{(k)} - \eta\,\nabla J(\boldsymbol\theta^{(k)})$. The learning rate $\eta$ is a hyperparameter: too small and the cost crawls, too large and every step overshoots the minimum.
- Each step crosses the contour lines at right angles: the gradient is perpendicular to the level set it starts on.
- With raw $x$ the bowl is a long thin valley, and descent zig-zags across it. Standardising $x$ makes it round, and the same $\eta$ walks straight in.
- Stochastic and mini-batch steps are noisy: each one uses only the highlighted points, so it is a guess at the true gradient.
- Raise $\eta$ and press Play until the cost curve turns upward and the path spirals out: that learning rate is too large.
- Lower $\eta$ to just below that point: about the fastest it gets.
- Switch to stochastic with a large $\eta$, then lower $\eta$: the cloud around $\hat{\boldsymbol\theta}$ shrinks with it.
2Logistic regression
Two classes in the plane, and a model that outputs a probability $\hat p = \sigma(\boldsymbol\theta^{\intercal}\mathbf{x})$. The shading is $\hat p$ itself; the solid line is $\hat p = 0.5$, the dashed ones $\hat p = 0.1$ and $0.9$. On the right, every point sits on the logistic curve at its own score $t = \boldsymbol\theta^{\intercal}\mathbf{x}$. Click the plot to add points.
Each step of descent lowers the log loss:
$$\begin{aligned}J(\boldsymbol\theta) = -\frac{1}{m}\sum_{i}\Big[&y^{(i)}\log\hat p^{(i)} \\ &+ (1-y^{(i)})\log(1-\hat p^{(i)})\Big].\end{aligned}$$- Points far on the wrong side of the line sit at the flat end of the curve but carry the largest loss — their term $-\log\hat p$ grows without limit.
- The boundary $\hat p = 0.5$ is always a straight line: it is $\boldsymbol\theta^{\intercal}\mathbf{x} = 0$.
- Add one gold triangle deep inside the blue cloud and watch how far it pulls the boundary — then compare a point just over the line.
3Softmax regression
Three classes. Each class $k$ has its own score $t_k = \boldsymbol\theta_k^{\intercal}\mathbf{x}$, and softmax turns the three scores into three probabilities that sum to one. The colour is the class with the highest probability, and fades where the model is unsure. Click anywhere on the plot to probe it: the bars show the scores and the probabilities at that point.
with $y^{(i)}$ one-hot: the log of the probability given to the true class, averaged.
- Each boundary between two regions is a straight line, the set where $t_j = t_k$. Three classes give at most three such lines, meeting at a point.
- Adding the same number to all three scores changes nothing: softmax only sees their differences.
- Probe a point exactly where the three regions meet: three bars of about $1/3$.
- Probe a point far out in one region: one probability near 1 even though the scores differ by only a few units — softmax is exponential.