Reward Modeling › Worked Example: Computing Preference Loss

Michael BrenndoerferDecember 24, 20256 min read
Language AI HandbookAll chapters
Part 37 of 67Alignment and RLHF
Chapter 275Reward Modeling › Worked Example: Computing Preference Loss
Reading section 6 of 10 · Worked Example: Computing Preference Loss · 6 minSection 6 of 10 · 6 min

Book contents

Section 6 of 10 · 6 min read

Reward Modeling › Worked Example: Computing Preference LossOne section · Sections in this chapter6 / 10 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's trace through the loss computation for a single preference pair to solidify understanding. Walking through a concrete numerical example makes the abstract formulas tangible and reveals how the loss function behaves in practice across different scoring scenarios.

Suppose we have a prompt asking for a simple explanation of gravity, with two candidate responses. Response A (the chosen response) gives a simple, accessible explanation using an everyday analogy. Response B (the rejected response) gives a technically correct but overly complex description using field equations. After training, our reward model produces the following scores:

  • r(x,yw)=2.1r(x, y_w) = 2.1 for the preferred response (A, the accessible explanation)
  • r(x,yl)=0.8r(x, y_l) = 0.8 for the rejected response (B, the technical description)

Step 1: Compute the reward difference.

Δr=r(x,yw)−r(x,yl)=2.1−0.8=1.3\Delta r = r(x, y_w) - r(x, y_l) = 2.1 - 0.8 = 1.3

where:

  • Δr\Delta r: the reward difference between the chosen and rejected responses
  • r(x,yw)r(x, y_w): the reward score for the preferred response (2.12.1)
  • r(x,yl)r(x, y_l): the reward score for the rejected response (0.80.8)

This positive difference of 1.3 indicates the model correctly believes the preferred response is better. The question is: how confident is this prediction, and how much loss does it incur?

Step 2: Convert to a preference probability via the sigmoid.

P(yw≻yl)=σ(1.3)(apply sigmoid to the reward difference)=11+e−1.3(substitute sigmoid definition)=11+0.2725(compute e−1.3≈0.2725)=11.2725(add denominator terms)≈0.786(final probability)\begin{aligned} P(y_w \succ y_l) &= \sigma(1.3) && \text{(apply sigmoid to the reward difference)} \\ &= \frac{1}{1 + e^{-1.3}} && \text{(substitute sigmoid definition)} \\ &= \frac{1}{1 + 0.2725} && \text{(compute } e^{-1.3} \approx 0.2725 \text{)} \\ &= \frac{1}{1.2725} && \text{(add denominator terms)} \\ &\approx 0.786 && \text{(final probability)} \end{aligned}

where:

  • P(yw≻yl)P(y_w \succ y_l): the calculated probability that the model assigns to the correct preference
  • σ(1.3)\sigma(1.3): the sigmoid function applied to the reward difference
  • 0.7860.786: the resulting probability (approximately 78.6%)

The model assigns about 78.6% probability to the correct preference. This is a reasonably confident prediction. This reflects the moderately large reward margin of 1.3 points. The model is more likely than not to get this comparison right, but it is not highly certain.

Step 3: Compute the negative log-likelihood loss.

L=−log⁡P(yw≻yl)(negative log-likelihood)=−log⁡(0.786)(substitute probability)≈0.241(evaluate natural log)\begin{aligned} \mathcal{L} &= -\log P(y_w \succ y_l) && \text{(negative log-likelihood)} \\ &= -\log(0.786) && \text{(substitute probability)} \\ &\approx 0.241 && \text{(evaluate natural log)} \end{aligned}

where:

  • L\mathcal{L}: the computed preference loss value
  • −log⁡(0.786)-\log(0.786): the negative log-likelihood of the correct preference

The loss of 0.241 is relatively small, showing the model is performing well on this example. For reference, a model that assigns exactly 50% probability (pure uncertainty) would incur a loss of −log⁡(0.5)≈0.693-\log(0.5) \approx 0.693.

Step 4: Compare with an incorrect ranking scenario.

Now consider if the model had incorrectly scored the responses, assigning higher reward to the technical description:

  • r(x,yw)=0.5r(x, y_w) = 0.5 (accessible explanation, incorrectly ranked lower)
  • r(x,yl)=1.8r(x, y_l) = 1.8 (technical description, incorrectly ranked higher)

Following the same steps: the reward difference is 0.5−1.8=−1.30.5 - 1.8 = -1.3. The sigmoid gives σ(−1.3)=1/(1+e1.3)=1/(1+3.669)≈0.214\sigma(-1.3) = 1/(1 + e^{1.3}) = 1/(1 + 3.669) \approx 0.214. The loss is −log⁡(0.214)≈1.54-\log(0.214) \approx 1.54.

Step 5: Compare the two loss values.

The loss of 1.54 for the incorrect ranking is more than six times larger than the 0.241 for the correct ranking. This asymmetry demonstrates how the loss heavily penalizes incorrect rankings while giving smaller gradients when the model is already correct. The gradient signal is strongest where the model needs improvement, naturally focusing the learning process on mistakes.

Step 6: Consider the gradient implications.

The gradient of the loss with respect to the reward difference is ∂L/∂Δr=σ(−Δr)−1=−σ(Δr)(1−σ(Δr))\partial \mathcal{L} / \partial \Delta r = \sigma(-\Delta r) - 1 = -\sigma(\Delta r)(1 - \sigma(\Delta r)). For the correct-ranking case (Δr=1.3\Delta r = 1.3), this gradient has magnitude 0.786×0.214≈0.1680.786 \times 0.214 \approx 0.168. For the incorrect-ranking case (Δr=−1.3\Delta r = -1.3), the gradient magnitude is 0.214×0.786≈0.1680.214 \times 0.786 \approx 0.168. While the gradient magnitudes are the same (by symmetry of the sigmoid), the direction is reversed: for the correct ranking, the gradient pushes toward a larger margin; for the incorrect ranking, it pushes toward flipping the order entirely. The large loss difference between the two cases reflects the log transformation, which compresses the scale near 1 and expands it near 0, creating the six-fold difference in loss from equal-magnitude gradient contributions.

Out[6]:
Visualization
Loss curve with a green dot at reward margin 1.3 and loss 0.24, annotated to show low loss for a correct ranking prediction.
Correct ranking example: the reward margin of 1.3 yields a low preference loss of 0.24, showing the model is already confident about the right ranking.
Loss curve with a red dot at reward margin -1.3 and loss 1.54, annotated to show high loss for an incorrect ranking prediction.
Incorrect ranking example: when the margin is -1.3, the preference loss rises to 1.54, six times higher, creating strong gradients that push the model to fix the wrong ranking.

This demonstration illustrates that the preference loss is a ranking objective and a calibrated probabilistic one. The gradients it produces reflect both the direction of the error and the model's current confidence, creating a training signal that naturally focuses effort where the model most needs to improve.