← → / SPACE to navigate
LECTURE 08 · FINALE · FUNCTION APPROXIMATION FOR POLICY ITERATION

Policy iteration, approximated.

Policy iteration is exact — if you can touch every state. Go has more positions than atoms in the universe, so the table is impossible and the policy must become a function we fit from data. We'll follow one example — AlphaGo — from "why a table fails" all the way to the move it missed against Lee Sedol, and to how the same recipe trained ChatGPT.

① The tabular loop

Policy iteration, and why Go breaks it.

② Function approximation

Store a function, not a table — the universal approximator.

③ The catch

It only works where data taught it (Game 4).

④ The update at scale

PMD · TRPO · PPO, in AlphaGo & ChatGPT.

References: Lan, Optimization Methods for RL, §2.4 · Cybenko (1989), Hornik (1991) · Schulman et al. (TRPO 2015, GAE 2016, PPO 2017) · Silver et al. (AlphaGo 2016).

Title
01 / 11
1 The exact loop

Policy iteration: evaluate, then improve.

Alternate two steps until the policy stops changing. Improvement is greedy: at each state, move to the action with the largest advantage. When no action can beat the current average anywhere, we've reached the optimal policy \(\pi^\star\).

Step A · Policy evaluation

Score the current policy \(\pi_k\).

\[ Q^{\pi_k}(s,a)=R(s,a)+\gamma\,\mathbb E_{s'}\big[V^{\pi_k}(s')\big] \]

Subtract the state's own average to get the advantage \(A^{\pi_k}=Q^{\pi_k}-V^{\pi_k}\): how much better than \(\pi_k\)'s average an action is. Averaged over \(\pi_k\)'s own actions it is \(0\).

Step B · Policy improvement

Take the max-advantage action.

\[ \pi_{k+1}(\cdot\mid s)=\arg\max_{p\in\Delta}\ \big\langle A^{\pi_k}(s,\cdot),\,p\big\rangle \]

All the mass goes on \(\arg\max_a A^{\pi_k}(s,a)\), the biggest-advantage action. (Same as greedy on \(Q\) — subtracting \(V(s)\) never moves the max.)

The improvement each step buys
\[ \max_{a}\,A^{\pi_k}(s,a)\ \ge\ 0\ \ \text{for every } s,\qquad =0\ \ \text{only at } \pi^\star. \]

That largest advantage is the room left to improve at \(s\). Grab it and the value can never drop — \(V^{\pi_{k+1}}(s)\ge V^{\pi_k}(s)\), the improvement theorem. Once it hits \(0\) everywhere, no action beats the average and the policy is optimal.

Clean and exact — but the greedy max needs a stored advantage for every state, solved one at a time. Fine on a gridworld. Let's try it on Go.

Policy iteration
02 / 11
1 The tabular attempt

Could we solve Go with a table?

The plan is exactly policy iteration: one row per board position, holding its value and its best move. Enumerate every position, evaluate it, improve it, repeat. So — how big is that table?

The tabular plan

One row per position.

  • Store \(V(s)\) and a best move for each board \(s\)
  • Sweep all \(s\): evaluate, then greedy-improve
  • Repeat until nothing changes
Why it can't exist

\(\sim 10^{172}\) legal positions.

  • Storage: more rows than atoms in the universe
  • Visitation: you'd see almost every position zero times — its value never learned
  • No two boards are "close" in a lookup table

The table can't be stored and can't be filled. The only way out: something that reads any position — even unseen ones — and returns a move. We need to generalize.

Go by table?
03 / 11
2 The fix

Store a function, not a table.

Don't memorize an answer per position. Learn one function \(\pi_\theta\) that reads a position and outputs a move distribution. Then we keep only its parameters \(\theta\) — a fixed, modest list of numbers — instead of the whole table.

\[ \pi_\theta(a\mid s)=\mathrm{softmax}\big(f_\theta(s,\cdot)\big)_a \qquad\Longrightarrow\qquad \text{store }\theta,\ \text{not the }|\mathcal S||\mathcal A|\text{ table} \]

One function, any state

Feed any board — including positions never seen in training — and get a move distribution out. That's generalization.

