PIXELBANKv9.1.0
Menu

Compute the Direct Preference Optimization (DPO) loss.

DPO loss for a single preference pair: LDPO=−log⁡σ(β⋅(log⁡πθ(yw∣x)πref(yw∣x)−log⁡πθ(yl∣x)πref(yl∣x)))L_{DPO} = -\log \sigma(\beta \cdot (\log \frac{\pi_\theta(y_w|x)}{\pi_{ref}(y_w|x)} - \log \frac{\pi_\theta(y_l|x)}{\pi_{ref}(y_l|x)}))

where y_w is the preferred (winning) response and y_l is the rejected (losing) response.

Input:

  • Line 1: beta (temperature parameter)
  • Line 2: N (number of preference pairs)
  • Next N lines: log_ratio_w log_ratio_l (log probability ratios for chosen and rejected)

where log_ratio = log(π_θ(y|x) / π_ref(y|x))

Output: Average DPO loss, rounded to 4 decimal places.

Example:

Input:
0.1
2
1.0 -1.0
0.5 0.0
Output:
0.4887
Reasoning:
  • The temperature parameter β\beta is set to 0.10.1 and the number of preference pairs NN is set to 22.
  • For each preference pair, we calculate the DPO loss using the given formula:
    • For the first pair, LDPO=−log⁡σ(0.1⋅(1.0−(−1.0)))=−log⁡σ(0.2)L_{DPO} = -\log \sigma(0.1 \cdot (1.0 - (-1.0))) = -\log \sigma(0.2)
    • For the second pair, LDPO=−log⁡σ(0.1⋅(0.5−0.0))=−log⁡σ(0.05)L_{DPO} = -\log \sigma(0.1 \cdot (0.5 - 0.0)) = -\log \sigma(0.05)
  • We compute the values: −log⁡σ(0.2)≈0.5988-\log \sigma(0.2) \approx 0.5988 and −log⁡σ(0.05)≈0.3785-\log \sigma(0.05) \approx 0.3785
  • The average DPO loss is then calculated as (0.5988+0.3785)/2≈0.4887(0.5988 + 0.3785) / 2 \approx 0.4887, which is rounded to 44 decimal places to obtain the final output.

Constraints:

  • 0.01 <= beta <= 1.0
  • 1 <= N <= 50
  • Round to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
DPO Loss - Medium | PixelBank