Search

Search papers and lessons

Pairwise Ranking Loss

Level 3 · 12 min read

A pairwise ranking loss teaches a model to score a preferred item above a rejected one, by penalising the model whenever the pair is in the wrong order or too close.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Losses & regularization
  • Applied ML
  • Neural network basics
  • ReadReady to readNot yet

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 sA=2.0s_A = 2.0, sB=0.5s_B = 0.5 and sC=−1.0s_C = -1.0. The user clicked A and did not click B, so we want A above B. That is one training pair, written (A,B)(A, B).

  1. Take a pair. Pick the preferred item AA and the rejected item BB. Check: the pair records a fact the data gives us, that A was observed and B was not.
  2. Subtract the scores. Compute d=sA−sB=2.0−0.5=1.5d = s_A - s_B = 2.0 - 0.5 = 1.5. Check: a positive dd means A is above B, which is the right order.
  3. Turn the difference into a probability. The sigmoid σ(d)=1/(1+e−d)\sigma(d) = 1/(1 + e^{-d}) maps the difference to a number between 0 and 1, read as the probability that A outranks B. Here σ(1.5)=1/(1+e−1.5)≈1/1.2231≈0.818\sigma(1.5) = 1/(1 + e^{-1.5}) \approx 1/1.2231 \approx 0.818. Check: a difference of 0 gives probability 0.5, which is a coin flip, as it should be.
  4. Take the negative log. The loss is L=−ln⁡σ(d)≈−ln⁡(0.818)≈0.201L = -\ln \sigma(d) \approx -\ln(0.818) \approx 0.201. Check: when the probability is near 1 the loss is near 0, and when it is near 0 the loss grows without bound.
  5. See what a wrong order costs. Now suppose sBs_B were 2.5 instead, so d=−0.5d = -0.5. Then σ(−0.5)≈0.378\sigma(-0.5) \approx 0.378 and L=−ln⁡(0.378)≈0.974L = -\ln(0.378) \approx 0.974. Check: the wrong order costs almost five times as much as the right one, so the training pushes hard against it.
  6. Move the scores. Training follows the gradient. Raising sAs_A or lowering sBs_B lowers LL, so the update moves A up and B down. Check: at d=1.5d = 1.5 the push is small, 1−σ(1.5)≈0.181 - \sigma(1.5) \approx 0.18, because the pair is already in good order.

The plot below shows the loss as a function of dd. The two marked points are steps 4 and 5.

−4−3−2−10123401234score difference d = s_preferred - s_rejectedloss
  • pairwise loss, -ln σ(d)
The loss falls toward 0 as the preferred item pulls ahead, and grows roughly in a straight line when the order is wrong.

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

Definition: Pairwise ranking loss

Let s(u,i)s(u, i) be the score the model gives item ii for user uu. For a triple (u,i,j)(u, i, j) where ii is preferred over jj, the loss is L(u,i,j)=−ln⁡σ(s(u,i)−s(u,j)),L(u, i, j) = -\ln \sigma\big(s(u,i) - s(u,j)\big), where σ(d)=1/(1+e−d)\sigma(d) = 1/(1 + e^{-d}) is the sigmoid. Training minimises the average of LL over sampled triples, often with a small penalty λ∥θ∥2\lambda \lVert\theta\rVert^2 on the model parameters θ\theta.

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 d=s(u,i)−s(u,j)d = s(u,i) - s(u,j). A positive dd means the preferred item is ahead. The loss is then a function of one number:

L=−ln⁡σ(d)=ln⁡(1+e−d).L = -\ln\sigma(d) = \ln\big(1 + e^{-d}\big).

The second form follows because −ln⁡(1/x)=ln⁡x-\ln(1/x) = \ln x with x=1+e−dx = 1 + e^{-d}. It is easy to compute and shows the shape directly. For large dd the term e−de^{-d} is tiny, so L≈0L \approx 0. For large negative dd the term e−de^{-d} is huge, and L≈−dL \approx -d.

Step 3 in symbols: the sigmoid and its probability

The sigmoid satisfies σ(d)+σ(−d)=1\sigma(d) + \sigma(-d) = 1. So σ(d)\sigma(d) is the probability that the model ranks ii above jj, and −ln⁡σ(d)-\ln\sigma(d) is the negative log-likelihood of that ordering. Minimising LL is therefore the same as maximum likelihood on the observed pairs, which connects to Maximum Likelihood Estimation.

Numbers from the walkthrough: σ(1.5)≈0.818\sigma(1.5) \approx 0.818 gives L≈0.201L \approx 0.201, and σ(−0.5)≈0.378\sigma(-0.5) \approx 0.378 gives L≈0.974L \approx 0.974.

Step 4 in symbols: the gradient

Differentiate with respect to dd. Since dddln⁡σ(d)=1−σ(d)\frac{d}{dd}\ln\sigma(d) = 1 - \sigma(d),

∂L∂d=−(1−σ(d))=−σ(−d).\frac{\partial L}{\partial d} = -\big(1 - \sigma(d)\big) = -\sigma(-d).

