INFS7410 · Information Retrieval and Web Search · Week 5

Exercise 4: margin loss, worked solution

Step-by-step calculation of the (multi-label) margin loss that RepBERT uses, for one query with one positive and three negative documents and a margin of 1. The page states the formula, works each positive–negative pair, and then shows how the loss stops caring once every pair clears the margin.

1. The problem and the margin-loss formula

Sources: slides 26–27 · Video 3 (RepBERT), 00:51–04:14 · Week 5 in-class exercise 4 (Week 5 lecture notes, in-class exercises)

Given the following relevance scores for a query qq, calculate the margin loss with margin 1. In RepBERT the score is the inner product of the mean-pooled query and document embeddings (slide 26).

CandidateRoleRel(q, ·)
p⁺positive (relevant) document1.5
p₁⁻negative document0.3
p₂⁻negative document0.1
p₃⁻negative document0.9
L=1n⋅∑1≤i≤mm<j≤nmax⁡(0, 1−(Rel(q,di+)−Rel(q,dj−))) \begin{aligned} L = \frac{1}{n} \cdot \sum_{\substack{1 \le i \le m \\ m < j \le n}} \max\Bigl(0,\ &1 - \bigl(\mathrm{Rel}(q,d_i^+) \\ &- \mathrm{Rel}(q,d_j^-)\bigr)\Bigr) \end{aligned}
Where
LL
the margin loss for this one query
qq
the query
di+d_i^+
the ii-th positive (relevant) document, 1≤i≤m1 \le i \le m
dj−d_j^-
the jj-th negative document, m<j≤nm < j \le n
mm
number of positives (here 1)
nn
index of the last document; documents m+1m+1 to nn are the negatives, so there are n−mn - m negatives (here 3)
1n\frac{1}{n}
the averaging factor; the lecture reads the sum as an average over the positive–negative pairs, so this solution divides by the number of pairs, m(n−m)=3m(n-m) = 3 (see the Watch out note in section 3)
Rel(q,d)\mathrm{Rel}(q,d)
relevance score of document dd for qq (inner product of the embeddings)
11
the margin: how far the positive should out-score each negative
max⁡(0,⋅)\max(0, \cdot)
the hinge: clips negative values to 0
Read it aloud L equals one over n times the sum, over every positive i and every negative j, of the maximum of zero and one minus the difference between the relevance of positive i and the relevance of negative j.
Formula in plain words For every pair of one positive and one negative document: take the amount by which the positive out-scores the negative, subtract that gap from 1, and clip at zero (the max with 0). A pair whose gap is already at least the margin of 1 contributes zero loss; a pair that falls short contributes the missing amount. Then take the average over all the pairs. A negative that scores higher than the positive gives a negative gap and a pair loss above 1, so wrongly ordered pairs are punished hardest. A larger margin demands a wider separation before a pair counts as satisfied.
The four scores on one line (margin = 1) within 1 of p⁺ → the pair has loss > 0 p₂⁻ p₁⁻ p₃⁻ p⁺ 0.1 0.3 0.9 1.5 1.5 − 1 = 0.5 gap 0.6 < 1 → loss 0.4
Only a negative that scores within 1 of the positive (here p₃⁻ at 0.9) produces loss; the two low-scoring negatives are already far enough below.
PracticeSlide 27 gives two single-pair cases. Compute the loss for Rel(q,d⁺) = 0.8, Rel(q,d⁻) = 0.1, and for Rel(q,d⁺) = 0.2, Rel(q,d⁻) = 0.8.
Answer Case 1:gap=0.8−0.1=0.7loss=max⁡(0,1−0.7)=0.3Case 2:gap=0.2−0.8=−0.6loss=max⁡(0,1+0.6)=1.6 \begin{aligned} \text{Case 1:}\quad \text{gap} &= 0.8 - 0.1 = 0.7 \\ \text{loss} &= \max(0, 1 - 0.7) = 0.3 \\ \text{Case 2:}\quad \text{gap} &= 0.2 - 0.8 = -0.6 \\ \text{loss} &= \max(0, 1 + 0.6) = 1.6 \end{aligned} The first loss is relatively small (the pair is close to the margin already); the second is relatively big (the ranking is wrong).

2. Worked solution

Sources: slides 26–27 · Video 3 (RepBERT), 01:29–03:35 · Week 5 in-class exercise 4

Worked example 1Margin loss for one query, one positive, three negatives

1

List the positive–negative pairs. With 1 positive and 3 negatives there are 1×3=31 \times 3 = 3 pairs, each contributing one term:

(p+,p1−), (p+,p2−), (p+,p3−) (p^+, p_1^-),\ (p^+, p_2^-),\ (p^+, p_3^-)
2

Pair 1, against p1−p_1^- (score 0.3). The gap is 1.5−0.3=1.21.5 - 0.3 = 1.2, which already exceeds the margin of 1:

max⁡(0, 1−1.2)=max⁡(0, −0.2)=0 \begin{aligned} &\max(0,\ 1 - 1.2) \\ &= \max(0,\ -0.2) = 0 \end{aligned}

The positive out-scores this negative by more than 1 full point, so the pair is “satisfied”: zero loss.

3

Pair 2, against p2−p_2^- (score 0.1). The gap is 1.5−0.1=1.41.5 - 0.1 = 1.4, also beyond the margin:

