AI Hero Club

LLM Post-training · Lesson 6

Marked Complete by You0 / 8

Saved in this browser only. Marking a lesson complete does not mean the project has passed verification.

Lesson Outline 9 subsections
Course Contents 8 lessons
Full course plan.
LLM Post-training / Learning Plan ↗

LESSON 06 / 8Suggested study 120 minutes

DPO: From Reward Substitution to Response Log-ratios

Why can increasing the winner-to-loser probability ratio still reduce the winner’s absolute probability?

3 Questions for This Lesson
  • Reverse KL 's best strategy to reward and replace BT, and eliminate the hints from the equations.
  • DPO gradient to distinguish four candidate linear eliminations from complete decoder inverse chain
  • The manual reference is the reverse of the absolute probability decline in Winner, defining the beta and length to match
Course Project ZIP ↓

Task, Prerequisites, and Derivation Scope

This chapter links the best strategies of chapters IV, BT, and V, KL, to a direct preference for the optimization of the DPO. Readers should be able to recreate the poor sigmoid and the index tilting that is being supported under the reference. We will not describe “No explicit reward network” as without statistical assumptions; the DPO still uses preferred models, fixed references and a set of comparisons. The project has two deliverables: the eight parameter strategy provides a poor score and a limited margin across parameters; and the complete microdecoder uses the true prompt/response path probabilities training all model parameters. Both shared a definition of loss, but the model was different from the numeric evaluation space.

The reference strategy is q, the strategy is pi, β\beta Strictly correct, adopting chapter V τ\tau = β\beta The answer-by-answer reward can be written by the best strategy formula. β\beta Multiply log⁡(π∗/q)\log(\pi^*/q) Plus. β\beta log⁡Z(x)\log Z(x). When the same x compares w with l, public β\beta log⁡Z\log Z It is a variable relationship between optimal distribution and corresponding incentives; replaces it with a limited parameter strategy. π∗\pi^* Post-optimization preferences appear to be modelling and estimation steps, and it cannot be said that the intermediate parameters of arbitrary training have been best served by the original incentive targets.

r(x,y)=βlog⁡π∗(y∣x)q(y∣x)+βlog⁡Z(x),m=β[log⁡πθ(yw∣x)q(yw∣x)−log⁡πθ(yl∣x)q(yl∣x)].r(x,y)=\beta\log\frac{\pi^*(y|x)}{q(y|x)}+\beta\log Z(x),\quad m=\beta\left[\log\frac{\pi_\theta(y_w|x)}{q(y_w|x)}-\log\frac{\pi_\theta(y_l|x)}{q(y_l|x)}\right].
Public logZs are only offset by hint winner/loser, reference q is fixed and can be rated for the event.

Substituting BT and Defining a Computable Loss

The BT wins with the sigmoid reward. β\beta By “winner's logpi minus logq” and then “loser’s logpi minus logq.” The negative probability of each observation is softplus(−m). On average, for a fixed comparison set, it is DPO losses. Here q must score with Pi on the same tip, the same answer text, the same tokenizer and the boundary rules; there is no common event meaning to reduce logprob in two models in different formats. Reference is not another random string, nor is there a global constant.

ℓDPO(θ)=−log⁡σ(m)=softplus⁡(−m).\ell_{DPO}(\theta)=-\log\sigma(m)=\operatorname{softplus}(-m).
m As defined above, preference is preferred to Winner and Loser under the same hint.

If log⁡πw\log\pi_w and log⁡πl\log\pi_l For intermediate variables, the loss to the former is β\beta Multiply (sigmoid(m) minus one), for which the opposite number is given; reference entry does not participate in guidance. θ\theta. The weight of a low or misdirected symbol is already high and low, which is a dynamic coefficient that logistic seems to give itself; it cannot be replaced with the words " Forever add the probability of Winner to the fixed amount". When all pairs have common miniature parameters, the gradient contribution needs to be added, not the optimal response that they do not affect each other separately.

∇θℓ=β(σ(m)−1)[∇θlog⁡πθ(yw∣x)−∇θlog⁡πθ(yl∣x)].\nabla_\theta\ell=\beta(\sigma(m)-1)\left[\nabla_\theta\log\pi_\theta(y_w|x)-\nabla_\theta\log\pi_\theta(y_l|x)\right].
The full response gradient is spread along all response locations; it is not equal to only updating the final output layer.

Example 1: A Policy Equal to the Reference Still Has a Learning Signal

