TL;DR
Many systems do not need an exact score for each item. They need the items in the right order: which search result comes first, which product to show first. A pairwise ranking loss trains a model on pairs of items, one preferred and one not, and asks that the preferred item score higher. It charges a penalty when the pair is in the wrong order and a smaller penalty when it is in the right order but too close to call. It solves the problem of learning an order from examples of which item beat which.
Builds on: Loss Function, Sigmoid Function · Leads to: Bayesian Personalized Ranking, Negative Sampling (choosing the rejected items), Triplet Loss (a related three-item version)
Where it's used
Recommendations from clicks. A shopping site or video service does not know what a user dislikes. It knows what the user clicked, bought or watched. Bayesian Personalized Ranking (BPR), published in 2009, turns this implicit feedback into pairs: an item the user interacted with should outrank an item they did not. Its loss is the pairwise loss below, and it became a standard baseline for recommender systems.
Search ranking. A search engine scores many documents for each query, and the ordering of the top results matters most. Microsoft's RankNet, introduced in 2005, trained a neural network on pairs of search results labelled by human raters, with a pairwise cross-entropy cost. Modern learning-to-rank systems keep this idea, because labelling "A is better than B" is often easier for a person than giving A an absolute score.
Session-based recommenders. In a session, a user clicks a few items in a row, and the next click should be predicted from the sequence. Recurrent models such as GRU4Rec, from 2015, were trained with pairwise losses that push the item the user actually clicked above sampled items they did not click. The loss gives the training signal, and the sequence model supplies the scores.
How it works
Take one user and three items: A, B and C. The model gives each item a score from its current parameters. Higher scores mean the model thinks the user likes the item more. Say the scores are , and . The user clicked A and did not click B, so we want A above B. That is one training pair, written .
- Take a pair. Pick the preferred item and the rejected item . Check: the pair records a fact the data gives us, that A was observed and B was not.
- Subtract the scores. Compute . Check: a positive means A is above B, which is the right order.
- Turn the difference into a probability. The sigmoid maps the difference to a number between 0 and 1, read as the probability that A outranks B. Here . Check: a difference of 0 gives probability 0.5, which is a coin flip, as it should be.
- Take the negative log. The loss is . Check: when the probability is near 1 the loss is near 0, and when it is near 0 the loss grows without bound.
- See what a wrong order costs. Now suppose were 2.5 instead, so . Then and . Check: the wrong order costs almost five times as much as the right one, so the training pushes hard against it.
- Move the scores. Training follows the gradient. Raising or lowering lowers , so the update moves A up and B down. Check: at the push is small, , because the pair is already in good order.
The plot below shows the loss as a function of . The two marked points are steps 4 and 5.
- pairwise loss, -ln σ(d)
In a real system the same steps run over many pairs at once. For each user, the training set holds positive items the user interacted with, and negative items drawn at random from the rest of the catalogue. Each step of gradient descent averages the loss over a batch of such pairs.
The math
Definition
Let be the score the model gives item for user . For a triple where is preferred over , the loss is where is the sigmoid. Training minimises the average of over sampled triples, often with a small penalty on the model parameters .
In words: the loss depends only on the difference of two scores, never on their absolute values. Adding the same constant to every score changes nothing, so the model learns relative order, which is exactly what ranking needs.
Step 2 in symbols: the difference
Write . A positive means the preferred item is ahead. The loss is then a function of one number:
The second form follows because with . It is easy to compute and shows the shape directly. For large the term is tiny, so . For large negative the term is huge, and .
Step 3 in symbols: the sigmoid and its probability
The sigmoid satisfies . So is the probability that the model ranks above , and is the negative log-likelihood of that ordering. Minimising is therefore the same as maximum likelihood on the observed pairs, which connects to Maximum Likelihood Estimation.
Numbers from the walkthrough: gives , and gives .
Step 4 in symbols: the gradient
Differentiate with respect to . Since ,
Chain rule gives the update for the scores: and . Check: at , , the push from step 6. At it is , a much stronger push.
Step 5 in symbols: a dot-product model
In a common recommender, each user has a vector and each item a vector , and the score is their Dot Product, . Then the loss is a function of the parameters through , and the gradient moves the user vector toward the difference of the two item vectors. This is why ranking models often learn embeddings.
Worked examples
Scores: , . The pair is in the right order.
- The difference is .
- The sigmoid is .
- The loss is .
- Check the formula . The two forms agree.
Interpretation: a gap of 1 in score gives a loss of about 0.31. A bigger gap pushes the loss toward zero.
A user has positive item and two sampled negatives and . Scores: , , .
- Pair : , so .
- Pair : , so .
- The batch loss is the average: , about 0.644.
- The gradient is largest for the second pair, because the item sits above the positive. Its push is , while the first pair's push is .
- The update therefore lifts and lowers the most, which is the correction the model needs.
Interpretation: a loss averaged over pairs keeps the model's attention on the pairs it gets wrong. Pairs that are already right contribute little to the gradient.
A search engine ranks documents for the query "trail running shoes". Two documents are shown to a rater, and the rater prefers document to document . The model currently scores them and , so it has the order backwards.
- The difference is .
- The loss for this pair is .
- A gradient step moves up and down, with a push of size on each score.
- Repeated over many rated pairs, these pushes move the scores until most preferred documents sit above their rejected partners.
Interpretation: the loss uses only the order that raters gave. It does not ask the model to predict a rating on a fixed scale, which is why it suits human judgments of "A is better than B".
Try it: a pair has and . What is the loss, to two decimal places? Answer: , .
Common mistakes and tradeoffs
It is tempting to think a ranking loss must sort the whole list. In fact it only compares pairs, so each update costs about the same whatever the length of the list. The full order emerges from many pairwise comparisons, not from a single sort inside the loss.
It is tempting to think an unclicked item is a true negative. In fact it is only unobserved; the user may not have seen it. Sampling negatives at random is a modelling choice, and it can make the model learn that popular items are always positive. This is why the choice of negatives is a central part of the method.
It is tempting to think a pairwise loss gives the same ranking as a loss on absolute scores. In fact a pointwise loss tries to fit each score to a target, and a pairwise loss only cares about differences. The two can disagree when the targets are on different scales.
Use a pairwise loss when the data gives you preferences or clicks and you care about the order at the top of the list. Do not use it when you need calibrated scores, such as a predicted probability for a decision threshold; then a pointwise loss such as logistic loss is more direct. The hinge alternative, Hinge Loss, is exactly zero once the margin is cleared, while this loss never reaches zero, which can keep pushing well-ordered pairs apart. Triplet losses make the same comparison in an embedding space and are common in retrieval.
History
Learning to rank became a research area in the early 2000s, because search engines needed to order documents and simple relevance scores were not enough. Human raters can often say which of two results is better more easily than they can give each one an absolute number, so a pairwise training signal fits the problem well.
RankNet, from Microsoft researchers in 2005, used a neural network trained on pairs with a probabilistic cross-entropy cost of this same sigmoid form. It showed that the approach worked at web scale. In 2009, Rendle and colleagues applied the same kind of loss to implicit feedback from clicks and purchases, and called the method Bayesian Personalized Ranking. Its training on sampled negatives became a common baseline, and later sequence recommenders adopted pairwise losses in the same way.
Recap
- A pairwise ranking loss compares two items for one user and asks that the preferred item score higher.
- Its value depends only on the difference of the two scores, and it is .
- The sigmoid turns the difference into a probability of the correct order, and the loss is its negative log.
- Gradients are large for wrong orders and small for orders that are already right, so training focuses on mistakes.
- The loss is used in recommenders such as BPR, in search ranking, and in session-based models.
Next, read the BPR paper for the recommender application, Negative Sampling for how the rejected items are chosen, and Triplet Loss for the three-item version that works in an embedding space.
Check yourself
QuestionA pair has scores and . What is the loss, and why?Show answer
The difference is , so and the loss is . The model is undecided about the pair, so it gives the pair a probability of one half, and the loss sits at .
QuestionA pair has and . Compute the loss to two decimal places.Show answer
The difference is . The loss is .
QuestionWhy does the pairwise loss use only the difference between two scores, not each score on its own?Show answer
Ranking asks only which item comes first. Adding the same constant to every score leaves every order unchanged, so a loss that depended on absolute values would waste effort fitting something the task does not need. Using the difference makes the loss depend only on the order.
QuestionA recommender sees a user click item and never see item . Why is it risky to label as a pair with preferred?Show answer
The missing click only means was not observed; the user may never have been shown it. Labelling it as a rejected item assumes a dislike that the data does not contain. Sampling negatives and treating the choice as uncertain reduces this risk.
QuestionA team wants a model whose output can be read as a probability that a user will click. Should it use only a pairwise ranking loss?Show answer
No. A pairwise loss learns order, not calibrated probabilities, so its outputs are not reliable as probabilities. A pointwise loss such as logistic loss fits each score to the click label and gives calibrated outputs. The team could combine both if it needs ordering and calibration.