AI Hero Club

Reasoning and Reinforcement Learning · Lesson 2

Marked Complete by You0 / 8

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

Lesson Outline 7 subsections
Course Contents 8 lessons
Full course plan.
Reasoning and Reinforcement Learning / Learning Plan ↗

LESSON 02 / 8Suggested study 135 minutes

Derive the Full-trajectory Policy Gradient from Finite Sums

When a reward is not directly differentiable with respect to parameters, how do finite trajectory sums yield a policy gradient?

3 Questions for This Lesson
  • Line-by-line extrascore Historic Equivalent with REINFORCE
  • Triangulation checks using a parsing gradient, limited differentials and tracks
  • Distinguishing training targets, sample gradients and one optimizer update
Course Project ZIP ↓

Write Down the Quantity to Optimize

For the time being, this chapter uses no neurological network. Only a limited strategy of three actions is available. z=(z0,z1,z2)z=(z_0,z_1,z_2) It's three real points, the probability of being downsized by softmax; the reward is fixed at one point in action zero and the rest zero. J(z)J(z) The expectation here is to repeat the average of the pumping action obtained by the same probability distribution, not the direct equivalent of the three incentives. When entered, this already contains the most central difficulty of the project ' s strategic learning: the sampling distribution itself depends on the parameters to be updated.

Readers know the guide and the principle of the commercial. Z=∑jezjZ=\sum_j e^{z_j}, pi=ezi/Zp_i=e^{z_i}/Z. The k points are guided by the molecules' instructions, the denominator's index entries, and the numeric ones are sorted as ∂pi/∂zk=pi(1i=k−pk)\partial p_i/\partial z_k=p_i(\mathbf1_{i=k}-p_k). The logarithmic probability lead is obtained by dividing it by a positive probability. It is called score, which indicates the local sensitivity of the logarithmic probability to the parameter that has been pumped, not the reward score.

Probability Normalization Implies a Zero-Mean Score

The score of each action is weighted by the probability of the action, and it has a sum of probabilities. Limited and can be guided on a case-by-case basis, while all probabilities are consistent on a single basis, with zero. This is the feed point for the back baseline and KL. The condition is that the current parameters are close to supporting non-frozen parameters, and that the current ten figures softmax are always strict and meet the conditions. It cannot control the hard threshold of “Is this allowed?” without the same guidance formula.

Yeah. J=∑ipiriJ=\sum_i p_i r_i Guides, rewards are fixed in this chapter, and no arguments are guided. Using the probabilistic guide is equal to the probabilities multiplied by the probabilities of the logarithm, the tactical gradient is the incentive to multiply the score expectations. This is an exact constant equation in a limited sample space, without the need for schilling samples to become endless. ri∇log⁡pir_i\nabla\log p_i It is only a random estimate of this expectation, which may be in poor direction; the precise constant equation must be separated from the limited sample estimate.

∇J=∑yR(y)∇pθ(y)=∑ypθ(y)R(y)∇log⁡pθ(y)=E[R∇log⁡pθ(y)]\nabla J=\sum_y R(y)\nabla p_\theta(y)=\sum_y p_\theta(y)R(y)\nabla\log p_\theta(y)=\mathbb E[R\nabla\log p_\theta(y)]
Limited, fixed support, incentives for precise gradients when not dependent on parameters.

Example 1: A Complete Three-Action Gradient Calculation

The first three points are zero, so one third of the probability for each action is expected to be rewarded by one third. p0(1−J)=2/9p_0(1-J)=2/9; split between the other two pk(0−J)=−1/9p_k(0-J)=-1/9. Three and zero, matching all points to the same constant does not change the softmax peer symmetry. Walking down this gradient is small enough to improve the relative score of the correct action; not three scores rise indefinitely together.

When you draw the correct action, the single sample gradient is [2/3,-1/3,-1/3], with a probability of one third; when you draw the wrong action, zero is rewarded, and the single sample gradient is zero, and [2/9,-1/9,-1/9] is combined. The zero gradient at the time you draw the wrong action cannot be interpreted as “The error action does not need to be reduced”, while the correct action is upgraded to the level where the other probabilities are squeezed. Once you add the baseline, the error action can also be given a non-zero sample gradient, but the overall expectation remains the same, and chapter III will prove precisely.

Then we'll score asymmetrically. [0.3,-0.2,0.1], item lab.py output actual probability, parsing gradient, and center limit difference. The center score adds or subtracts the k score by a smaller volume, h, recalculating the J, divided by 2h. When the function is smooth and the h is moderate, the cut error is a second scale; h is too small to be dominated by a float offset error. The project uses a negative five-point ratio of h to ten and examines the error rather than in general terms. “Numerical guidance is the exact value.” 。

From Individual Actions to Complete Trajectories

Now back to input x and two-step response y. Use. θ\theta The self-regression definition states that the probability of the first step is determined by input, and that the second step is conditioned on the first step already written, with the full probability multiplying both. Taking a logarithm turns the multiplier into a sum, then guides the shared parameter, and gets two positions score. A successful end reward goes on the whole and means that the front and back actions take credit; not just updating the output of the last number. Both steps go through the same Transformer, and the parameter gradients are also transmitted through the middle invisible state.

log⁡pθ(y∣x)=log⁡pθ(u∣x)+log⁡pθ(v∣x,u),E[∇log⁡pθ(y∣x)]=0\log p_\theta(y\mid x)=\log p_\theta(u\mid x)+\log p_\theta(v\mid x,u),\quad \mathbb E[\nabla\log p_\theta(y\mid x)]=0
EOS is limited to probability one; two-step sharing of all model parameters.

