Reinforcement learning explained

A child learning to ride a bicycle is not shown a table of correct answers; she tries, wobbles, falls and slowly finds out what works. Reinforcement learning gives a machine the same kind of lesson. This page explains the ideas behind it with the simplest world in which they can be seen: a grid of cells with walls, a goal and pits.

A grid world of four rows and twelve columns with a row of pits between the start and the goal flag, cells coloured from purple to yellow by their learned value, white arrows for the best move in each cell, an orange path that keeps away from the pits and a dashed pink path along their edge, and a chart of the sum of rewards per episode for SARSA and Q-learning

Learning from rewards

In supervised learning a model is shown examples with the right answers. In reinforcement learning there are no right answers, only consequences: an agent acts in an environment, the environment changes and hands back a reward, a number that says how good that was. The agent's job is to find a way of acting, a policy, that collects as much reward as possible in the long run. The difficulty is that rewards can come long after the actions that earned them: a chess move is only rewarded or punished when the game ends, many moves later. Working out which earlier choices deserve the credit is the central problem.

Markov decision processes

The mathematics of reinforcement learning is the Markov decision process, introduced by Richard Bellman in the 1950s. It has states (here, the cells of the grid), actions (up, right, down, left), transition probabilities (where an action from a state can lead; on ice, sometimes sideways) and rewards (a small cost per move, a reward at the goal, a penalty in a pit). Markov means that what happens next depends only on the current state and action, not on how the agent got there. An episode runs from the start until a terminal state such as the goal. The return from a time step is the sum of the rewards that follow, each discounted by γ for every step it lies in the future: G = r₁ + γ r₂ + γ² r₃ + … With γ below 1, sooner is better than later, and the sum stays finite even without an end.

Values and the Bellman equation

The value of a state under a policy is the return expected from it, and the action value Q(s, a) is the return expected after taking action a in state s and following the policy afterwards. Bellman noticed that values satisfy a simple recursive relation: the value of a state is the expected immediate reward plus the discounted value of the state that follows. For the best policy this becomes the Bellman optimality equation, V*(s) = maxₐ Σ p(s′ | s, a) [r + γ V*(s′)]. Once the optimal values are known, acting well is easy: in each state take the action with the highest value. In a grid world the values form a landscape that rises towards the goal, and the policy simply climbs it.

Value iteration: planning with a model

If the transition probabilities and rewards are known, the optimal values can be computed without any trial and error. Value iteration starts with all values at zero and repeatedly applies the Bellman optimality equation to every state. After the first sweep only the cells next to the goal know about it; after the second, the cells two steps away; the information spreads outward like a wave until the values stop changing. This is dynamic programming, and it is guaranteed to converge for γ below 1, or for γ = 1 when every policy eventually ends. It needs a model of the world, which real problems rarely provide.

Temporal-difference learning: Q-learning and SARSA

Without a model, the agent must learn from samples of experience. Temporal-difference learning, developed by Richard Sutton in the 1980s, updates a guess from another guess: after each step the agent compares its old estimate Q(s, a) with the reward r it received plus the discounted estimate of where it landed, and moves the old estimate a fraction α of the way: Q(s, a) ← Q(s, a) + α [r + γ Q(s′, a′) − Q(s, a)]. The bracket is the TD error. SARSA, named after the five values it uses (s, a, r, s′, a′), takes a′ to be the action the agent actually takes next. Q-learning, Chris Watkins' method of 1989, uses the best action instead, maxₐ′ Q(s′, a′), whatever the agent does next. Expected SARSA uses the average over the actions the policy might take. With enough exploration and a suitably shrinking α, Q-learning is proven to converge to the optimal values.

Exploration, exploitation and the cliff

An agent that always takes the action that currently looks best may never find out that another one is better. The simplest remedy is ε-greedy: with probability ε pick a random action, otherwise the best-looking one. Sutton and Barto's cliff-walking example shows how this choice interacts with the learning rule. The start and the goal are at the two ends of a cliff edge; stepping off costs 100 and sends the agent back to the start. Q-learning learns the values of the greedy policy, so it finds the shortest path right along the edge, but while it still explores, an occasional random step takes it over the cliff, and its reward per episode is poor. SARSA learns the value of what it really does, random steps included, so it learns that walking next to the edge is dangerous and takes a safer path one or two rows up, earning more along the way. If ε is lowered to zero over time, both end up on the shortest path.

Beyond the grid

A table with one value per state and action works for a grid of a hundred cells, but not for backgammon, Go or a robot arm, whose states cannot be listed. There the table is replaced by a function approximator, usually a neural network. Gerald Tesauro's TD-Gammon learned to play backgammon at world-class level by temporal-difference learning in 1992; DeepMind's Deep Q-Network learned dozens of Atari games from the pixels in 2015; AlphaGo and AlphaZero combined reinforcement learning with tree search to beat the best human players at Go and chess; and reinforcement learning from human feedback is used to fine-tune large language models. The ideas are the ones in the grid: states, actions, rewards, values, the Bellman equation and the balance between exploring and exploiting.

Поширені запитання

Is reinforcement learning the same as trial and error?

Trial and error is part of it, but reinforcement learning also remembers and generalises what each trial taught. The agent keeps estimates of how good each action is in each state and improves them after every step, so the results of one trial change how it acts in the next, and good outcomes propagate back to the earlier choices that led to them.

What is the difference between on-policy and off-policy learning?

An on-policy method such as SARSA learns the value of the policy it is following, exploration included. An off-policy method such as Q-learning learns the value of a different policy, usually the greedy one, from experience generated by an exploring policy. Off-policy learning can learn from old data or from watching others, but it can be less stable; on-policy learning is safer while learning, as the cliff example shows.

Why use a discount factor?

A discount factor below 1 makes rewards that come sooner worth more than rewards that come later, which matches many real goals, keeps the return finite in tasks that never end, and makes the mathematics converge. It also shapes behaviour: with γ = 0.9 and no step cost, an agent still prefers the shortest path, because the goal's reward shrinks by 10 % with every extra step.

How long does Q-learning take to learn?

In a small grid, a few hundred episodes; in general, the number of experiences needed grows with the number of states and actions and with how rarely the rewarding outcomes are found. Sparse rewards, such as a single goal far away, make learning slow because random exploration seldom reaches it. Shaping rewards, better exploration and function approximation all help in larger problems.

Готові спробувати?

Відкрийте PictorX у браузері — без встановлення і без кредитної картки.

Open the reinforcement learning gridworld