Makes the policy starting point to be fully copied, and the relative log ratio is zero, m=0, loss log2, while sigmoid(m) is reduced by one in two if. β\beta = 0.5 The loss of the Winner logprob guide is - 0.25 For Loser is + 0.25. The loss is not zero and the gradient is not zero; the DPO changes the relative preference from the reference starting point. “The training won't move.” Mixes the function values with the lead numbers.

In a limited linear strategy, the logpi difference for the same x is equal to the logit difference because softmax public unicoded items are consumed. Take x=2, Winner is one, Loser is zero, and the gradient updates only the bias and slope of these two actions: Winner [−0.25,−0.5], Loser [0.25,0.5], and the rest zero. The learning rate is 0.2 hours when Winner parameters increase [0.05,0.1], Loser decreases the same amount. The independent inspection is the center of eight parameters, and the independent check is the new margin is positive and loss drops. A zero gradient for some of the parameters is the result of the formula here, not the break chain.

Complete decoder cannot directly replicate the simplification of “Only two output lines”. Different answers have different contexts after the first token, and subsequent softmax reclassifications are no longer shared; the gradients sought by logprob go through embedding, attention, location, forward feed and output head. The project sequences provide two complete answers, one for response+EOS and one for ZXBXZ, and then for automatic microdistribution of loss, checking all seventeen parameters with limited gradients and actual updates. This realization will be carried over to API under Chapter VII.

Example 2: Better Relative Preference Does Not Guarantee Higher Winner Probability

Sets the reference for Winner and Loser to 0.4 and 0. 2, respectively. The current strategy is 0.2 and 0. 02, with the rest of the quality distributed to other answers. Winner compares the reference rate to 0. 5, while Loser is 0. 1, with the difference log ratio to log5; β\beta = about 0 804719 at 0.5, BT predicts a preference of about 0 690983, loss about 0. 369640. Compared to the reference log2, DPO improved, but the absolute probability for Winner decreased from 0.4 to 0 2.

This example is not an anomaly: the DPO data bind Winner to the reference correction of Loser, without fixing the quality of all other answers separately. If Loser falls faster, margins can improve, even if Winner drops. The independent check is to print two absolute probability, two log comparison, and eventually margin, one that declares all desired behaviour to be increased by looking only at the square. In open text, failure to compare answers may absorb probabilities makes loss preferable to independent generation evaluation necessary to be preserved together.

Beta and Optimization: Why Monotonicity Is Not Guaranteed

β\beta Connecting the incentive scale to the price of deviation from reference in the deduction; it also zooms in margin and gradient in the realization of loss. β\beta More robust KL costs, more recent strategy references; but practical training is limited step-to-step data, changing β\beta At the same time, it changes the gradient scale and saturation speed. KL is not always measured. β\beta Strictly single. Items are limited reference columns β\beta Validation results for 0.1, 0.5, 2 do not use small grids as a rule. Sequence layout is fixed β\beta 0.1, don't test it.

β\beta At zero in margin and zero in the strategy gradient; therefore, implementation should be rejected β\beta, cannot be understood as “Zero.” β\beta Zero automatically tracks the reward indefinitely. The real “NoneKLIncentive maximizes” limit is not a direct zero-in-the-code factor for training dynamics with the DPO parameterization. Another extreme is β\beta Largely, a small amount of logs saturated the sigmoid over changes and were more sensitive to floating point stability and learning rates; the project used softplus to avoid direct calculation of negative logsigmoids.

Response Probability Must Cover the Complete Event

In response to self-return, logpi(y|x) is equal to the sum of each token in response and EOS log probability, and the hint totoken is not counted as a condition. The length of Winner and Loser are still aggregated because they are the probability of full string events. If the token average is changed, the score will be redefined to no longer the same full answer probability as the original DPO derived; this variant can be studied, but the name must be changed and the target stated, and the exact theoretical conclusion cannot continue to be quoted after the length has been removed.

The total logprob naturally changes in length, but the correction of reference offsets the long effects common to some of the two models and does not guarantee that all preferred data length deviations will be eliminated. For example, the SFT model contains long answers, the DPO prefers to be short, and EOS behavior will be influenced by a direct gradient. The sequence items strictly retain EOS, first answer totoken and different lengths mask, and will report separately on the correctness rate for each NLL answer; three are not the same target and will not be in conflict with a “Combined score”.

The limited reference strategy is fully standardized in four actions, allowing the full distribution of KL to be accurately calculated; decoder can calculate a valid probability for a given string path with a limited length, but it cannot list all the text. In theory, an open path at the end of the budget generation can be registered as abort result, closing the total quality of the limited tree creation probability; this project does not seek peace with this huge tree. The re-entry of the four fixed candidates is used only for candidate diagnostics, and cannot claim to have measured the entire text KL or satisfy some global KL constraint.

