Reinforcement learning (RL) trains an agent from rewards instead of labelled examples. The agent acts, the environment answers with a new state and a reward, and the agent has to learn which actions pay off over the long run, including actions whose reward arrives many steps later. RL covers game-playing agents, robot control, and the final stage of training for chat models (RLHF). The core of it fits in a few equations, and a gridworld is enough to see them working.

Try something, get a reward or not, and do more of what earned rewards. Image: Megajuice, CC0, via Wikimedia Commons.

Mental Model

  • The world is a Markov decision process: the next state and reward depend only on the current state and action.
  • The goal is to maximize the discounted return, not the next reward. The discount says how much the future counts.
  • Value functions estimate the expected return from a state (or state and action). Bellman equations relate the value of a state to the values of its successors, and that recursion is what makes learning possible.
  • Value-based methods (Q-learning) learn and act greedily on it. Policy-gradient methods (REINFORCE, PPO) adjust the policy directly.
  • Exploration is part of the problem: an agent that only exploits what it already knows never finds better options.

1. Markov Decision Processes and Returns

An MDP is a tuple : states, actions, transition probabilities , rewards, and a discount factor . A policy gives the probability of each action in each state. The return from time is

With and three rewards of 1, the return is . A constant reward of 1 forever has return . A near 1 makes the agent far-sighted and the learning problem harder; a small makes it greedy. Discounting also keeps infinite sums finite.

2. Value Functions and Bellman Equations

The state value and the action value satisfy a recursion:

The best achievable values satisfy the Bellman optimality equation

and the optimal policy is greedy with respect to . If you know you can solve these by iterating the equation (dynamic programming). Model-free methods learn from sampled experience instead.

3. Q-Learning on a Gridworld

Q-learning (Watkins, 1989) keeps a table and nudges each entry toward a one-step bootstrap target after every transition:

It is off-policy: the target uses the greedy action even though the agent behaved -greedily (random action with probability , otherwise the best known). The grid below is the classic 3x4 example: start at the bottom left, a wall in the middle, +1 for reaching the top-right cell, -1 for the cell below it, and a step cost of -0.04. Moves are deterministic and bumping into a wall leaves the agent in place.

import numpy as np
 
R, C, wall = 3, 4, {(1, 1)}
term = {(0, 3): 1.0, (1, 3): -1.0}
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]          # up, down, left, right
 
def step(s, a):
    n = (s[0] + moves[a][0], s[1] + moves[a][1])
    if not (0 <= n[0] < R and 0 <= n[1] < C) or n in wall:
        n = s
    return (n, term[n], True) if n in term else (n, -0.04, False)
 
rng = np.random.default_rng(0)
Q = np.zeros((R, C, 4))
gamma, alpha, eps = 0.9, 0.5, 0.2
for episode in range(2000):
    s = (2, 0)
    for _ in range(100):
        a = rng.integers(4) if rng.random() < eps else int(Q[s].argmax())
        s2, r, done = step(s, a)
        Q[s][a] += alpha * (r + (0 if done else gamma * Q[s2].max()) - Q[s][a])
        s = s2
        if done:
            break
 
arrows = "^v<>"
for i in range(R):
    print(" ".join("#" if (i, j) in wall else "G" if (i, j) == (0, 3) else "X" if (i, j) == (1, 3)
                   else arrows[Q[i, j].argmax()] for j in range(C)))
print(np.round(Q.max(-1), 2))

Running it prints the greedy policy

> > > G
^ # ^ X
^ > ^ <

and state values of 0.73, 0.86 and 1.0 along the top row, 0.62 and 0.86 in the middle row, and 0.52, 0.62, 0.73, 0.62 along the bottom. The values agree with working backwards by hand: the cell next to the goal is worth (no step cost on the final move), the one before it , then . Every arrow leads along a shortest path to the goal and none steps onto the -1 cell. From the bottom right the agent walks left and then up.

4. Policy Gradients and REINFORCE

When the action space is large or continuous, a table is impossible. Instead parameterize the policy and ascend the expected return . The policy gradient theorem gives

REINFORCE (Williams, 1992) estimates this from sampled episodes: raise the log-probability of each action in proportion to the return that followed it. The estimate has high variance, so one subtracts a baseline , typically a learned value function, replacing by an advantage . Subtracting a function of the state does not bias the gradient. A learned baseline is the starting point of actor-critic methods.

5. PPO

Plain policy gradients can take a step that wrecks the policy, and the data collected by the old policy is then useless. Proximal policy optimization (Schulman et al., 2017) lets you reuse a batch for several gradient steps while limiting how far the policy moves. With the probability ratio , the clipped objective is

with a common default.

Worked example

Let and . With a positive advantage the terms are and , the minimum is 2.4, and since that is the clipped constant the gradient is zero: the action is already more likely than it should be, so there is no more push. With the terms are and , the minimum is (unclipped), so the gradient still pushes the probability back down. The objective is pessimistic: it never rewards moving further than the clip range in the helpful direction, but it fully penalizes a harmful move.

In practice PPO adds a value-function loss and often an entropy bonus, and uses generalized advantage estimation to compute .

6. RLHF in One Section

Reinforcement learning from human feedback (Christiano et al., 2017; applied to language models in InstructGPT, Ouyang et al., 2022) has three steps:

  1. Start from a pretrained model and fine-tune it on demonstrations (supervised fine-tuning).
  2. Collect human comparisons between pairs of responses and fit a reward model with a Bradley-Terry loss, , where is the preferred response.
  3. Optimize the policy with PPO to maximize . The KL penalty to the supervised model stops the policy from drifting into text that fools the reward model.

Direct preference optimization (Rafailov et al., 2023) folds steps 2 and 3 into a single supervised loss on the preference pairs, with no explicit reward model and no sampling loop.