Storage collapses

Go's \(\sim 10^{172}\)-row table becomes \(\sim 10^{8}\) weights. The count no longer grows with the number of states.

Similar in, similar out

Nearby positions share weights, so one update nudges many at once. The table treated every row as unrelated.

The old table is just the special case where \(f_\theta\) is a plain lookup (one-hot features, no sharing). Function approximation is the same object with the states tied together.

Function approximation
04 / 11
2 choosing the form

What function should we choose?

We store a function \(f_\theta\) instead of a table — but which one? First we pick a function class: a family of candidate functions that share one form and differ only by their parameters \(\theta\). Fitting then searches inside that class for the member closest to the data.

f_θ data
Green dots = data; dashed = the gaps. Least squares picks \(\theta\) so the curve \(f_\theta\) minimizes the sum of squared gaps.
a function class: one form, many members
\[ \mathcal F_{\text{lin}}=\big\{\,f_\theta(x)=w^\top x+b\ :\ \theta=(w,b)\,\big\} \]
learning = pick the best member
\[ \theta^\star=\arg\min_{\theta}\ \tfrac{1}{N}\sum_{i=1}^{N}\big(f_\theta(x_i)-y_i\big)^2 \]
  • The class sets what's reachable. The linear class holds only lines and planes — if the truth curves, no member fits well.
  • Bigger class, more shapes. Polynomials, then kernels, add curvature — but you still hand-pick the class (the degree, the kernel).
  • Neural network — a class so rich it is a universal approximator (Cybenko '89, Hornik '91): enough neurons hold a match to any continuous function, so we stop choosing the form. Cost: data hunger.
❓ Ask the class
In an MDP, what is \(f_\theta\) actually approximating?
The very objects the table used to hold — the policy \(\pi_\theta(a\mid s)\) and the value \(V_\theta(s),\,Q_\theta(s,a)\) (the advantage follows). All functions of the state, with far too many states to tabulate — so we fit.
Which function?
05 / 11
3 The trade-off

Great where data taught it — lost where it didn't.

Fitting \(\theta\) pins the function down only where training data lives (the support). Off that support it must guess — and a flexible net can guess wildly.

move 78 seen in training no data → guesses
Dashed = truth · solid = learned \(f_\theta\). Inside the data band they agree; outside, the fit peels off.
  • On support — dense data, the fit tracks the truth. Interpolation is where deep RL shines.
  • Off support — no data to constrain it, so \(f_\theta\) extrapolates, often badly. Capacity becomes a liability.
  • AlphaGo vs. Lee Sedol, Game 4: Lee's move 78 was one the team reported AlphaGo rated near 1-in-10,000. The position it created was off the distribution its networks had learned — its value estimates went astray, and it lost that game.
  • The fix is never "more capacity" — it's more relevant data, and staying near where you have it.
On vs off support
06 / 11
4 Step B on \(\theta\)

Improving a function: one sampled objective.

In a table, Step B is an exact per-state \(\arg\max\): you hold the true \(A^{\pi_k}\) for every \((s,a)\) and solve each state on its own. A function has neither — no per-state knob, and only a finite-sample estimate \(\hat A\) from rollouts. So Step B becomes a single objective maximized over \(\theta\).

Tabular PI · exact, per state

Solve each state on its own.

\[ \pi_{k+1}(\cdot\mid s)=\arg\max_{p\in\Delta}\ \langle A^{\pi_k}(s,\cdot),\,p\rangle \]

You hold the true \(A^{\pi_k}\) for every state — a closed-form \(\arg\max\), solved one state at a time, a full sweep each round.

Function · policy mirror descent

Push toward \(\hat A\), stay near \(\pi_k\).

\[ \max_{\theta}\ \frac{1}{N}\sum_{i=1}^{N}\Big[\ \eta\,\big\langle \hat A(s_i,\cdot),\,\pi_\theta(\cdot\mid s_i)\big\rangle-\mathrm{KL}\big(\pi_\theta(\cdot\mid s_i)\,\|\,\pi_k(\cdot\mid s_i)\big)\ \Big] \]