The Fixed Reference Is Part of the Objective

Freezing the reference requires three things: the number snapshots are not changed, the reference gradient maps are not built forward, and the model is in a determined evaluation mode. No_grad alone does not prevent its parameter object from being updated elsewhere; there is no event freeze; only copying the variable name may still point to the same variable list or module. The project limited strategy is read in no_grad, with all reference parameters since the serial version of Deepcopy readquires_grad for False and eval, while the DPO is reading in n_grad and comparing the complete state before and after training.

If each step of reference follows the current strategy, even if every time detach, margin returns zero, the log loss will remain log2, but the gradient may still drive the strategy, because only the guides for the reference branch are blocked; if both branches are linked to the same calculation chart, the gradient may be zero. Both errors can be misjudged by Loss alone. Independent checks should also assert object identity, parameters freeze, and reference to Hash and strategy updates, rather than just checking whether a single mark is limited.

BUILD THE MECHANISM

Achieve full response rating and DPO

The path logprob is requested by the mask of response+EOS, and the DPO loss is achieved by fixed reference. Download this full course, running in the project root directory; edit only learner/starter.py, with a tectonic framework provided by the project.

Input and Output Prompt and padding are not counted, and the answer path is aggregated and not averaged by length.
beta>0; path fraction for reference detach, policy winner/loser reserves gradient.

Run in the Project DirectoryStudy tasks to be completed
# response_scores
def response_scores(logits,targets,mask):
    raise NotImplementedError('dpo')

# dpo_loss
def dpo_loss(policy_w,policy_l,reference_w,reference_l,beta):
    raise NotImplementedError('dpo')
Hint 1

Write the two different answers into probabilities and then get a log.

Hint 2

margin=beta*((policy_w-policy_l)-(ref_w-ref_l))。

Hint 3

Margin is 0:00 loss=log2, Winner negative, Loser positive; gradient should not be received by reference.

Independent inspection .venv/bin/python -m learner.check --stage dpo --output runs/learner-dpo.json

Migration test The EOS probability changes separately and a token is added to Prompt; the former affects the full probability of answering, while the latter does not re-evaluate the fixed response logits.

Reference Implementation and Explanation
Run in the Project DirectoryReference achieved
# response_scores
def response_scores(logits,targets,mask):
 if logits.ndim!=3 or targets.shape!=logits.shape[:2] or mask.shape!=targets.shape:raise ValueError('shape mismatch')
 if not torch.isfinite(logits).all():raise ValueError('finite logits required')
 safe=targets.masked_fill(~mask,0);return (logits.log_softmax(-1).gather(-1,safe[...,None]).squeeze(-1)*mask).sum(-1)

# dpo_loss
def dpo_loss(policy_w,policy_l,reference_w,reference_l,beta):
 if not beta>0:raise ValueError('positive beta')
 margin=beta*((policy_w-policy_l)-(reference_w.detach()-reference_l.detach()));return torch.nn.functional.softplus(-margin).mean()

This Lesson's Project Contribution

This chapter delivers DPO losses, full gradients and two calculations that reveal misunderstandings. β\beta 0.5, learning rate 0.2, with a fixed SFT snapshot as reference; the sequence version uses 60 steps SFT and 40 steps DPO. β\beta 0.1 calculates the real answer path with a full parameter decoder. The two budgets and models are different and cannot mix their numbers into the same performance curves. The next chapter links mask, logprob, freeze and state preservation to a real implementable training project and retains all format migration failures.

The correction of the full answer ratio cannot be omitted. log⁡πw\log\pi_w Less log⁡πl\log\pi_l Again, sigmoid is optimized by another preferred classification target; it does not maintain a relative fixed reference incentive variable. Even if the training of Pair has increased its accuracy, it cannot be interpreted in the same way. The project also saves two points for policy and reference, and the test strategy is equal to zero for all margins at the time of reference, and detects misuse, inconsistencies or answers mask.

The theoretical preference model assumes that the observation success rate is given by a sigmoid with a bad reward, and that the DPO inherits the same mismatch if the real data contains loops or labelling rules that are missing from the context. Saves the obvious BT for not eliminating the BT's representational hypothesis. Limited data may also be completely distinguishable in parameter space, and continued optimization will increase the margin, reduce its losses, without ensuring that other response quality is controlled.

The only reference for guidance is that of a constant. β\beta It's still a fixed hyper-parameter, and it's not inverted. β\beta, new targets are needed, otherwise models may simply reduce training losses by changing scales instead of changing desired behaviour. β\beta and steps are not automatically reconciled in training. β\beta It is not a contradiction, but two separate experiments; their absolute losas values cannot be directly horizontally superior.

