Applications of Machine Learning

Lecture 21 · Animations

Recommender systems: from ratings to factors

One small ratings matrix, three ways of filling in what it does not say: per-user and per-film offsets, the truncated SVD of a matrix whose gaps have been filled, and a factorisation fitted to the observed ratings only. Press Step to advance one unit and read what happened; press Play to let it run.

All data on this page is synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture. The same matrix is used throughout: 60 users, 30 films, 460 ratings on a 1–5 scale (25.6% of the cells), drawn from a rank-2 taste model with user and film offsets and noise, rounded to whole stars. 92 ratings are held out at random, each leaving its user and its film at least two training ratings, and every RMSE called held-out is measured on them.

1How much is the user, how much the film

The bias model $\hat r_{ui} = \mu + b_u + b_i$, fitted by alternating closed forms: each Step solves for every user offset $b_u$ with the film offsets held fixed, or the other way round. On the left, every offset against the number of ratings it was estimated from; the hollow marker is the plain average it would have been without shrinkage. On the right, held-out RMSE for the baselines, the bias model as it is now, and the factorisation of widget 3.

The mathematics

The bias model minimises, over the training ratings $\Omega$,

$$\begin{aligned}J_b = &\sum_{(u,i)\in\Omega}\bigl(r_{ui} - \mu - b_u - b_i\bigr)^2 \\ &+ \lambda\Bigl(\sum_u b_u^2 + \sum_i b_i^2\Bigr).\end{aligned}$$

With the $b_i$ fixed, $J_b$ is a quadratic in each $b_u$ alone; setting its derivative to zero gives the slide’s closed form

$$b_u = \frac{\sum_{i\in I_u}(r_{ui} - \mu - b_i)}{|I_u| + \lambda},$$

and symmetrically for $b_i$. Each half-step is therefore an exact minimisation, so $J_b$ never rises. Writing the plain average as $\bar e_u$, the shrunk offset is $b_u = \frac{|I_u|}{|I_u|+\lambda}\,\bar e_u$: the fewer the ratings, the harder the pull towards zero.

Watch for
  • The funnel: on the left, the lines from hollow to filled markers are longest at small counts. A user with two ratings keeps only $2/(2+\lambda)$ of their average offset.
  • The fit settles within a few half-steps: there is no interaction between users and films for the alternation to untangle.
  • Two averages and a shrinkage parameter close about half of the gap between the global mean’s held-out RMSE and the factorisation’s on this matrix — gap closed in the side panel. On MovieLens the same comparison gave 80.4%.
Try this
  1. Set $\lambda = 0$: the hollow and filled markers coincide, and the offsets of the sparsest users and films spread the widest.
  2. Set $\lambda = 30$: every offset is pulled towards zero, and the held-out RMSE climbs back towards the global mean’s.

2Why the SVD cannot simply be taken

Fill every empty cell with a number — zero, or the global mean $\mu$ — and the truncated SVD gives the best rank-$d$ approximation of the filled matrix. Each Step adds one singular component. Top: the filled matrix and its rank-$d$ approximation (films down, users across; darker is higher; red outlines are the held-out cells). Bottom: RMSE against $d$, on the training ratings and on the held-out ones, for both fills.

The mathematics

Write the filled matrix $M = U\Sigma V^{\top}$ with singular values $\sigma_1 \ge \sigma_2 \ge \dots$, and keep $d$ of them: $M_d = U_d\Sigma_d V_d^{\top} = M V_d V_d^{\top}$. Eckart–Young says $M_d$ is the closest rank-$d$ matrix to $M$, with

$$\lVert M - M_d\rVert_F^2 = \sum_{k>d}\sigma_k^2 ,$$

so the error falls with every component and is zero at full rank. The Frobenius norm sums over every cell, filled ones included, with equal weight. The $\sigma_k^2$ are the eigenvalues of $M^{\top}M$; the page finds them by Jacobi rotations.

Watch for
  • The zero-filled approximation is dragged towards zero everywhere: its held-out RMSE is more than twice the global mean’s at every rank.
  • The training RMSE falls as $d$ grows, for both fills. The held-out RMSE does not follow: the extra components fit the fill and the noise, not the tastes.
  • At full rank, $d = 30$, the SVD returns the matrix it was given, so every held-out prediction is the fill value itself. The mean fill then scores exactly the global mean’s RMSE.
Try this
  1. Play the mean fill to the end and find its best rank: on this matrix it never reaches the bias model of widget 1, let alone the factorisation fitted to the observed entries.
  2. Switch the maps to the zero fill: four cells in five of the filled matrix are zero, and the approximation spends its rank on reproducing them.

3Factorise only what was observed

The full model $\hat r_{ui} = \mu + b_u + b_i + p_u\cdot q_i$ with $d = 2$: the biases are fitted first, as in widget 1 with $\lambda = 3$, and the factors model what they leave. On the left, users ($p_u$) and films ($q_i$) in the same plane; click a user to see its arrow and the lines along which $p_u\cdot q$ is $-1$, $0$ and $+1$ star. On the right, the first 12 users and 15 films: training ratings in bold, held-out cells outlined in red with the prediction underneath, and every other cell showing the model’s prediction faintly.

The mathematics

With $\tilde r_{ui} = r_{ui} - \mu - b_u - b_i$, ALS minimises

$$\begin{aligned}J = &\sum_{(u,i)\in\Omega}\bigl(\tilde r_{ui} - p_u\cdot q_i\bigr)^2 \\ &+ \lambda\bigl(\lVert P\rVert_F^2 + \lVert Q\rVert_F^2\bigr).\end{aligned}$$

Fixing $Q$ makes each $p_u$ a ridge regression; setting the gradient to zero gives $p_u = (Q_u^{\top}Q_u + \lambda I)^{-1}Q_u^{\top}\tilde r_u$, where $Q_u$ holds only the films $u$ rated. Each half-step is exact, so $J$ never rises. SGD uses the slide’s update, $e_{ui} = \tilde r_{ui} - p_u\cdot q_i$, $p_u \mathrel{+}= \eta(e_{ui}q_i - \lambda p_u)$, $q_i \mathrel{+}= \eta(e_{ui}p_u - \lambda q_i)$. That applies $\lambda$ once per rating, the penalty $\lambda(\lVert p_u\rVert^2 + \lVert q_i\rVert^2)$ inside the sum, so its $\lambda$ has its own scale.

Watch for
  • With ALS, $J$ falls at every half-step — the narration checks it — while the held-out RMSE is free to rise.
  • Each user’s predictions are projections: films on the “$+1$” line are predicted one star above the user’s and the film’s offsets.
Try this
  1. With ALS, set $\lambda$ to its minimum and play: the training RMSE stays low, but the held-out RMSE climbs far above the bias model’s. A user with two training ratings gives two equations for two unknowns, is fitted exactly, and has learned nothing.
  2. Switch to SGD, set its $\lambda$ to the minimum and play: the held-out RMSE bottoms out and then rises while the training RMSE keeps falling. The epoch count is a capacity parameter too.
  3. At the default settings, play ALS and then SGD to the end and compare best held-out so far: different routes, within a hundredth of each other. SGD’s held-out RMSE then drifts up as the epochs continue.