#347Pairwise Ranking Loss (BPR / RankNet)MediumML System DesignLoss FunctionsAsked atMeta · Google · Amazon
Pairwise Ranking Loss (BPR / RankNet)
Background
Pairwise ranking loss treats ranking as a series of pairwise preference choices: positive item should outrank negative item . The Bradley-Terry / RankNet form uses a logistic on the score difference; BPR (Bayesian Personalised Ranking) is the same loss applied to implicit-feedback recsys.
Problem statement
Implement pairwise_ranking_loss(scores_pos, scores_neg) returning the mean BPR/RankNet loss:
where are the model scores for the positive and negative items in pair .
Input
scores_pos- 1-D array of positive scores, length .scores_neg- 1-D array of negative scores, length .
Output
float >= 0, the mean loss.
Examples
Input: scores_pos=[2.0], scores_neg=[1.0]
Output: -ln(sigma(1)) ~= 0.3133
Constraints
- Use a numerically stable softplus: . The naive form overflows for very negative .
- Lengths must match; ValueError otherwise.
Notes
- Pairwise vs pointwise. Pointwise (BCE on individual items) optimises calibration; pairwise optimises ordering. Production rankers often use pointwise + pairwise auxiliary.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •reference single pair pos-neg=1
- •example two pairs
- •sample asymmetric triple