Papers · Reinforcement learning and alignment · 2023

Direct Preference Optimization: Your Language Model is Secretly a Reward Model

Rafael Rafailov, Archit Sharma, Eric Mitchell and others

The notation of the DPO paper, explained: its 33 displayed equations, then every symbol it uses, grouped by category and shown the way the paper writes it.

Read the paper on arXiv

Reading this paper

DPO: fine-tune a language model on human preferences directly, with a classification-style loss, instead of training a reward model and running reinforcement learning.

πθ\pi_\theta is the model being trained and πref\pi_{\text{ref}} the frozen starting model. yw≻yl∣xy_w \succ y_l \mid x reads "for prompt xx, the answer ywy_w (the winner) is preferred to yly_l (the loser)". β\beta sets how far πθ\pi_\theta may move from πref\pi_{\text{ref}}, σ\sigma is the logistic sigmoid, and Z(x)Z(x) is a normalizing constant that cancels out, which is the trick that makes the method work.

Read from arXiv's HTML version: 33 displayed equations and 101 pieces of inline math. 40 symbols and constructs from the catalog.

The paper's displayed equations

Open any of them in the decoder for a symbol-by-symbol reading.

  1. (1)
    p∗(y1≻y2∣x)=exp⁡(r∗(x,y1))exp⁡(r∗(x,y1))+exp⁡(r∗(x,y2)).p^{*}(y_{1}\succ y_{2}\mid x)=\frac{\exp\left(r^{*}(x,y_{1})\right)}{\exp\left(r^{*}(x,y_{1})\right)+\exp\left(r^{*}(x,y_{2})\right)}.
    Decode →
  2. (2)
    LR(rϕ,D)=−E(x,yw,yl)∼D[log⁡σ(rϕ(x,yw)−rϕ(x,yl))]\mathcal{L}_{R}(r_{\phi},\mathcal{D})=-\mathbb{E}_{(x,y_{w},y_{l})\sim\mathcal{D}}\bigl[\log\sigma(r_{\phi}(x,y_{w})-r_{\phi}(x,y_{l}))\bigr]
    Decode →
  3. (3)
    max⁡πθEx∼D,y∼πθ(y∣x)[rϕ(x,y)]−βDKL[πθ(y∣x)∣∣πref(y∣x)],\max_{\pi_{\theta}}\mathbb{E}_{x\sim\mathcal{D},y\sim\pi_{\theta}(y\mid x)}\bigl[r_{\phi}(x,y)\bigr]-\beta\mathbb{D}_{\textrm{KL}}\bigl[\pi_{\theta}(y\mid x)\mid\mid\pi_{\text{ref}}(y\mid x)\bigr],
    Decode →
  4. (4)
    πr(y∣x)=1Z(x)πref(y∣x)exp⁡(1βr(x,y)),\pi_{r}(y\mid x)=\frac{1}{Z(x)}\pi_{\text{ref}}(y\mid x)\exp\left(\frac{1}{\beta}r(x,y)\right),
    Decode →
  5. (5)
    r(x,y)=βlog⁡πr(y∣x)πref(y∣x)+βlog⁡Z(x).r(x,y)=\beta\log\frac{\pi_{r}(y\mid x)}{\pi_{\text{ref}}(y\mid x)}+\beta\log Z(x).
    Decode →
  6. (6)
    p∗(y1≻y2∣x)=11+exp⁡(βlog⁡π∗(y2∣x)πref(y2∣x)−βlog⁡π∗(y1∣x)πref(y1∣x))p^{*}(y_{1}\succ y_{2}\mid x)=\frac{1}{1+\exp\left(\beta\log\frac{\pi^{*}(y_{2}\mid x)}{\pi_{\text{ref}}(y_{2}\mid x)}-\beta\log\frac{\pi^{*}(y_{1}\mid x)}{\pi_{\text{ref}}(y_{1}\mid x)}\right)}
    Decode →
  7. (7)
    LDPO(πθ;πref)=−E(x,yw,yl)∼D[log⁡σ(βlog⁡πθ(yw∣x)πref(yw∣x)−βlog⁡πθ(yl∣x)πref(yl∣x))].\mathcal{L}_{\text{DPO}}(\pi_{\theta};\pi_{\text{ref}})=-\mathbb{E}_{(x,y_{w},y_{l})\sim\mathcal{D}}\left[\log\sigma\left(\beta\log\frac{\pi_{\theta}(y_{w}\mid x)}{\pi_{\text{ref}}(y_{w}\mid x)}-\beta\log\frac{\pi_{\theta}(y_{l}\mid x)}{\pi_{\text{ref}}(y_{l}\mid x)}\right)\right].
    Decode →
  8. ∇θLDPO(πθ;πref)=−βE(x,yw,yl)∼D[σ(r^θ(x,yl)−r^θ(x,yw))⏟higher weight when reward estimate is wrong[∇θlog⁡π(yw∣x)⏟increase likelihood of yw−∇θlog⁡π(yl∣x)⏟decrease likelihood of yl]],\nabla_{\theta}\mathcal{L}_{\text{DPO}}(\pi_{\theta};\pi_{\text{ref}})=\\ -\beta\mathbb{E}_{(x,y_{w},y_{l})\sim\mathcal{D}}\bigg[\underbrace{\sigma(\hat{r}_{\theta}(x,y_{l})-\hat{r}_{\theta}(x,y_{w}))}_{\text{higher weight when reward estimate is wrong}}\bigg[\underbrace{\nabla_{\theta}\log\pi(y_{w}\mid x)}_{\text{increase likelihood of $y_{w}$}}-\underbrace{\nabla_{\theta}\log\pi(y_{l}\mid x)}_{\text{decrease likelihood of $y_{l}$}}\bigg]\bigg],
    Decode →
  9. (8)
    f(r;πref,β)(x,y)=r(x,y)−βlog⁡∑yπref(y∣x)exp⁡(1βr(x,y))f(r;\pi_{\text{ref}},\beta)(x,y)=r(x,y)-\beta\log\sum_{y}\pi_{\text{ref}}(y\mid x)\exp\left(\frac{1}{\beta}r(x,y)\right)
    Decode →
  10. (9)
    ∑yπref(y∣x)exp⁡(1βr(x,y))⏟=π(y∣x), using Thm. (?) reparam.=1,\sum_{y}\underbrace{\pi_{\text{ref}}(y\mid x)\exp\left(\frac{1}{\beta}r(x,y)\right)}_{=\pi(y\mid x)\text{, using Thm.~(?) reparam.}}=1,
    Decode →
  11. (10)
    max⁡πθEπθ(y∣x)[rϕ(x,y)−βlog⁡∑yπref(y∣x)exp⁡(1βrϕ(x,y))⏟f(rϕ,πref,β)−βlog⁡πθ(y∣x)πref(y∣x)⏟KL]\max_{\pi_{\theta}}\mathbb{E}_{\pi_{\theta}(y\mid x)}\bigg[\underbrace{r_{\phi}(x,y)-\beta\log\sum_{y}\pi_{\text{ref}}(y\mid x)\exp\left(\frac{1}{\beta}r_{\phi}(x,y)\right)}_{f(r_{\phi},\pi_{\text{ref}},\beta)}-\underbrace{\beta\log\frac{\pi_{\theta}(y\mid x)}{\pi_{\text{ref}}(y\mid x)}}_{\text{KL}}\bigg]
    Decode →
  12. (11)
    max⁡πEx∼D,y∼π[r(x,y)]−βDKL[π(y∣x)∣∣πref(y∣x)]\max_{\pi}\mathbb{E}_{x\sim\mathcal{D},y\sim\pi}\bigl[r(x,y)\bigr]-\beta\mathbb{D}_{\textrm{KL}}\bigl[\pi(y|x)||\pi_{\text{ref}}(y|x)\bigr]
    Decode →

