AI Beyond Language Models · Learning from Rewards

Q-Learning: Learning from Rewards

Nobody tells the robot the right moves. It only gets a reward at the goal, and over a few tries the value of that reward flows backwards along the corridor.

q_learning.py

# A robot in a corridor: cell 0, 1, 2, then the goal. Reaching the goal is worth 10.
# It learns how good "move right" is from each cell, by trying it.
cells = ["cell 0", "cell 1", "cell 2", "goal"]
values = [0.0, 0.0, 0.0, 0.0]
alpha, gamma = 0.5, 0.9   # learning rate, and how much a future reward counts

for episode in range(3):
    cell = 0
    while cell < 3:
        values[cell] = round(values[cell] + alpha * ((10 if cell == 2 else 0) + gamma * values[cell + 1] - values[cell]), 2)
        cell += 1
    print("episode", episode, values)

Step by step

  1. A corridor: three cells, then the goal.
  2. The robot's value for "move right" in each cell. All 0: it knows nothing yet.
  3. alpha: how far each update moves. gamma: a reward one step away is worth 0.9 of a reward now.
  4. Episode 0: the first try.
  5. Start in cell 0.
  6. Python checks cell < 3 to decide whether the loop keeps going.
  7. Update cell 0: no reward here, and the next cell's value is 0. Nothing to learn yet.
  8. cell changes from 0 to 1.
  9. Python checks cell < 3 to decide whether the loop keeps going.
  10. Line runs: values[cell] = round(values[cell] + alpha * ((10 if cell == 2 else 0) + gamma * values[cell + 1] - values[cell]), 2).
  11. cell changes from 1 to 2.
  12. Python checks cell < 3 to decide whether the loop keeps going.
  13. Cell 2: moving right reaches the goal, reward 10. Its value moves halfway there: 5.
  14. cell changes from 2 to 3.
  15. Python checks cell < 3 to decide whether the loop keeps going.
  16. It prints episode 0 [0.0, 0.0, 5.0, 0.0].
  17. Episode 1: walk the corridor again.
  18. cell changes from 3 to 0.
  19. Python checks cell < 3 to decide whether the loop keeps going.
  20. Line runs: values[cell] = round(values[cell] + alpha * ((10 if cell == 2 else 0) + gamma * values[cell + 1] - values[cell]), 2).
  21. cell changes from 0 to 1.
  22. Python checks cell < 3 to decide whether the loop keeps going.
  23. Cell 1: no reward, but the next cell is now worth 5. 0.5 × (0.9 × 5) = 2.25. The reward has started to flow back.
  24. cell changes from 1 to 2.
  25. Python checks cell < 3 to decide whether the loop keeps going.
  26. Cell 2: 5 + 0.5 × (10 − 5) = 7.5.
  27. cell changes from 2 to 3.
  28. Python checks cell < 3 to decide whether the loop keeps going.
  29. It prints episode 1 [0.0, 2.25, 7.5, 0.0].
  30. Episode 2.
  31. cell changes from 3 to 0.
  32. Python checks cell < 3 to decide whether the loop keeps going.
  33. Cell 0 finally learns: 0.5 × 0.9 × 2.25 = 1.01.
  34. cell changes from 0 to 1.
  35. Python checks cell < 3 to decide whether the loop keeps going.
  36. Cell 1: 4.5.
  37. cell changes from 1 to 2.
  38. Python checks cell < 3 to decide whether the loop keeps going.
  39. Cell 2: 8.75, closing in on 10.
  40. cell changes from 2 to 3.
  41. Python checks cell < 3 to decide whether the loop keeps going.
  42. It prints episode 2 [1.01, 4.5, 8.75, 0.0].
  43. After three tries, every cell knows "right" is worth something, and more the closer it is to the goal. A real agent also stores a value for every other move, sometimes tries random moves, and repeats until the values settle.