v9.1.1
Menu

Reward Model Loss

easyFREEReward ModelingLLM 2: Training & Alignment

Compute the reward model training loss.

Given N preference pairs (chosen reward, rejected reward), the loss is: L=−1N∑i=1Nlog⁡σ(rchoseni−rrejectedi)L = -\frac{1}{N} \sum_{i=1}^{N} \log \sigma(r_{\text{chosen}_i} - r_{\text{rejected}_i})

where σ is the sigmoid function.

Input:

  • Line 1: N (number of pairs)
  • Next N lines: r_chosen r_rejected

Output: The loss value, rounded to 4 decimal places.

Example

Input
2
2.0 1.0
1.5 0.5
Output
0.2899
Reasoning
  • We start with the given input: N=2N = 2, and two pairs of rewards: (2.0,1.0)(2.0, 1.0) and (1.5,0.5)(1.5, 0.5).
  • For each pair, we calculate log⁡σ(rchoseni−rrejectedi)\log \sigma(r_{\text{chosen}_i} - r_{\text{rejected}_i}):
    • For the first pair: log⁡σ(2.0−1.0)=log⁡σ(1.0)\log \sigma(2.0 - 1.0) = \log \sigma(1.0)
    • For the second pair: log⁡σ(1.5−0.5)=log⁡σ(1.0)\log \sigma(1.5 - 0.5) = \log \sigma(1.0)
  • We then compute the average of these values and multiply by −1-1: −12(log⁡σ(1.0)+log⁡σ(1.0))-\frac{1}{2} (\log \sigma(1.0) + \log \sigma(1.0))
  • Since σ(1.0)≈0.7311\sigma(1.0) \approx 0.7311, log⁡σ(1.0)≈−0.2899×2\log \sigma(1.0) \approx -0.2899 \times 2, and thus −12×−0.2899×2=0.2899-\frac{1}{2} \times -0.2899 \times 2 = 0.2899

Constraints

  • 1 <= N <= 100
  • Use log-sigmoid: log(σ(x)) = x - log(1 + exp(x)) for stability when x > 0
  • Round to 4 decimal places

Learn this in

More from LLM 2: Training & Alignment

solution.py

Test results

Sign in to run your code against the test cases.