You can take 2 points now, or wait one step for a possible 4. Which should you choose? Reinforcement learning (RL) considers both the immediate reward and what a decision changes later. These notes start with small environments, then build up to value estimates, policy updates, and working training loops.

Each chapter starts with an example before introducing equations and code. On a first read, follow the examples; when reviewing, use the tables to compare algorithms and check implementations. The examples are original toy environments, not course assignments or reproduced slides.

If the names blur together, start with Q-learning, PPO, and DPO. For current difficulties, read signal, cost, and stability. For their origins, follow Tech history into the RL timeline.

Find a chapter by topic

In one linePick a route by your problem: returns and targets first, then policy, value or model-based methods, plus data, exploration, imitation and debugging.

Route Read What you should be able to do
Define the problem MDPs and Bellman → MC, TD, and boundaries Compute returns and training targets from a trajectory
Change the policy directly Policy gradients → Actor–Critic and GAE → Natural gradient and TRPO Explain gradients, baselines, the two masks, and why step size needs control
Choose actions through value Fitted Q and convergence limits → DQN → DDPG / TD3 → SAC Separate regression loss from policy performance and trace network gradients
Think ahead with a model Model-based RL → Which model to learn → Planning and LQR → Combining planning and learning Separate dynamics learning, planning, and policy learning; say what a model should predict and how search results become training targets
Understand data and rewards Offline RL and OPE → Per-decision weights, doubly robust, and LSTD · Exploration and hierarchy · Imitation and rewards State the added assumptions, not just the algorithm names
Generate offline training data with a model Offline RL and OPE → Offline model-based RL Calculate reward penalties, HALT values, and conservative critic updates; distinguish predicted trajectories from real evidence
Decide what to try next Exploration and hierarchy → Exploration and uncertainty Calculate UCB scores, posteriors, and information gain; distinguish novelty, randomness, and useful exploration
Learn from demonstrations Imitation and rewards → MaxEnt IRL → GAIL · Reward-model ranking losses Fit feature expectations, calculate a discriminator reward, and distinguish pairwise from full-ranking supervision
Run a meaningful experiment Experiments and debugging → The LLM bridge Check the implementation before interpreting reward as progress

These lessons go beyond algorithm names. We work through Bellman residual bounds, TD targets, gradient directions, PPO clipping, and control trajectories, then examine how CQL, IQL, and FQE handle offline data. The imitation sequence adds reward fitting and distribution matching through worked examples. Hierarchy and multi-agent learning remain introductions rather than comprehensive theoretical treatments.

For language-model training, continue with RLOO's leave-one-out baseline: it adds a baseline proof and worked arithmetic, and needs no multi-GPU setup.

The reward needs its own check. What a reward model predicts uses a small arithmetic problem to distinguish correct answers, valid steps, and future success, then works through label masks and score aggregation. For inference-time search, continue with splitting a fixed budget between generation and verification.

Review: six calculation and implementation questions

In one lineSix implementation questions with numbers and conditions, such as why leave-one-out excludes itself and why a shared EOS/PAD ID can't be masked wholesale.

If you know policy gradients and want to prepare for a technical discussion or debug training code, start here. Each row links to a specific calculation or derivation, so you can revisit one point without rereading the whole chapter. If baselines, returns, and advantages are still unfamiliar, follow the foundations route above first.

Question What your explanation should include Derivation or worked example
Same-prompt rewards are 1, 0, and 0.5. What are the leave-one-out advantages, and why exclude each sample itself? The advantages are 0.75, −0.75, and 0. Unbiasedness uses independent sampling conditional on the prompt and a zero-mean score function; subtracting any mean is not equivalent RLOO calculation and proof
Does assigning higher reward to demonstrated routes necessarily improve an IRL model? Feasible alternatives matter, so the partition function cannot be dropped. The three-route gradient is $1.6-10/7$; it matches a feature expectation, not every route frequency The MaxEnt IRL gradient
Does 50% discriminator accuracy establish successful GAIL imitation? It can indicate matching distributions or a weak discriminator. Check independent task outcomes too; at ideal matching, our per-step reward is $\log2$, not zero Discrimination and distribution matching
Can a ranking of three responses be treated as three independent preference pairs? Pairwise loss is a valid choice but differs from full-ranking PL loss. State whether prompts or pairs receive equal weight, so extra candidates do not silently dominate training Pairwise versus ranking objectives
If EOS and PAD share an ID, can every occurrence of that ID be masked out? No: a generated EOS is an action; padding is not. Build masks from generation boundaries and align logits with the following token Which tokens get the gradient
For per-token losses $[2,4]$ and $[1,1,1,1]$, why do three means differ? Mean sequence sums give 5, mean per-response means give 2, and the valid-token mean gives $5/3$. The denominator changes sample weights; microbatching should preserve the chosen objective A sum, not a mean, over each response

Once you can reproduce the numbers, change an assumption: correlated samples, unequal candidate counts, variable trajectory lengths, or a reward-service timeout. Does the calculation still hold? If not, which step needs to change?

Check your understanding with counterexamples

In one lineTen counterexamples to plausible claims, such as that mean return is the lowest-variance baseline or that 256 failures prove a model cannot succeed.

These questions do not require more algorithm names. Predict the answer first, then use a small environment or calculation to find the missing assumption.