max⁡(0, 1−1.4)=max⁡(0, −0.4)=0 \begin{aligned} &\max(0,\ 1 - 1.4) \\ &= \max(0,\ -0.4) = 0 \end{aligned}
4

Pair 3, against p3−p_3^- (score 0.9). The gap is 1.5−0.9=0.61.5 - 0.9 = 0.6, which falls short of the margin of 1:

max⁡(0, 1−0.6)=max⁡(0, 0.4)=0.4 \begin{aligned} &\max(0,\ 1 - 0.6) \\ &= \max(0,\ 0.4) = 0.4 \end{aligned}

This is the one pair that still violates the margin, so it contributes a positive loss equal to the shortfall (0.4).

5

Average over the pairs. Sum the three contributions and divide by the number of pairs, 3:

L=0+0+0.43=0.43≈0.133 \begin{aligned} L &= \frac{0 + 0 + 0.4}{3} \\ &= \frac{0.4}{3} \approx 0.133 \end{aligned}
ResultL ≈ 0.133 (exactly 0.43\frac{0.4}{3}). Only the hardest negative (0.9) generates any loss; the two easy negatives are already separated by more than the margin and are ignored. The gradient therefore focuses entirely on pushing p3−p_3^- further away from the query.
00.511.5 00.511.5 gap = Rel(q,d⁺) − Rel(q,d⁻) pair loss margin pair 3: gap 0.6 → 0.4 pairs 1, 2: 0 wrong order (gap < 0): loss > 1
Each pair's loss is a hinge: it shrinks linearly as the gap grows and is exactly 0 once the gap reaches the margin.
PracticeRecompute the loss for Rel(q,p⁺) = 1.0 and negatives 0.2, 0.7, 1.1 (margin 1).
One negative out-scores the positive.
AnswerGaps: 0.8, 0.3, −0.1. Pair losses: max⁡(0,0.2)=0.2max⁡(0,0.7)=0.7max⁡(0,1.1)=1.1 \begin{aligned} \max(0, 0.2) &= 0.2 \\ \max(0, 0.7) &= 0.7 \\ \max(0, 1.1) &= 1.1 \end{aligned} Average over the 3 pairs: 2.03≈0.667 \frac{2.0}{3} \approx 0.667

3. How the loss behaves, and exam tips

Sources: slides 19, 21, 26–27 · Video 2 (DPR) · Video 3 (RepBERT), 03:35–04:26 · Week 5 in-class exercise 4

Worked example 2Sanity check the behaviour

1
Suppose training pushes Rel(q,p3−)\mathrm{Rel}(q,p_3^-) down to 0.5.
2
The gap becomes 1.5−0.5=1.0≥11.5 - 0.5 = 1.0 \ge 1, so that term becomes max⁡(0,0)=0\max(0, 0) = 0.
ResultThe total loss reaches L=0L = 0: every pair then satisfies the margin and training on this query stops. This “stop caring once it's good enough” behaviour is the key difference from InfoNCE, which always produces some gradient.
AspectInfoNCE (Exercise 3; DPR, ANCE)Margin loss (this exercise; RepBERT)
Comparesthe positive against all negatives at once, through a softmaxeach positive–negative pair independently
Zero loss whennever exactly; it approaches 0 as P(p⁺ | q) → 1every pair's gap is at least the margin
Easy negativesstill add a little to the denominatorcontribute exactly 0
Styleprobabilisticenforces a minimum separation; can give more stable training in some scenarios (Video 3)
Common trapClip each pair at zero before averaging. The most common mistake is summing the raw values (−0.2) + (−0.4) + 0.4 = −0.2 and letting the negative terms cancel the positive one. The max(0, ·) applies per pair, so satisfied pairs contribute exactly 0, never a negative amount.
Exam angleDivide by the number of pairs (here 3), following the course's reading of the formula. Contrast with Exercise 3: InfoNCE compares the positive against all negatives at once through a softmax; margin loss treats each positive–negative pair independently and only penalises pairs whose gap is below the margin. See the Exercise 3 solution.
Watch outIn the formula's own indexing, documents 1 to mm are positives and m+1m+1 to nn are negatives, so nn counts all documents (4 here); PyTorch's MultiLabelMarginLoss likewise divides by the number of candidates. Read that way, the answer would be 0.44=0.1\frac{0.4}{4} = 0.1. Video 3 describes the sum as an average over the positive–negative pairs, and the in-class answer divides by the 3 pairs, giving 0.133. Use the course convention and state the normaliser you use; the pair-by-pair reasoning (only p3−p_3^- contributes 0.4) is identical either way.
PracticeWith the original scores, what is the loss if the margin is 0.5 instead of 1?
AnswerPair losses become: max⁡(0,0.5−1.2)=0max⁡(0,0.5−1.4)=0max⁡(0,0.5−0.6)=0L=0 \begin{aligned} \max(0, 0.5 - 1.2) &= 0 \\ \max(0, 0.5 - 1.4) &= 0 \\ \max(0, 0.5 - 0.6) &= 0 \\ L &= 0 \end{aligned} A smaller margin is easier to satisfy, so even p3−p_3^- (gap 0.6) is now far enough below the positive.

4. Summary and study checklist