P2: Matrix factorization with BPR (Rendle, Freudenthaler, Gantner & Schmidt-Thieme, 2009)¶
- Status: done (trend not reproduced; reason written up)
- Code:
recsys/models/bpr.py - Experiment: EXP-002
What the paper did¶
Most real feedback is implicit: purchases, clicks, plays. You see what people chose, never what they disliked. Earlier methods treated every unseen item as a "no" and tried to predict a score for each one. BPR (Bayesian Personalized Ranking) instead learns a ranking: for each user, an item they chose should score higher than an item they didn't. It trains on random triples (user, chosen item, unchosen item) with stochastic gradient descent, and works with any scoring model. Paired with matrix factorization, it became the standard way to train embeddings on implicit data.
Why they built it this way¶
The Netflix Prize had shown that learned embeddings (matrix factorization) beat neighbourhood methods for predicting ratings. But most products have no ratings, and the real goal is the order of a list, not a predicted star rating. BPR keeps the embeddings and changes the objective to the one that matters: rank chosen items above the rest. Sampling triples keeps each update cheap, so it scales to large catalogues.
What I built¶
- Each user and item gets a vector of 32–128 numbers, plus a bias per item. Score = user vector · item vector + item bias.
- Training triples: a real (user, item) pair plus one uniformly random item the user does not have (re-drawn if they do).
- The BPR loss, −log σ(score(u, i) − score(u, j)), minimised with mini-batch SGD and L2 regularisation, in plain numpy.
- Early stopping: each setting trains once and is scored on validation at several epoch counts.
What differs¶
- Item bias: not in the paper's MF model; a common addition that lets the model learn popularity separately.
- Mini-batches of 4,096 triples instead of one triple at a time.
- Evaluation: Recall@K and NDCG@10 on the following month (a time split), not AUC.
- Data: Video_Games 5-core (25,612 items, about 95k users, median 6 interactions per user).
Result vs the paper's trend¶
| Model (test, Dec 2022) | Recall@10 | Recall@50 | NDCG@10 |
|---|---|---|---|
| Previous step: P1 item-to-item | 0.0187 | 0.0541 | 0.0101 |
| This project: BPR, 500 epochs | 0.0168 | 0.0520 | 0.0095 |
| Bar to beat: 30-day popularity | 0.0280 | 0.0691 | 0.0186 |
The trend does not reproduce here. BPR ties item-to-item (all 95% intervals cross zero) and loses to 30-day popularity. The test here is the following month, and new games are a third of that month's demand; their vectors are barely trained, so learned embeddings gain nothing over item similarity.
What I learned¶
- Matrix factorization learns popularity first. For about 15 epochs the item bias soaks up popularity; taste arrives later. A short run makes MF look useless, so the epoch count must be tuned on validation.
- Embeddings do not fix freshness. Like item similarity, they are a memory of the past. The fix has to be a feature of time, which is exactly what P3's "example age" adds.
- Cost matters. BPR needed 16 minutes of training to tie a 3-second baseline. In production, that difference is paid every retrain.