Classic RLHF trains a reward model and then runs reinforcement learning against it while a KL penalty keeps the policy near a reference model. Direct Preference Optimization observes that the optimum of that KL-regularized objective has a closed form: the reward of a response is beta times the log-ratio between the policy and the reference. Substituting that reward into the Bradley-Terry likelihood removes the reward model and the reinforcement learning loop. What remains is a logistic loss on the difference of two log-ratios, trained directly on preference pairs with ordinary gradient descent.
Implement dpo_implicit_reward and dpo_loss.
n: pi_chosen, pi_rejected from the policy and ref_chosen, ref_rejected from the frozen reference.dpo_implicit_reward(pi, ref, beta) is beta * (pi - ref).dpo_loss(pi_chosen, pi_rejected, ref_chosen, ref_rejected, beta) is the mean of -log sigmoid(r_chosen - r_rejected) where each reward is the implicit reward. Use np.logaddexp(0, -d).beta > 0. Return Python floats.Compute the two implicit rewards, take their difference and reuse the stable logistic loss from the reward-model question.
If the policy equals the reference both rewards are zero, so the loss must be ln 2.
No results yet
Run your code to check it against the examples, or submit to run every test.