Roll out \(\pi_k\), estimate the advantages \(\hat A\), then a gradient step on the shared \(\theta\). Same \(\langle \hat A,\pi_\theta\rangle\) as the left — averaged over sampled states, plus a \(-\mathrm{KL}\) leash to \(\pi_k\).

Function approximation is needed precisely because we can't enumerate every state and action. So \(\hat A\) (and \(Q\)) is reliable only around the states and actions the current policy visits often, and can be far off where the data never reached. A greedy jump can land the policy right in that untrusted region — betting on numbers that are wrong. So we move gradually: small step (the \(-\mathrm{KL}\) leash), collect new experience, re-estimate \(\hat A\), repeat.

Don't jump beyond the part of the state space where your value estimates are trustworthy.
Improve the function
07 / 11
5 LLMs as RL · the modeling

A language model is a policy.

Generating a response is an episode: choose tokens one at a time. That maps exactly onto the RL objects we have used all along.

State
prompt + tokens so far
\[ s_t=(x,\ y_1,\dots,y_{t-1}) \]
Action
the next token
\[ a_t=y_t\ \in\ \mathcal V\ \ \text{(vocabulary)} \]
Policy
the model itself
\[ \pi_\theta(a_t\mid s_t)\ \text{— next-token distribution} \]
Kernel
dynamics — deterministic
\[ P(s_{t+1}\mid s_t,a_t)=1,\ \ s_{t+1}=s_t\!\oplus\! a_t \]
Reward
terminal — whole response
\[ R=r(x,y)\ \text{at the end};\ \ 0\ \text{before} \]

The transition kernel is trivial — appending a token is deterministic. All the difficulty moves into the reward and the advantage.

LLM as RL
08 / 11
5 LLMs as RL · training

Fine-tune by trial and reward.

The same evaluate → improve loop as policy iteration, now on language — with the small, careful steps from two slides ago.

① Roll out

For each prompt \(x\), sample responses \(y\sim\pi_\theta(\cdot\mid x)\) — the model answers.

② Score

Rate each finished response with a reward \(R(x,y)\); the advantage is how much better than the model's typical answer.

③ Update

Take a small step raising the probability of above-average responses — then resample and repeat.

\[ \theta\ \leftarrow\ \theta+\alpha\,\mathbb E\big[\,\nabla_\theta\log\pi_\theta(y\mid x)\,\cdot\,A(x,y)\,\big] \]

This is RLHF (and RL with verifiable rewards). Clean in principle — but it needs a reward \(R\) and an advantage \(A\), and getting each is genuinely hard.

LLM training
09 / 11
5 LLMs as RL · what's hard

Two things stand in the way.

Challenge 1

Reward is hard to get.

  • There is no formula for "a good answer."
  • Learn one from human preferences — a reward model — but it is noisy and can be gamed (reward hacking).
  • Verifiable rewards (math, code, unit tests) are trustworthy, but exist only in narrow domains.
  • One signal for a whole response: sparse.
Challenge 2

Advantage is hard to calculate.

  • Advantage = reward − baseline; the baseline \(V(s)\) must be estimated for language states — high variance.
  • Reward arrives only at the end, so credit must be spread over hundreds of tokens (credit assignment).
  • Practical fixes: a learned critic, or group-relative — sample many answers, score against their mean.

The kernel was free and the policy is just the model. Nearly all the engineering in modern LLM RL goes into estimating \(R\) and \(A\) well enough to take a good step.

Challenges
10 / 11
YOUR TURN

Now you model an RL problem.

Pick a problem you care about and cast it into the objects we've used all lecture. Name these five and you can hand it to policy iteration.

Name these five
State \(s\) — what the agent sees before it decides
Action \(a\) — what it gets to choose
Policy \(\pi_\theta(a\mid s)\) — the function from state to action
Reward \(R(s,a)\) — the outcome you actually want
Transition \(P(s'\mid s,a)\) — how the world responds
Advantage \(A^\pi(s,a)\) — is this action better than average?

Then run the loop you now know: evaluate \(V^\pi\), improve toward the max-advantage action, keep each step on a short leash — and repeat.

Your turn
11 / 11