Famous equations in this paper

Symbols, by category

Most used first. The examples are written as the paper writes them.

Notation conventions

  1. yy, xx, rr

    Italic letters (variables) 818 uses

    A plain italic letter is a variable; conventions say what kind (x input, y target, W weights…).

  2. y1y_{1}, y2y_{2}, LR\mathcal{L}_{R}

    Subscript (index or label) 321 uses

    A subscript picks an element (xᵢ), a time step (h_t), a named copy (W_q), or the parameters (p_θ).

  3. p∗p^{*}, r∗r^{*}, π∗\pi^{*}

    Star (optimal) 49 uses

    As a superscript, the optimal value: θ* is the best θ. Between two terms, convolution.

  4. D\mathcal{D}

    Dataset (𝒟) 24 uses

    The dataset: usually N input-label pairs.

  5. x(i)x^{(i)}, yw(i)y_{w}^{(i)}, yl(i)y_{l}^{(i)}

    Superscript in parentheses 13 uses

    An index in a superscript: x⁽ⁱ⁾ is the i-th training example, W⁽ˡ⁾ the weights of layer l. Not a power.

  6. πSFT\pi^{\text{SFT}}, {x(i),yw(i),yl)(i)}i=1N\{x^{(i)},y_{w}^{(i)},y_{l})^{(i)}\}_{i=1}^{N}

    Superscript (power or label) 12 uses

    Usually a power (x² = x·x), but a capital or a word up there is often just a label (W^Q).

  7. L\mathcal{L}

    Loss (ℒ) 10 uses

    The loss: the number training tries to make small. Also written L, J(θ) or ℓ.

  8. …\ldots

    Dots (ellipsis) 8 uses

    “And so on”: continue the pattern.

  9. Underbrace and overbrace 6 uses

    A horizontal brace under or over part of a formula labels that part. It adds no math.

  10. r^\hat{r}

    Hat (estimate) 5 uses

    A hat marks an estimate or prediction: ŷ is the model’s guess for y.

