Paul's Python CoursePPC

Project 9 of 12, for Neil

CartPole with DQN

Q-values from a small network instead of a table, kept steady by a replay buffer and a frozen target network: built on the racing grid, then taken home to balance Gymnasium's CartPole, which Neil calls a rocket landing in miniature.

About 4 hours, in 14 steps.

Sign up free to open itLog in

Act 8 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • NeilRight now the crab's Q-values live in a table, with a number for every tile and every move.
  • NeilA network could hold them instead, and cope with worlds far too big for any table.
  • AaeshaAnd tiles in the same row would learn from each other, which is how brains generalise. A table can't do that.
  • PaulThe trouble is a network that sets its own homework wobbles a lot, but there are two old tricks for that.
  • NeilThen we'll do the standard test, a pole balanced on a cart. It's basically a booster landing on its tail, only tiny.

First, in the workshop, write dqn.py, Deep Q on the racing grid in track.py. A memory is (tile, action, reward, after, over), with action a number from 0 to 3, in the order of ACTIONS.

  1. features(tile): seven numbers, a 1 for the tile's row among three, then a 1 for its column among four.
  2. q_values(params, X): a ReLU hidden layer, then four Q-values, for every row of X at once.
  3. ReplayBuffer(capacity): add(tile, action, reward, after, over); len(buffer); sample(size, rng), a list of size different memories picked at random. Past capacity, the oldest memory goes.
  4. targets(target_params, rewards, X_after, overs, gamma=0.9): the reward, plus gamma times the frozen copy's best Q-value after the move, unless the move ended the race.
  5. train_step(params, target_params, batch, gamma=0.9, rate=0.05): one gradient step on a batch of memories. Returns the loss before the step.
  6. epsilon_at(episode, decay=100): 1.0 at race 0, falling in a straight line to 0.05 at race decay, then 0.05.
  7. copy_params(params): a frozen copy that training doesn't touch.
  8. train(episodes, rng): epsilon-greedy races; every move remembered; once there are 32 memories, one train_step per move on a batch picked at random; the frozen copy refreshed every 50 moves.

Run as a program, it trains for 150 races, then shows the network's line and races it 1000 times, against a crab that moves at random.

Then, at home, cartpole_dqn.py: the same DQN on Gymnasium's CartPole-v1, four numbers in and two actions out, with a replay buffer of 50,000 steps and a target network refreshed every 500. Trained for 100,000 steps, it must balance the pole for at least 195 steps on average, over 20 episodes it never trained on.

What it prints
The network's line
> > > F
^ # ^ P
^ > ^ v
Average return over 1000 races: 1.02
At random: -9.18

(.venv) $ python cartpole_dqn.py
Pushing at random: 21.7 steps, on average
The network: 500.0 steps of 500, on average

What you learn

What's in it

  1. A rule instead of a table
  2. Training on targets
  3. What the move should have been worth
  4. A memory, sampled at random
  5. A frozen copy for the targets
  6. Is it frozen?
  7. Explore less as it learns
  8. How Paul would do it
  9. Fix Riya's version
  10. Make Deep Q
  11. The frozen copy
  12. CartPole, at home
  13. Ship it