Reinforcement learning
Learning by trial and error: an agent acts, the world answers with a reward, and behaviour that pays off is repeated. Seven steps on a 12-cell grid, with every reward added up.
In 30 seconds
Reinforcement learning trains an agent by letting it act and scoring the results. At each step it observes its situation (the state), chooses an action, and receives a number (the reward).
Its aim is the largest total reward over time, not the biggest reward right now. Rewards often arrive late, so the agent must work out which earlier actions deserve the credit.
What it learns is a policy: a rule for what to do in each state. To improve it, the agent must sometimes explore untried actions rather than only repeat what already works.
Reinforcement learning, step by step
Rewards are simply added here; many methods count later rewards for less (a discount factor). The last chart is one simulated run of Q-learning on this grid, with episodes cut off after 30 moves.
- Step 1: Agent and environment. Reinforcement learning has two parts. The agent is the learner and decision-maker. The environment is everything it acts on: a game, a maze, a robot’s surroundings. The agent picks an action, the environment answers with a new situation and a number called the reward, and the cycle repeats. Nobody tells it the right action.
- Step 2: State, action, reward. Make it concrete with a small grid. The state is the situation the agent is in, here simply which of the 12 cells it stands in. The actions are its four moves. The reward is the number handed back after each move: −1 for an ordinary step, +10 for stepping onto the goal, −10 for falling in the pit. Goal and pit both end the run.
- Step 3: One episode, added up. One run from start to an end state is an episode. Here the agent wanders and reaches the goal on its seventh move: six ordinary moves at −1 each, then +10. The total, called the return, is 4. The agent’s aim is not a big reward right now but the largest return over the whole episode.
- Step 4: Delayed reward. The +10 arrives only on the last move, yet every earlier move helped earn it. On its own reward, −1, the first move looks bad. The fix is to credit each state with all the reward that followed it. Counting back from the goal gives 10, 9, 8 and so on, down to 4. This credit problem is the heart of reinforcement learning.
- Step 5: The policy. What the agent learns is a policy: a rule for which action to take in each state. On the grid that is one arrow per cell. Learning means changing arrows so the return goes up. No policy does better here: from the start it reaches the goal in five moves for a return of 6.
- Step 6: Explore or exploit. Suppose the agent only knows its wandering route, worth 4. Exploiting means repeating it. Exploring means trying a move it has not tested: it might find the shorter route worth 6, or step into the pit and end on −13. An agent that never explores stays stuck with 4. A common rule: make a random move a small fraction of the time.
- Step 7: Better by trial and error. Now let a simple learner loose on the grid. It keeps an estimate of the reward still to come after each move, mostly follows the best estimate, and explores one move in ten. Its first episodes are long and costly. Within about ten it finds the 5-move route. The later dips are exploration: slower routes and one fall into the pit.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Q-learningLearning the value of each move by trial, error and one update rule. Advanced
- Monte Carlo tree searchChoosing a move by playing random games and keeping score in a growing tree. Advanced
- RLHFPeople compare answers, a scorer learns their taste, and the model is tuned to match it. Intermediate
Spotted a mistake on this page?