Greek letters

  1. π\pi

    Pi 225 uses

    A policy π(a | s) in reinforcement learning, or the constant 3.14159…

  2. β\beta

    Beta 99 uses

    Usually a decay rate (Adam’s β₁, β₂), a shift parameter in normalization, or a weight on a loss term.

  3. θ\theta

    Theta 50 uses

    The model’s parameters: all of its weights and biases, as one collection.

  4. τ\tau

    Tau 26 uses

    A temperature that sharpens or flattens a softmax; in RL, a trajectory.

  5. ϕ\phi

    Phi 15 uses

    The parameters of a second model (often the encoder q_φ), or a feature map φ(x).

  6. σ\sigma

    Sigma (lowercase) 15 uses

    Either the sigmoid function σ(z) = 1/(1+e⁻ᶻ) or a standard deviation. The context decides.

  7. α\alpha

    Alpha 2 uses

    Often a learning rate or step size; also attention weights αᵢⱼ or a mixing weight.

Vectors and matrices

  1. KK

    Q, K, V (query, key, value) 27 uses

    In attention: queries Q, keys K and values V, each a matrix with one row per token.

  2. ≻\succ

    Succeeds / PSD order 6 uses

    For matrices, A ⪰ 0 means positive semidefinite; for outputs, y_w ≻ y_l means y_w is preferred.

  3. (⋅)(\cdot), ⋅\cdot

    Centre dot 2 uses

    A dot product between vectors, plain multiplication between numbers, or an empty slot f(·).

Calculus and gradients

  1. σ′\sigma^{\prime}, r′r^{\prime}, πr′\pi_{r}^{\prime}

    Prime 28 uses

    Either a derivative, f′(x), or simply "another one": x′ is a second sample, s′ the next state.

  2. ∇θ\nabla_{\theta}

    Nabla (gradient) 10 uses

    The gradient: the vector of partial derivatives. ∇_θ L is how the loss changes with each parameter.

Probability and statistics

  1. ∣\mid, ∣|

    Given (conditional bar) 178 uses

    “Given”: p(y | x) is the probability of y given that x is known.

  2. log⁡\log

    Logarithm 69 uses

    A logarithm, almost always the natural log (base e) in ML papers.

  3. exp⁡\exp

    Exponential 50 uses

    The exponential function eˣ, with e ≈ 2.718. Always positive.

  4. ∼\sim

    Distributed as (tilde) 26 uses

    “Is drawn from” or “is distributed as”: x ~ p means x is a sample from the distribution p.

  5. E(x,yw,yl)∼D\mathbb{E}_{(x,y_{w},y_{l})\sim\mathcal{D}}, Eπθ(y∣x)\mathbb{E}_{\pi_{\theta}(y\mid x)}, Ex∼D,y∼π\mathbb{E}_{x\sim\mathcal{D},y\sim\pi}

    Expectation 22 uses

    The expected value: the average of what is inside, weighted by the probabilities in the subscript.

  6. pp

    Probability p(x) 16 uses

    A probability or probability density; p is usually the true or model distribution, q an approximation.

  7. KL\textrm{KL}, KL\text{KL}, ∣∣||

    KL divergence 8 uses

    How different distribution q is from p. Zero when they match; not symmetric.

  8. ;;

    Semicolon (parameterized by) 8 uses

    In p(x; θ), the semicolon separates the variable x from fixed parameters θ.

Functions and operators

  1. 1Z(x)\frac{1}{Z(x)}, 1β\frac{1}{\beta}, π(y∣x)πref(y∣x)\frac{\pi(y|x)}{\pi_{\text{ref}}(y|x)}

    Fraction 89 uses

    Division: the top divided by the bottom.

  2. ∑y\sum_{y}, ∑j=kK\sum_{j=k}^{K}

    Summation 18 uses

    Add up the expression for every value of the index, from the bottom limit to the top.

  3. max⁡πθ\max_{\pi_{\theta}}, max⁡π\max_{\pi}, min⁡π\min_{\pi}

    Max and min 9 uses

    The largest (or smallest) value, over the arguments or over the range in the subscript.

  4. ∏k=1K\prod_{k=1}^{K}

    Product 7 uses

    Multiply the expression for every value of the index.

  5. ::, →\to

    Arrow (maps to) 2 uses

    Maps to (f: X → Y), tends to (x → 0), or implies (⇒).

  6. arg max⁡π\argmax_{\pi}

    Argmax 1 use

    The input that makes the function largest, not the largest value itself.

Sets, shapes and definitions

  1. {x(i),yw(i),yl(i)}\{x^{(i)},y_{w}^{(i)},y_{l}^{(i)}\bigr\}, {x(i),yw(i),yl)(i)}\{x^{(i)},y_{w}^{(i)},y_{l})^{(i)}\}, {3,6,9,12}\{3,6,9,12\}

    Set braces 8 uses

    A set: a collection of items. {x | condition} is every x that satisfies the condition.

  2. >>, ≥\geq

    Inequalities 7 uses

    Less than / greater than (or equal). ≪ means “much smaller than”.

  3. ∈\in

    Element of 5 uses

    “Is in” or “belongs to”: x ∈ S says x is a member of the set S.

  4. ≠\neq

    Not equal 1 use

    The two sides are different.

1 thing the catalog has no entry for yet
  • D\mathbb{D} D 5

Formulas from arXiv's HTML version of the paper, © their authors; the explanations are this site's. Title and authors as arXiv lists them.

More reinforcement learning and alignment papers