AI from scratch · Elective · Recommenders
Recommenders
The feed, the "you might also like", the next song: recommenders decide much of what people see online, and they're one of the most common ML systems in industry. The core idea fits in one line: describe every user and every item by a few numbers, their tastes and its traits, so that a user likes an item when the two line up. The engineering around it (millions of items, users with no history, ratings shaped by what was recommended before) is where the job gets interesting. You'll train one here on yourself.
1. Rate a few, get the rest
Sixteen NYC outings. The model has ratings from 300 made-up people (generated with hidden tastes, so the demo has something to learn from). Rate at least four outings you have an opinion on, then press Recommend. It trains a model with two taste numbers per person and two trait numbers per outing, on everyone's ratings and yours, right here.
The map is the model's own picture: each outing at its two learned numbers, and your taste as an arrow. Nobody told it what the numbers mean; they come out as directions like "culture vs sport" or "indoors vs outdoors" because those explain the ratings. A predicted rating is how far an outing lies along your arrow, plus how good the outing is in general and how generous a rater you are.
A recommender gives each user a taste vector and each item a trait vector, and predicts how much a user likes an item from how well the two line up.
2. Matrix factorization
Put the ratings in a table, users by items, mostly empty. Matrix factorization fills it in with a low-rank model (the SVD lesson's idea, learned only from the filled cells):
\[ \hat r_{ui} = \mu + b_u + b_i + p_u \cdot q_i \]
\(\mu\) is the average rating, \(b_u\) how generous user \(u\) is, \(b_i\) how good item \(i\) is overall, and \(p_u \cdot q_i\) the match between taste and traits (\(k\) numbers each, 2 here, 16 to 200 in practice). Training minimizes squared error on the known ratings plus an L2 penalty on everything (chapter 11), by gradient descent. On the MovieLens dataset in the practice notebook, with a time split: predicting the average gives an error (RMSE) of 0.91 stars; biases and a single taste number, 0.84; 16 taste numbers with a strong penalty, 0.83. The same 16 numbers with no penalty score 0.90: no better than the average, because they memorized the training ratings.
Matrix factorization fills the mostly-empty ratings table with average + user bias + item bias + taste·traits, learned from the known cells with an L2 penalty.
Why a few numbers per person are enough
- Imagine the full table, every user's rating of every item. If tastes were random, each row would be its own story and nothing could be predicted.
- But they aren't: people who love jazz clubs tend to love the Met; people who love Knicks games tend to love Yankee Stadium. Rows are mixtures of a few patterns.
- A table whose rows mix \(k\) patterns has rank about \(k\) (linear algebra 1): it's \(P Q^\top\), users' mixing weights times the patterns.
- Rank \(k\) means each of the \(m\) users and \(n\) items needs only \(k\) numbers: \(k(m + n)\) in total instead of \(mn\). For 300 people and 16 outings at \(k = 2\): 632 numbers against 4,800 cells, of which only 1,800 were rated.
- That's why the empty cells can be filled at all: far fewer unknowns than known ratings. Regularization keeps the few-data users and items from bending the patterns to their noise.
3. How real recommenders work
- Implicit feedback. Most products have clicks, plays and purchases, not star ratings. A click means some interest; no click means nothing certain (the user may never have seen it). Models are trained to rank clicked items above sampled unclicked ones (word vectors' negative sampling, again).
- Two stages. With millions of items, a retrieval model first picks a few hundred candidates fast: a two-tower network embeds users and items separately, and a vector index finds the nearest items (the retrieval lesson's nearest-neighbor search). A heavier ranking model, often boosted trees or a deep network with many features (time of day, recency, price), then orders those few hundred.
- Ranking metrics. What matters is the top of the list: recall@k (how many of the items the user later engaged with were in the top k) and NDCG (which rewards putting them first). RMSE on stars says little about a feed: in the notebook, the model with the best RMSE puts a user's next liked movies in its top 10 only 0.7% of the time, while the plain list of most-rated movies manages 3.6%. Star-prediction models favor obscure films with a few high ratings; models for feeds are trained on the ranking itself.
A new user signs up. What can matrix factorization recommend them?
- Their best personal matches
- Random items
- Only items that are good for everyone (\(\mu + b_i\), roughly popularity), until they rate or click something, or unless other information about them is used
Real systems learn from clicks, retrieve candidates with two-tower embeddings and a vector index, rank them with a richer model, and judge by the top of the list.
Where it breaks: a model that shapes its own data
- Feedback loops. Items that get recommended get clicked, and clicks become training data for the next model, which recommends them more. Popular gets more popular, and niche items never get the exposure to prove themselves (MLOps' feedback loops). Systems reserve some slots for exploration.
- Missing not at random. People rate what they chose to watch, mostly things they expected to like, so the ratings over-represent good matches. A model trained on them overestimates how much people like the unseen.
- Optimizing the wrong number. Maximizing clicks rewards clickbait; maximizing watch time rewards endless scrolling (ML strategy's gamed metric). Product teams add satisfaction surveys, diversity and long-term retention as guardrails.
- Offline isn't online. Recall@10 on last month's clicks measures how well a model predicts what the old recommender showed. Only an A/B test (Probability 4) says whether users are better off.
4. What to carry forward
- \(\hat r = \mu + b_u + b_i + p_u \cdot q_i\): biases do much of the work; tastes add the personal part; regularize.
- Few numbers per user and item suffice because tastes are mixtures of a few patterns (low rank).
- Implicit feedback, two-tower retrieval plus a ranking model, judged by recall@k and NDCG, then A/B tested.
- Cold start needs content and context; feedback loops need exploration.
All lessons: AI from scratch. The ratings in section 1 are synthetic; the notebook uses MovieLens (F. Maxwell Harper and Joseph Konstan, GroupLens, University of Minnesota), downloaded from GroupLens.
Last updated