The reminder part is not a tactical extraction response, and does not add the hint-word probability to the apparent response. End action forces a probability of one, zero logarithm, zero gradient in this agreement, and should not be added to a false negative logarithm for 14 groups of softmax. If a free text agreement is changed, EOS will become a real random move, requiring a redefinition of sample space, cut rules and approximation. This project deliberately fixes two steps to see these boundaries clearly and does not pose fixed length conclusions as any long text.

Example 2: Calculating Two Paths with a Shared Prefix

Sets the probability of a first step four at 0. 2, and the probability of a second step eight after four is 0. 3, or the path [4,8,EOS] is 0. 06, or log0.2 plus log0. 3. If the final validation is passed, the strategy gradient sample is the sum of the first step score and the second step score. If the second step log0.3 is used, there is no direct first step to select credit, the target is no longer the probability of a complete path; That's why the path_logps in project have to add two places together.

Another [4,7,EOS] shares the first step, which is different and failed. The local gradients of the two paths are not separate from the shared parameters, because the same input and prefix are calculated using the same graph. After sampling, the response numbers are re-estimated as fixed data to legal score; do not try to find a normal guide for the output number of the discrete multinomial. What we seek is a sample of the probabilities of the number that has been processed through the desired constant equation.

Signs, Averaging, and Update Order in the Code

Optimizers usually minimize losses, so the maximum incentive is given to the target of the sample with a negative number. A single sample loses a negative incentive times the average of logarithm probabilities, the gradients are reduced after inverse transmission, and the gradients for reward targets rise. If used and not average, the updated scale is multiplied by batches at the same learning rate; the volume cannot be changed, but the experiment is still described as exactly the same optimal setting.

The training cycle must first empty the previous gradients, calculate the current map, reverse disseminate, check the limited values, and then crop and update them. PyTorch defaults on the cumulative gradient; forgets that emptiness can be mixed into the old batch. Incentives, baselines and reference probability stop the gradient in the estimater as required, chapter IV explains why; model current log probability retains the gradient. Detach does not allow models “No study.” to be used, but rather avoids miscalculating the weight of the estimate device as another path for minor loss.

Independent Checks and Conclusions That Must Not Be Substituted

The project provides three-track reconciliations: stdlib's three actions resolve the difference between centre; full 100 paths seek direct automatic fractions of summation; and the same one holds only the score path after stopping the gradient for probabilities and returns. The latter two paths equal the algebra check not for sample accuracy, but rather for target gradients.

These checks only demonstrate consistency in the local gradients of the specified function. It does not guarantee that limited step optimization is best on the global scale, nor that training topic upgrades can be moved to unincorporated input. The accompanied models, limited step lengths, cropping, Adam status and sampling differentials are all involved in the actual trajectory. The project therefore retains initialization seeds, monitoring steps, RL steps and a gradient book for each phase; Chapter VII uses the freeze check point to measure the true results rather than using them. “Gradient is correct” Alternative “Model Society” 。

Run in the Project DirectoryDirectly Run Check
from lab import finite
r=finite([.3,-.2,.1])
assert r["max_error"] < 1e-9
assert abs(sum(r["gradient"])) < 1e-12
print(r)

TRY IT YOURSELF

Practice I: Rebuild score from commercial law

Yeah. p_i=exp(z_i)/sum_j exp(z_j) Request the z_k lead number and give the log p_i lead number.

Hint 1
The commercial rules are used to find the guide between softmax molecules and denominators.
Hint 2
Collapse the results into brackets at p_i and remove the p_i for guidance on the log; and then re-align the i by p_i for weighting.
Compare with the Solution
Molecular guide is an indicator. i=k Multiply exp(z_i), the number of denominator guides exp(z_k). Commercial law is instructed i=k By p_i minus p_i_k, sort p_i(Instructionsi=k−p_k). Because softmax probabilities are positive, divided by p_i get the log guide indicator i=k−p_k. By p_i, the first p_k minus the second p_k multiplied by the sum of 1.

TRY IT YOURSELF

Practice 2: Inverse transmission is missing one step

Path probability 0.2×0.3, only log0.3 times reward training. What's missing?

Hint 1
How many entries should be kept after the path probability is multiplied?
Hint 2
Draws the path of first step and second step score to target; indirect changes in shared parameters do not amount to missing entries being restored.
Compare with the Solution
Complete log probability is log0.2+log0.3. Two steps are required for guidance, and missing the first step score would lose credit for directly raising the correct middle number. The sharing of parameters may still indirectly change the probability of the first step, but that does not restore the missing target item. It must be matched by the actual response position.

TRY IT YOURSELF

Practice III: Boundaries with limited differentials

Does the error necessarily decrease by reducing the difference h from 1e-5 to 1e-16?

Hint 1
The central difference is accompanied by a cut-off error with a floating point.
Hint 2
The former is reduced with h and the latter is expanded by h, seeking to stabilize the zone rather than just trying to minimize the pace.
Compare with the Solution
No. The smooth function cut-off error usually decreases with h square, but the double-precision function value decreases to offsets the effect by multiplying the rounding error by a very small h. A section of h-stabilized area should be viewed and contrasted with parsing/auto-dense independent, and the smallest h cannot be considered as the highest accuracy.

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. score?

Show ExplanationIt describes the parameter sensitivity of the probability of the currently drawn action.
2. Why is it the sum of the probability of using logs based on complete trajectory?

Show ExplanationThere is no need to act independently; step two is a first step.
3. How do you maximize the rewards when the optimizer is minimized?

Show ExplanationThe gradient drops negative target equals the gradient rises positive target.
4. Gradient check proves what?

Show ExplanationLocal guides cannot be replicated as successful or generalized learning.

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