Reinforcement Learning / Blog Posts / 2026

Direct Policy Optimization (DPO): Reinforcement Learning with Human Feedback (with the maths behind fine-tuning LLMs)

MW
Mohammad Wasil Saleem published on October 1, 2026 • 4 min read
Table of Contents

1. Overview & Context

When we fine-tune a Large Language Model using standard next-token prediction, we teach it grammar, syntax, and basic pattern matching. But raw text generation alone doesn't guarantee that an LLM will be helpful. To truly align a model with human intent, we need a mechanism to reward desirable behavior and discourage toxic or unhelpful outputs.

With Reinforcement Learning with Human Feedback (RLHF) and its optimization algorithm, Direct Policy Optimization (DPO), we can help align the model with human preferences with the help of Reward Modeling.

In the previous blog, Proximal Policy Optimization: Reinforcement Learning with Human Feedback, we explored the complete mathematical and architectural pipeline behind RLHF with PPO.

In this blog, we will explore the complete mathematical and architectural pipeline behind RLHF with DPO, and how it differs from PPO.

2. Why DPO?

As we have already seen in previous blog, we need 4 models for alignemtn in PPO, namely, Reference Model (SFT LLM), Actor (Policy LLM), Reward Model (adapted from SFT), and , Critic Model(Value Function LLM). Training all of these models can be computationally expensive and complex.

DPO simplify this process by directly optimizing the policy model directly on human preferences without the need for a separate reward model or critic LLM.

Direct Policy Optimization (DPO) offers several advantages over traditional Reinforcement Learning with Human Feedback (RLHF) approaches like Proximal Policy Optimization (PPO):

3. Direct Policy Optimization (DPO)

We can rewrite the Reward Model's scoring function directly in terms of the LLM's own token probabilities. Therefore, instead of using a separate reward model to evaluate the response, DPO looks at the LLM itself, and evaluates, how likely is the policy llm to generate a choosen response compared to our reference LLM model, and similarly, how likely is the policy llm to generate a rejected response compared to the reference LLM model.

Therefore, we only need two models: the policy model and the reference model.

Note: In traditional reinforcement learning, a model learns through trial and error via a continuous feedback loop: TODO: NEED TO UPDATE THIS
                        ┌─────┐
            Prompt ───► │ LLM │───► Response ───► DPO Loss Function ───► Parameter Update
                        └─────┘
            

The DPO Loss Function mathematically forces the model to do two things at once:

The standard naming conventions are the same as in the previous blog on PPO:

Quick Nomenclature Note of all the different LLMs used (Total 4):

  • SFT Base (πSFT) / Reference Model: The model fine-tuned on supervised chat conversations. Generally denoted as πSFT or πref. It serves as our baseline anchor.
  • Policy LLM (πθ): The language model trained to generate text, typically denoted as πθ (parameterized by weights θ).

3. Human Preference Data

To train the reward model, we use pairwise human preference data, like we did for reward model training in PPO:

Example Preference Data Structure

{
"prompt": "x: What is 2 + 2?",
"chosen": "y_w: The answer to 2 + 2 is 4.",
"rejected": "y_l: 2 + 2 is 5, but sometimes math is relative."
}

4. Fine-tuning the Policy with DPO

In this section, we will explore how the Policy LLM is fine-tuned using Direct Policy Optimization (DPO) to align it with human preferences.

Here, we combine everything into DPO training step to update LLM weights (θ).

For every prompt in your batch,

        ┌──────────┐
y_w ───►│Policy LLM│ ───► log probabilities
    \  /│  (π_θ)   │
     \/ └──────────┘
     /\  
    /  \┌──────────┐
y_l ───►│ SFT LLM  │ ───► log probabilities
        │ (π_sft)  │
        └──────────┘
                

Policy LLM Architecture

The Policy LLM isn't built from scratch. It is initialized directly from our trained SFT checkpoint (πSFT) without any modifications.

Calculate the Implicit Rewards

$$ \begin{aligned} score_{w} &= \beta \log \frac{\pi_\theta(y_w|x)}{\pi_{SFT}(y_w|x)}, \text{and} \\ score_{l} &= \beta \log \frac{\pi_\theta(y_l|x)}{\pi_{SFT}(y_l|x)} \\ \text{where}, \\ \beta = &\text{ temperature parameter that controls how strictly the model} \\ &\text{sticks to the reference mode. } \end{aligned} $$

It checks how confident the policy model πθ is, about this response compared to the reference model πSFT.

DPO Loss Function

The DPO algorithm uses a simple standard sigmoid cross-entropy loss function to update the policy parameters:

