Part XVI — Reinforcement Learning: Bandits, MDPs, Policy Gradients & PPO · Chapter 7

Policy gradient methods (REINFORCE)

Hook

Q-learning discovers a policy indirectly — learn a value for every action, then always take the best one. What if a policy could just be a set of parameters, adjusted by gradient ascent, the same way every other model in this course has been?

Intuition

Back to the three-armed bandit — but instead of a Q-table, there's a preference number for each arm, run through a softmax to get a probability of picking it. Step through five pulls and watch the bars themselves shift: an arm that pays off gets more likely to be picked next time, one that doesn't gets less so.

Formalize

With preferences θ\theta and policy πθ(a)=softmax(θ)a\pi_\theta(a) = \text{softmax}(\theta)_a, the REINFORCE update after taking action aa and observing return GG is:

θiθi+α(1[i=a]πθ(i))G\theta_i \leftarrow \theta_i + \alpha\big(\mathbb{1}[i{=}a] - \pi_\theta(i)\big)\,G
  • θi\theta_i — the preference parameter for action ii, before being run through softmax.
  • πθ(i)\pi_\theta(i) — the current policy's probability of picking action ii: softmax of the preferences.
  • aa — the action that was actually taken.
  • 1[i=a]\mathbb{1}[i{=}a] — equals 11 if ii is the action actually taken, 00 otherwise.
  • GG — the return observed for that action.
  • α\alpha — the learning rate controlling the update size.
  1. Same shape as the softmax cross-entropy gradient

    (1[i=a]πθ(i))\big(\mathbb{1}[i{=}a] - \pi_\theta(i)\big) is exactly the softmax cross-entropy gradient from Part II, indicator minus predicted probability.

  2. But scaled by the return, not by 1

    Cross-entropy always scales by 11 (the true label is certain), while REINFORCE scales by GG, whatever return that action happened to earn.

  3. What that scaling does

    A big positive return pushes hard toward that action; a return of exactly zero pushes not at all.

Play

Compare the very first policy to the one after five pulls. Nothing here ever built or consulted a value table — the probabilities shifted directly, one gradient step at a time, purely from the returns each pull happened to produce.

Worked example

Starting from θ=(0,0,0)\theta=(0,0,0) — a uniform policy — with α=0.5\alpha=0.5:

  1. First pull: arm A, reward 1

    π=(13,13,13)\pi = (\frac13,\frac13,\frac13) before the update. Each preference updates by α(1[i=A]πi)(1)\alpha(\mathbb{1}[i{=}A]-\pi_i)(1):

    • θA0+0.5(113)(1)=13\theta_A \leftarrow 0 + 0.5(1-\frac13)(1) = \frac13
    • θB0+0.5(013)(1)=16\theta_B \leftarrow 0 + 0.5(0-\frac13)(1) = -\frac16
    • θC0+0.5(013)(1)=16\theta_C \leftarrow 0 + 0.5(0-\frac13)(1) = -\frac16

    B and C both drop, pushed down even though they weren't pulled, simply for not being the chosen action.

  2. A reward of exactly zero changes nothing

    Pulling arm C next happens to pay 00. Since the update scales by the return, multiplying by 00 zeroes out every single term — θ\theta comes out of that step completely unchanged, regardless of which arm was pulled.

  3. After all five pulls

    Pulls 3 and 5 both pay 00, so (per the previous step) they leave θ\theta unchanged. Only pulls 2 and 4 (both pulling B, both paying 11) move the parameters:

    • After pull 2: π(0.452,0.274,0.274)\pi\approx(0.452,0.274,0.274) from θ=(13,16,16)\theta=(\frac13,-\frac16,-\frac16), giving θ(0.107,0.196,0.304)\theta \approx (0.107, 0.196, -0.304)
    • After pull 4: π(0.363,0.397,0.240)\pi\approx(0.363,0.397,0.240) from that θ\theta, giving θ(0.074,0.498,0.424)\theta \approx (-0.074, 0.498, -0.424)

    θB0.498\theta_B \approx 0.498, clearly ahead of θA0.074\theta_A \approx -0.074 and θC0.424\theta_C \approx -0.424 — the policy now assigns arm B more than half its probability mass, without ever computing a single Q(s,a)Q(s,a).

Checkpoint

Step forward until the policy gives arm B more than half the probability mass.

Step forward to try it
Summary
θiθi+α(1[i=a]πθ(i))G\theta_i \leftarrow \theta_i + \alpha\big(\mathbb{1}[i{=}a] - \pi_\theta(i)\big)\,G

Q-learning and policy gradients solve the exact same problem from two different directions: one estimates values and reads a policy off them; the other adjusts a policy directly, using nothing but the returns actions happen to produce. Both are, underneath, still gradient-based learning from a numeric signal — the same idea as every optimizer since Part II, aimed at a different kind of objective. The next chapter applies that same direct-policy idea somewhere it's become extremely consequential: teaching a language model to prefer the answers people actually like.