Question Example to work through
If the policy stays fixed, does the state distribution stay fixed too? The alternating L–R chain: stationary probabilities are equal, but discounted visitation is $2/3$, $1/3$
Does mean return always give the lowest-variance baseline? The two-action bandit: mean return is 2.6, but the variance-minimizing baseline here is 1.4
If Q-learning can reuse old data, can multistep returns be used unchanged? Two-step replay: target value is 6.4, but the uncorrected two-step target has mean 2.35
Do accurate model predictions make their gradients reliable? Matching values, opposite derivatives, then a zero-variance but biased gradient
If the best logged episode earns 3, is 3 the best an offline policy can do? Stitching compatible transitions: the stipulated deterministic MDP allows return 5, even when the logged sample gives trajectory IS no nonzero weights
Does conservative value learning lower every Q-value? COMBO's two-entry fit: A rises from 2 to about 2.5714, while B falls from 5 to about 3.6667; the direction depends on sampling distributions
Is an unpredictable result worth exploring? A probe and a random coin: the probe provides about 0.2781 bit; the coin provides no information about the hidden state
If success becomes more likely after a prefix, did the prefix become more valid? Changing the continuation policy: the same prefix has sample success rates of 1/4 and 3/4
Does higher reward-model pair accuracy guarantee a better selected response? The five-response counterexample: 90% pair accuracy can still rank the only acceptable answer second
Do 256 failures on a fixed task prove the model cannot succeed? Zero successes and discovery budgets: a true success rate of 0.5% still gives about a 27.7% chance of no successes

Each example states its assumptions. For review, explain not only the numbers but whether the calculation survives stochastic transitions, a different policy, or less data.

Reading the examples, derivations, and code

In one lineBuild intuition from the examples, then check the conditions in the equations; when implementing, debug in a tiny environment, not by curves.

What you want today How to read What to take away
Build intuition Follow the opening task, diagrams, and calculation tables; skip derivations for now Explain why a component is needed
Understand the mechanism Revisit equations, conditional expectations, symbols, and assumptions Calculate an example and locate the limits of a guarantee
Implement it Inspect data sources, targets, gradients, masks, and update timing Debug in a tiny environment rather than relying only on curves

Key calculations are visible in the text without moving sliders or opening an interactive panel. A few flows briefly highlight reading order; all content remains visible afterward, and reduced-motion settings are respected. Existing interactive experiments remain optional ways to change parameters, not substitutes for the lesson.

Three steps to look for in an algorithm

In one lineBreak any algorithm into sampling, a target and an update. Training on old data while still interacting can be both online and off-policy.

Sample
Which policy collects data?
→Estimate
What is the target?
→Update
Where do gradients go?
Method Main learned object Action selection An easily missed cost
REINFORCE Policy Sample from the policy High return variance; old data cannot be mixed in naively
DQN Q-value Discrete argmax with exploration during training Bootstrapping, approximation, and off-policy data can destabilize learning
State-value Actor–Critic Policy + V Sample from the policy Critic errors affect the Actor
TD3 / SAC Policy + two Q-functions A continuous-action policy Reusing data saves interaction, not necessarily computation
Model-based Dynamics, sometimes policy / value Planning or policy Accurate predictions do not ensure reliable planned behavior

On-policy / off-policy compares the behavior policy collecting data with the target policy being evaluated or improved. Online / offline describes whether training can obtain new interactions. A method that keeps interacting while learning from older data can be both online and off-policy.

Prerequisites and notation

In one lineConditional expectation, the chain rule and gradients are enough to start. Reward, return and value are three different numbers.

Conditional expectation, the chain rule, and gradients are enough to start. For a refresher on probability and gradients, see ML mathematics. The PyTorch examples assume familiarity with autograd and tensor shapes.

Throughout, $s_t$ is the state, $a_t$ the action, $r_t$ the reward received after that action, and $\gamma$ the discount. Finite episodes use $t=0,\ldots,T-1$; terminal value is zero. We use $\pi_\theta$ for a stochastic policy and $\phi$ for value parameters. Reward, return, and value are not interchangeable.

These terms recur throughout the notes:

Term Meaning here
Policy Chooses an action or assigns action probabilities given a state
Reward Feedback after one step, not the entire trajectory's outcome
Return Future rewards accumulated from a step using the chosen convention
Value Expected future return under a particular policy
Training target The number fitted in this update; it may be an estimate, not ground truth
Bootstrapping Using an existing value estimate for the future not yet observed
Trajectory / rollout A sequence of states, actions, and rewards; a rollout may be only a segment

Change one assumption at a time

In one lineAfter an example, change one condition, like termination to timeout, and redo the calculation. The page experiments show the math, not real training.

After a worked example, change just one condition: replace termination with a timeout, move $\lambda$ from 0 to 1, or make two Q-networks rank actions differently. The small interactive experiments are for exactly this. They illustrate computations, not complete training runs or invented performance gains.

Run the reference calculations to check the arithmetic, then the PyTorch checks to inspect stop-gradient behavior. Neither requires a GPU.

Figures you can experiment with

In one lineFive adjustable figures, each for one question, such as how reward flows back or how much one update changes. Guess first, then check the table.

Question Chapter What to change
How does reward propagate backward? MDPs and Bellman Adjust the discount and step through backups
Which future steps affect this advantage? Actor–Critic and GAE Adjust λ and inspect the contribution table
Why can better regression still diverge? Fitted Q Advance fits and compare value curves with MSE
How much does one policy update change? Natural gradient and TRPO Adjust the logit step and KL budget
How does feedback help a wrong model? Model-based RL Adjust model error and horizon; compare real trajectories

After changing a parameter, try explaining the result before opening the calculation table. The point is not to memorize which way a curve moves, but to see how a change carries through the calculation.

Public references

For terminology and derivations, see Spinning Up: RL foundations; individual chapters link original papers. PPO and RLHF already have a separate sequence. Continue there after these foundations rather than memorizing the same definitions twice. To see these methods applied to multi-step agents and world models, continue to training agents with RL and world models in the frontier series.