PIXELBANKv9.1.0
Menu

Extract the implicit reward from a DPO-trained model.

In DPO, the implicit reward is: r(x,y)=βlog⁡πθ(y∣x)πref(y∣x)+βlog⁡Z(x)r(x, y) = \beta \log \frac{\pi_\theta(y|x)}{\pi_{ref}(y|x)} + \beta \log Z(x)

Since Z(x) is the same for all y given x, we can compare rewards by: r(x,y)∝βlog⁡πθ(y∣x)πref(y∣x)r(x, y) \propto \beta \log \frac{\pi_\theta(y|x)}{\pi_{ref}(y|x)}

Given log-probabilities from policy and reference model for N responses to the same prompt, compute the implicit rewards and rank responses.

Input:

  • Line 1: beta
  • Line 2: N
  • Next N lines: log_prob_policy log_prob_ref response_label

Output: Response labels ranked by implicit reward (highest first), one per line.

Example:

Input:
0.1
3
-2.0 -3.0 A
-1.5 -1.0 B
-3.0 -4.0 C
Output:
C
A
B
Reasoning:
  • First, we read the input values: β=0.1\beta = 0.1, N=3N = 3, and the log-probabilities for each response.
  • Then, we calculate the implicit reward for each response using the formula: r(x,y)∝βlog⁡πθ(y∣x)πref(y∣x)r(x, y) \propto \beta \log \frac{\pi_\theta(y|x)}{\pi_{ref}(y|x)}. For the given inputs, the calculations are:
    • For A: r(A)∝0.1log⁡−2.0−3.0r(A) \propto 0.1 \log \frac{-2.0}{-3.0}
    • For B: r(B)∝0.1log⁡−1.5−1.0r(B) \propto 0.1 \log \frac{-1.5}{-1.0}
    • For C: r(C)∝0.1log⁡−3.0−4.0r(C) \propto 0.1 \log \frac{-3.0}{-4.0}
  • Next, we compute the actual values:
    • For A: r(A)∝0.1log⁡−2.0−3.0=0.1log⁡0.6667≈0.1⋅−0.4055≈−0.0406r(A) \propto 0.1 \log \frac{-2.0}{-3.0} = 0.1 \log 0.6667 \approx 0.1 \cdot -0.4055 \approx -0.0406
    • For B: r(B)∝0.1log⁡−1.5−1.0=0.1log⁡1.5≈0.1⋅0.4055≈0.0406r(B) \propto 0.1 \log \frac{-1.5}{-1.0} = 0.1 \log 1.5 \approx 0.1 \cdot 0.4055 \approx 0.0406
    • For C: r(C)∝0.1log⁡−3.0−4.0=0.1log⁡0.75≈0.1⋅−0.2877≈−0.0288r(C) \propto 0.1 \log \frac{-3.0}{-4.0} = 0.1 \log 0.75 \approx 0.1 \cdot -0.2877 \approx -0.0288 However, considering the proportionality and the actual log values, the correct order should be based on 0.1log⁡πθ(y∣x)πref(y∣x)0.1 \log \frac{\pi_\theta(y|x)}{\pi_{ref}(y|x)} which translates to 0.1⋅(log⁡probpolicy−log⁡probref)0.1 \cdot (\log_prob_policy - \log_prob_ref) for each.
    • For A: 0.1⋅(−2.0−(−3.0))=0.1⋅1=0.10.1 \cdot (-2.0 - (-3.0)) = 0.1 \cdot 1 = 0.1
    • For B: $0.1 \cdot (-1.5 - (-1.0

Constraints:

  • 0.01 <= beta <= 1.0
  • 1 <= N <= 20
  • Break ties by original order
🔒

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.
Implicit Reward from DPO - Medium | PixelBank