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
    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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

    A new user signs up. What can matrix factorization recommend them?

    The cold start problem. Fixes: ask a few onboarding questions (like section 1), use what you know (location, the item they arrived on), and content features for new items (genre, text embeddings) so they can be recommended before anyone has rated them.

    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

    4. What to carry forward

    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