#301ListNet Listwise Ranking LossMedium

ListNet Listwise Ranking Loss

Background

ListNet treats learning-to-rank as a probability distribution problem: define a softmax-derived distribution over permutations from both the predicted scores and the true relevance labels, then minimise their cross-entropy. The simplest listwise loss in production use.

Problem statement

Implement listnet_loss(scores, relevances). Let ppred=softmax(scores)p^{\text{pred}} = \text{softmax}(\text{scores}) and ptrue=softmax(relevances)p^{\text{true}} = \text{softmax}(\text{relevances}) over the candidate list. The loss is the cross-entropy:

L  =  ipitruelogpipred\mathcal L \;=\; -\sum_i p^{\text{true}}_i \log p^{\text{pred}}_i

Use numerically stable softmax (subtract max before exp).

Input

  • scores — 1-D array of floats, length nn, the model's predicted scores.
  • relevances — 1-D array of floats, length nn, the true relevance labels.

Output

Returns a Python float >= 0.

Examples

Example 1 — identical scores and relevances → low loss

Input:  scores = relevances = [1.0, 2.0, 3.0]
Output: ≈ entropy of softmax([1,2,3])

Example 2 — reversed scores → higher loss

Input:  scores = reversed(relevances)
Output: much higher than the matched case

Constraints

  • Length mismatch raises ValueError.
  • Softmax must be numerically stable (subtract max).
  • Returns a plain Python float.

Notes

  • Top-1 vs top-k. This is the top-1 ListNet (probability the item is the best). The original paper extends to top-kk permutations; top-1 is the version that scales.
  • vs pairwise. Listwise sees all items at once and balances them; pairwise (BPR / RankNet) sees one pair at a time. Listwise generally gives a small NDCG improvement at scale.
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: identical scores and relevances
  • Example: reversed predictions raise the loss
  • Sample: two-item cross-entropy value