Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Matrix Factorization

BPR and Pairwise Ranking

Learn pairwise ranking for recommenders: the BPR loss, negative sampling, the SGD update, and a NumPy implementation compared with a popularity baseline.

Rating-error losses and ALS aim at predicting a number for each cell. But a recommender is judged on the order of a short list: did the item the user actually liked come above the ones they would not have liked? Bayesian Personalized Ranking (BPR) optimizes that directly. Instead of asking "what score should this item get?", it asks "should item i be ranked above item j for this user?"

BPR is one of the most widely used losses for implicit feedback, and its core idea (compare a positive with a sampled negative) appears again in two-tower models, contrastive learning and modern retrieval systems.

Training triples

For each user u, an item i they interacted with is a positive. Any item they did not interact with is a candidate negative j. BPR trains on triples (u, i, j) and wants the model to give:

TEXT
score(u, i)  >  score(u, j)

With matrix factorization the score is x_u ยท y_i (you can add an item bias). The loss for one triple is the negative log of a sigmoid of the score difference:

TEXT
loss = - ln ฯƒ( score(u,i) - score(u,j) )  +  ฮป ( |x_u|ยฒ + |y_i|ยฒ + |y_j|ยฒ )

ฯƒ is the logistic function. The loss is small when the positive outscores the negative by a wide margin and large when the order is wrong. Minimizing it approximately maximizes AUC, the chance that a random positive is ranked above a random negative.

The SGD update

Let d = score(u,i) โˆ’ score(u,j) and g = ฯƒ(โˆ’d) = 1 / (1 + e^d). That g is large when the pair is ordered wrongly, so wrongly ordered pairs get large updates. Then:

TEXT
x_u += lr * ( g * (y_i - y_j) - ฮป x_u )
y_i += lr * ( g * x_u        - ฮป y_i )
y_j += lr * ( -g * x_u       - ฮป y_j )

The positive item moves toward the user, the sampled negative moves away. A pair that is already ordered correctly by a large margin has g โ‰ˆ 0 and barely changes anything.

One BPR training step

Pick a user And one of their positives
Sample a negative Something they have not used
Compare scores Positive should win
Nudge vectors Bigger nudge if it lost

No ratings needed, only an ordering.

1 / 2

Implementing BPR

The data generator is the same one used in the ALS lesson: 120 users in three taste groups, with 20% of each user's interactions hidden for testing. We train with uniformly sampled negatives and report recall at 10.

Python BPR matrix factorization

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Watch the recall rise as epochs pass: with random vectors it starts around 0.15, and by epoch 30 it is about 0.40, well above the 0.21 that the non-personalized popularity ranking reaches on the same split. BPR needs more epochs than ALS here because each step only looks at one sampled pair.

Negative sampling matters

A catalog has far more non-interactions than interactions, so BPR cannot use every negative. It samples one (or a few) per positive. The sampling strategy changes what the model learns:

  • Uniform sampling is simple and unbiased but wastes work, because most random items are already ranked far below the positive, so g is near zero.
  • Popularity-based sampling draws negatives in proportion to their popularity (often with a power such as 0.75). Popular items that the user ignored are more informative than obscure ones.
  • Hard negative mining picks, among several random candidates, the one the current model scores highest, i.e. the most confusing wrong answer. It trains faster but risks including items the user would have liked and merely never saw (false negatives).
  • Exposure-aware sampling uses impression logs: an item the user was shown and skipped is a much better negative than one they never saw.

The next block compares uniform sampling with picking the hardest of five candidates on the same data and prints the final recall of both.

Python Uniform vs hard negative sampling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 12 epochs the hardest-of-five run reaches about 0.38 against 0.34 for uniform sampling; on a catalog this small the gap is modest and noisy. Hard negatives pay off most on large catalogs, where a uniformly drawn negative is nearly always trivially easy.

BPR in practice

  • Model-agnostic. BPR is a loss, not a model. You can plug in any scorer: factorization, a neural network, a graph model. Matrix factorization with BPR is just the simplest case.
  • Related losses. WARP and its approximations weight a mistake by how deep the positive is buried in the ranking, which focuses on the top of the list. Softmax (sampled) losses compare one positive with many negatives at once.
  • Evaluate on ranking metrics. Pairwise losses do not produce calibrated scores, so predicted values are meaningful only for ordering. See ranking metrics.
  • Libraries. implicit and LightFM offer BPR and WARP training in compiled code; the loop above is for understanding.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does one BPR training example consist of?

    BPR trains on triples (u, i, j) and wants the positive i to be scored above the negative j.
  2. 2. When does a BPR update change the vectors a lot?

    The weight g = ฯƒ(โˆ’d) is close to 1 for wrongly ordered pairs and close to 0 for pairs that are already well separated.
  3. 3. Why is a shown-but-skipped item a better negative than a random unseen item?

    Random unseen items may be false negatives. Impressions show the user actually declined them.
  4. 4. BPR is best described as

    BPR is a loss function; the scorer behind it can be factorization or a neural network.

Summary

  • BPR learns from triples (user, positive item, sampled negative item) and wants the positive scored higher.
  • The loss is โˆ’ln ฯƒ(score_pos โˆ’ score_neg) and approximately maximizes AUC.
  • Wrongly ordered pairs receive large updates; correct ones barely change anything.
  • Negative sampling (uniform, popularity-based, hard, exposure-aware) strongly affects quality.
  • It is a loss, usable with any scoring model, and is judged with ranking metrics.

Next: Neural Collaborative Filtering