Implement the pairwise margin ranking loss and its subgradients

Quick Overview

Implement the pairwise margin ranking loss in NumPy and return the mean loss together with its subgradients for both score arrays. It tests hinge losses on score differences, correct handling of the kink, averaging over the batch, and numerically safe vectorized code.

Implement the pairwise margin ranking loss and its subgradients

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `margin_ranking_loss(x1, x2, y, margin=1.0)` for 1-D score arrays `x1`, `x2` and labels `y`. Each term is `max(0, margin - y_i * (x1_i - x2_i))`. Return a tuple `(loss, grad_x1, grad_x2)`: the mean loss as a Python `float` and its subgradients with respect to `x1` and `x2` (arrays of shape `(n,)`). For example: ```python x1 = [2.5, 0.5, 1.0, -1.0] x2 = [1.0, 1.5, 1.0, 0.5] y = [1, 1, -1, -1] margin = 1.0 # terms: [0, 2, 1, 0] -> loss = 0.75 # expected grad_x1 = [0.0, -0.25, 0.25, 0.0] # expected grad_x2 = [0.0, 0.25, -0.25, 0.0] ``` ```hint Slope on each side of the kink Each term is piecewise linear in the score difference `x1_i - x2_i`. Work out its slope when the term is positive and when it is zero, and remember that the mean divides every term by `n`. ``` ### Constraints & Assumptions - `x1`, `x2` and `y` have equal length `n ≥ 1`, and every `y_i` is -1 or +1. - Gradients are averaged over `n`, the full batch, including pairs that contribute no loss. - A term exactly at the hinge (value 0) contributes zero loss and zero subgradient. - Scores may be as large as about `1e6`, and the results must stay finite. ### What a Strong Answer Covers - A vectorized computation of the per-pair term and the mean loss over all `n` pairs, returned as a Python `float`. - The subgradient with respect to `x1` on the active pairs, and why the gradient for `x2` has the opposite sign. - Correct treatment of a term that is exactly zero, so it adds neither loss nor gradient. - Numerical behavior for large scores, plus time and space complexity. - A check of the output against the worked example. ### Follow-up Questions - How does this loss relate to the hinge loss used in SVMs? - How would you get a smooth, everywhere-differentiable alternative, and how would you compute it without overflow? - How would you check the returned gradients numerically, and what goes wrong with finite differences near the hinge?

Overview: Implement the pairwise margin ranking loss in NumPy and return the mean loss together with its subgradients for both score arrays. It tests hinge losses on score differences, correct handling of the kink, averaging over the batch, and numerically safe vectorized code.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement margin_ranking_loss(x1, x2, y, margin=1.0) for 1-D score arrays x1, x2 and labels y. Each term is max(0, margin - y_i * (x1_i - x2_i)). Return a tuple (loss, grad_x1, grad_x2): the mean loss as a Python float and its subgradients with respect to x1 and x2 (arrays of shape (n,)).

For example:

x1 = [2.5, 0.5, 1.0, -1.0]
x2 = [1.0, 1.5, 1.0,  0.5]
y  = [1,   1,   -1,  -1]
margin = 1.0

# terms: [0, 2, 1, 0] -> loss = 0.75
# expected grad_x1 = [0.0, -0.25,  0.25, 0.0]
# expected grad_x2 = [0.0,  0.25, -0.25, 0.0]

Constraints & Assumptions

  • x1 , x2 and y have equal length n ≥ 1 , and every y_i is -1 or +1.
  • Gradients are averaged over n , the full batch, including pairs that contribute no loss.
  • A term exactly at the hinge (value 0) contributes zero loss and zero subgradient.
  • Scores may be as large as about 1e6 , and the results must stay finite.

What a Strong Answer Covers Guidance

  • A vectorized computation of the per-pair term and the mean loss over all n pairs, returned as a Python float .
  • The subgradient with respect to x1 on the active pairs, and why the gradient for x2 has the opposite sign.
  • Correct treatment of a term that is exactly zero, so it adds neither loss nor gradient.
  • Numerical behavior for large scores, plus time and space complexity.
  • A check of the output against the worked example.

Follow-up Questions Guidance

  • How does this loss relate to the hinge loss used in SVMs?
  • How would you get a smooth, everywhere-differentiable alternative, and how would you compute it without overflow?
  • How would you check the returned gradients numerically, and what goes wrong with finite differences near the hinge?