Chain rule gives the update for the scores: ∂L/∂s(u,i)=−σ(−d)\partial L/\partial s(u,i) = -\sigma(-d) and ∂L/∂s(u,j)=+σ(−d)\partial L/\partial s(u,j) = +\sigma(-d). Check: at d=1.5d = 1.5, σ(−1.5)≈0.182\sigma(-1.5) \approx 0.182, the push from step 6. At d=−0.5d = -0.5 it is σ(0.5)≈0.622\sigma(0.5) \approx 0.622, a much stronger push.

Step 5 in symbols: a dot-product model

In a common recommender, each user has a vector pu\mathbf{p}_u and each item a vector qi\mathbf{q}_i, and the score is their Dot Product, s(u,i)=pu⋅qis(u,i) = \mathbf{p}_u \cdot \mathbf{q}_i. Then the loss is a function of the parameters through d=pu⋅(qi−qj)d = \mathbf{p}_u \cdot (\mathbf{q}_i - \mathbf{q}_j), 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

Example
1. One pair, one number

Scores: sA=1.0s_A = 1.0, sB=0.0s_B = 0.0. The pair (A,B)(A, B) is in the right order.

  1. The difference is d=1.0−0.0=1.0d = 1.0 - 0.0 = 1.0.
  2. The sigmoid is σ(1)=1/(1+e−1)≈1/1.3679≈0.731\sigma(1) = 1/(1 + e^{-1}) \approx 1/1.3679 \approx 0.731.
  3. The loss is −ln⁡(0.731)≈0.313-\ln(0.731) \approx 0.313.
  4. Check the formula ln⁡(1+e−1)=ln⁡(1.3679)≈0.313\ln(1 + e^{-1}) = \ln(1.3679) \approx 0.313. 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.

Example
2. The standard case: a batch of three pairs

A user has positive item ii and two sampled negatives j1j_1 and j2j_2. Scores: s(u,i)=2.0s(u,i) = 2.0, s(u,j1)=1.0s(u,j_1) = 1.0, s(u,j2)=2.5s(u,j_2) = 2.5.

  1. Pair (i,j1)(i, j_1): d1=2.0−1.0=1.0d_1 = 2.0 - 1.0 = 1.0, so L1=ln⁡(1+e−1)≈0.313L_1 = \ln(1 + e^{-1}) \approx 0.313.
  2. Pair (i,j2)(i, j_2): d2=2.0−2.5=−0.5d_2 = 2.0 - 2.5 = -0.5, so L2=ln⁡(1+e0.5)≈ln⁡(2.6487)≈0.974L_2 = \ln(1 + e^{0.5}) \approx \ln(2.6487) \approx 0.974.
  3. The batch loss is the average: (0.313+0.974)/2=0.6435(0.313 + 0.974)/2 = 0.6435, about 0.644.
  4. The gradient is largest for the second pair, because the item j2j_2 sits above the positive. Its push is σ(0.5)≈0.622\sigma(0.5) \approx 0.622, while the first pair's push is σ(−1)≈0.269\sigma(-1) \approx 0.269.
  5. The update therefore lifts ii and lowers j2j_2 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.

Example
3. Realistic: a search query

A search engine ranks documents for the query "trail running shoes". Two documents are shown to a rater, and the rater prefers document XX to document YY. The model currently scores them s(X)=0.8s(X) = 0.8 and s(Y)=1.3s(Y) = 1.3, so it has the order backwards.

  1. The difference is d=s(X)−s(Y)=0.8−1.3=−0.5d = s(X) - s(Y) = 0.8 - 1.3 = -0.5.
  2. The loss for this pair is ln⁡(1+e0.5)≈0.974\ln(1 + e^{0.5}) \approx 0.974.
  3. A gradient step moves s(X)s(X) up and s(Y)s(Y) down, with a push of size σ(0.5)≈0.622\sigma(0.5) \approx 0.622 on each score.
  4. 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 s(preferred)=3.0s(\text{preferred}) = 3.0 and s(rejected)=1.0s(\text{rejected}) = 1.0. What is the loss, to two decimal places? Answer: d=2.0d = 2.0, L=ln⁡(1+e−2)=ln⁡(1.1353)≈0.13L = \ln(1 + e^{-2}) = \ln(1.1353) \approx 0.13.

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 dd of the two scores, and it is −ln⁡σ(d)-\ln\sigma(d).
  • 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 spreferred=2s_{\text{preferred}} = 2 and srejected=2s_{\text{rejected}} = 2. What is the loss, and why?Show answer

The difference is d=0d = 0, so σ(0)=0.5\sigma(0) = 0.5 and the loss is −ln⁡(0.5)=ln⁡2≈0.693-\ln(0.5) = \ln 2 \approx 0.693. The model is undecided about the pair, so it gives the pair a probability of one half, and the loss sits at ln⁡2\ln 2.

QuestionA pair has spreferred=0.5s_{\text{preferred}} = 0.5 and srejected=1.5s_{\text{rejected}} = 1.5. Compute the loss to two decimal places.Show answer

The difference is d=0.5−1.5=−1d = 0.5 - 1.5 = -1. The loss is ln⁡(1+e1)=ln⁡(1+2.718)=ln⁡(3.718)≈1.31\ln(1 + e^{1}) = \ln(1 + 2.718) = \ln(3.718) \approx 1.31.

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 ii and never see item jj. Why is it risky to label (i,j)(i, j) as a pair with ii preferred?Show answer

The missing click only means jj 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.

Sources