The average offline pair also has sample weights. Winner of a tip compares three times with three Loser, not the same feedback from three independent users; these comparisons share Winner and context and are statistically relevant. The current training uses 18 pair rights as an empirical objective, and tests only report this set of designated relationships, not as an accurate confidence range for independent samples. To estimate overall human preferences, additional sampling and labelling models are required, and cannot be replaced by a synthesizing rule in this course.

Reverse transmission to EOS is particularly important: when short answers are better than long answers, the probability of lifting the end may be the primary path for a model to satisfy preference. If EOS is missing, the model claims to rate the full answer, the model may learn to start correctly but not stop; if all EOS are deleted as padding, both DPO and SFT will miss the end event. Chapter VII validates the EOS item by item at the level of the effective target member, not just whether the string prefix is correct.

Standalone ProgramStandard library independent validation: the reference starting gradient is opposed to absolute probability decline.
import math
softplus=lambda t:max(t,0)+math.log1p(math.exp(-abs(t)))
sigmoid=lambda t:1/(1+math.exp(-t))
beta=.5;q_w,q_l=.4,.2;p_w,p_l=.2,.02
m=beta*((math.log(p_w)-math.log(q_w))-(math.log(p_l)-math.log(q_l)))
assert p_w<q_w and m>0 and softplus(-m)<math.log(2)
print('winner fell',q_w,'to',p_w,'margin',m,'preference probability',sigmoid(m),'loss',softplus(-m))
grad_w=-beta/2;grad_l=beta/2
assert grad_w<0 and grad_l>0
print('at reference loss',math.log(2),'winner/loser logprob gradients',grad_w,grad_l)

TRY IT YOURSELF

Practice I: Public Constants Dissipate

If Winner and Loser come from different x, can they still get rid of beta logZ? Why?

Hint 1
First, we will write about the unicoded entries in the respective awards by Winner and Loser.
Hint 2
The two hints are different, and Z's own variables are different; check whether the “Common Constant Dissipation” premise is still valid.
Compare with the Solution
No. The two hints have their own unicoded constant Z(xw) and Z(xl), which generally differ from the one with the difference between the two. The DPO ' s comparative model defines the same hint condition, and the error of the integration is not only a data format problem, but also undermines the key eliminations in the reasoning.

TRY IT YOURSELF

Practice II: Average length variant

Winner has 2 answers and Loser has 10. Why can’t it be possible to divide the logprob by length instead of using the original DPO theory?

Hint 1
The probability of responding to the event comes from a combination of probabilities of conditions, and what kind of return does the log correspond to?
Hint 2
The total logprob of two different lengths is listed alongside the average length, checking whether the length is different from the original margin weight of the gradient.
Compare with the Solution
The original is a complete event probability, and the serial logs are aggregated. It is not the same probability of a full answer, except when the length becomes geometrically average token probability; two articles with different lengths change the weight of margin and gradient. It can be studied as a new rating variable, but it needs to be independently defined and validated and cannot be described as fully equivalent to the original loss.

TRY IT YOURSELF

Practice III: Reference drifting faults

If each step is to replace reference with the current strategy and detach, does Loss always log2 prove that the training has not been updated? How do you accept it?

Hint 1
Function values are fixed at a point, which does not mean that the point guides the branch of the strategy to zero.
Hint 2
Compared to the two branches, using reference as a constant deetach guide; check for poor policy parameters and separate identity of reference.
Compare with the Solution
No proof. Margin is zero at every step, but the guide to the branch of strategy is still positive and negative beta/2, and the detach blockback reference number may be continuously updated. The difference between the background and the reference to the independent Hash should be checked; the correct realization of the value of reference is fixed, no gradient, and the policy independently changes. If the shared map of the two branches does not have detach, it may be zero, another failure.

CHECK YOUR UNDERSTANDING

Check Your Understanding

Answer first, then open the explanation to check your reasoning. These questions help reveal gaps in understanding; independent implementation and transfer require separate evaluation.

1. What's the DPO missing?

Show ExplanationThe BT assumption and the fixed reference still exist.
2. The strategy is equal to DPO losses and gradients at the time of reference?

Show Explanationfunction value is different from the lead number, and the reference is the training starting point.
3. Is it true that Margin has an absolute chance of a rise in Winner?

Show ExplanationIt binds the relative reference differential log ratio, and other answers can absorb probabilities.
4. What is the real answer of decoder for logprob?

Show ExplanationThe same complete answer must contain an initial answer and an end, with the reminder subject only to conditions.

Manual completion records your progress; it does not certify your project.