$$ \begin{aligned} L_{DPO}(\theta) &= - log (\sigma (score_{w} - score_{l})) \\ L_{DPO}(\theta) &= - log \bigg[ \big(\sigma \beta \log \frac{\pi_\theta(y_w|x)}{\pi_{SFT}(y_w|x)} - \beta \log \frac{\pi_\theta(y_l|x)}{\pi_{SFT}(y_l|x)} \big) \bigg] \end{aligned} $$

Derivation of the DPO Loss Function

The DPO loss function is derived from the principle of maximizing the likelihood of the chosen response while minimizing the likelihood of the rejected response. The sigmoid function is used to convert the difference in scores into a probability, which is then used in the cross-entropy loss.

$$ \begin{aligned} \nabla_{\theta} L_{DPO}(\theta) &= \nabla_{\theta} (- log (\sigma (score_{w} - score_{l}))) \\ &= \nabla_{\theta} \bigg (- log (\sigma (\beta \log \frac{\pi_\theta(y_w|x)}{\pi_{SFT}(y_w|x)} - \beta \log \frac{\pi_\theta(y_l|x)}{\pi_{SFT}(y_l|x)})) \bigg) \\ \nabla_\theta L_{\text{DPO}}(\theta) &= -\beta \mathbb{E} \left[ \underbrace{\left( 1 - \sigma(score_w - score_l) \right)}_{\text{Weighting factor}} \left( \nabla_\theta \log \pi_\theta(y_w\vert{}x) - \nabla_\theta \log \pi_\theta(y_l\vert{}x) \right) \right] \\ \end{aligned} $$ $$ \text{where,} \nabla_\theta \log \pi_\theta(y_w\vert{}x): \text{Push the weights to increase the probability of the chosen response.} \\ -\nabla_\theta \log \pi_\theta(y_l\vert{}x): \text{Push the weights to decrease the probability of the rejected response.} \\ $$

The Weighting Factor: If the model is already doing a good job ranking chosen over rejected, the sigmoid term approaches 1, making the weighting factor approach 0 (stopping unnecessary updates). If the model gets it wrong, the gradient spikes, forcing a heavy correction.

Mathematical and Numerical Example

Lets say, we pass our prompt and responses through both models, πSFT and πθ, yielding the following sequence log-probabilities log π:

$$ \begin{aligned} \text{Reference Model, } \pi_{SFT}: \\ log \pi_{SFT}(y_w|x) &= -2.0 \\ log \pi_{SFT}(y_l|x) &= -4.0 \\ \text{Active Policy Model, } \pi_\theta: \\ log \pi_\theta(y_w|x) &= -1.5, \text{(model got better at predicting the chosen response)} \\ log \pi_\theta(y_l|x) &= -4.5, \text{(model got worse at predicting the rejected response)} \\ \\ &\text{Let the temperature hyperparameter be, } \beta = 0.1 \\ \\ score_{w} &= \beta \log \frac{\pi_\theta(y_w|x)}{\pi_{SFT}(y_w|x)} = 0.1 * (-1.5 - (-2.0)) = 0.05 \\ score_{l} &= \beta \log \frac{\pi_\theta(y_l|x)}{\pi_{SFT}(y_l|x)} = 0.1 * (-4.5 - (-4.0)) = -0.05 \\ \\ &\text{\textbf{Bradley-Terry DPO Loss:}} \\ L_{DPO}(\theta) &= - log (\sigma (score_{w} - score_{l})) \\ &= - log (\sigma (0.05 - (-0.05))) \\ &= - log (\sigma (0.1)) \approx 0.6446 \\ \\ &\text{\textbf{Gradient of DPO Loss:}} \\ \nabla_\theta L_{\text{DPO}}(\theta) &= -\beta \big[ \left( 1 - \sigma(score_w - score_l) \right) \left[ \nabla_\theta \log \pi_\theta(y_w\vert{}x) - \nabla_\theta \log \pi_\theta(y_l\vert{}x) \right] \big] \\ &= -0.1 * \left( 1 - \sigma(0.1) \right) \left( \nabla_\theta \log \pi_\theta(y_w\vert{}x) - \nabla_\theta \log \pi_\theta(y_l\vert{}x) \right) \\ &= -0.03554 * \left( \nabla_\theta \log \pi_\theta(y_w\vert{}x) - \nabla_\theta \log \pi_\theta(y_l\vert{}x) \right) \end{aligned} $$

If the model gets it right: As σ approaches 1.0 (meaning the model strongly prefers the chosen response), the term (1 - σ) approaches 0, No unnecessary gradient updates.

If the model gets it wrong: The gradient scales up, forcefully tweaking the internal weights (θ) to increase the probability of generating yw while decreasing the probability of generating yl.