Reinforcement learning in a grid

AI from scratch · 18 of a series

Reinforcement learning in a grid: lesson transcript

1. No dataset, only a world

Everything until now learned from examples with answers attached. This is different. A small world: an agent that starts in the corner, a goal worth +1, two pits worth −1, some walls, and a tiny cost for every step. Nobody tells the agent the way. It has to find out by walking, falling in, and trying again.

2. Four numbers per square

The agent keeps a table: for every square, four numbers, one per direction, meaning "how good is it to go that way from here". Call them Q. They all start at zero, so the agent has no opinion and wanders. Here is its first walk. It bumps into walls, wastes steps, and eventually stumbles somewhere that ends the episode.

3. Reward trickles backwards

When a step lands somewhere valuable, the number for the step that got it there is nudged up: a little of the reward, plus a share of the best number available from the new square. So value spreads one square per visit, backwards from the goal. After a few episodes the squares next to the goal glow; a few more and the glow reaches further out.

4. Watch it learn

The scrub bar is the episode counter. Blue is value, the arrows are the best-known direction, and the green trail is the current walk. Early walks are long and end in pits as often as at the goal. As the table fills in, the walks get shorter and straighter. On the right, steps per episode falling.

5. Explore or exploit

Each step the agent either takes its best-known direction or, with probability ε, a random one. Set ε to zero and it commits to the first route that ever worked, even a terrible one. Set it high and it keeps stumbling into pits long after it knows better. Every setting retrains from scratch; scrub to watch.

6. How far ahead it cares

γ is how much a reward is worth if it is one step further away. Near 1 and a distant goal still lights up the whole path back to the start. Small and the glow fades within a few squares, so far-off squares look worthless and the agent has nothing to follow until it is nearly there. Real tasks tune this: chess needs patience, a reflex does not.

7. Name it: reinforcement learning

Act, see what happens, adjust the numbers, repeat: this is *reinforcement learning*, and the table trick is Q-learning. Replace the table with a network that takes the square as input and outputs the four numbers, train that network with gradient descent on the same update, and you have the recipe behind game-playing agents. Replace "square" with "conversation so far" and reward with human ratings, and you have how chat models are tuned after pretraining.

8. Did it land?

A route that works, and no exploration.

Check your understanding

The agent has found a long, safe route to the goal. You now set ε to 0. Will it ever discover the shorter route?

The table only changes for squares the agent visits. With no random moves it repeats the known route forever, and the squares on the short route keep their zeros. Some exploration is the price of ever finding something better than what you already know, which is the whole tension in the ε slider.