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.
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.
features(tile): seven numbers, a 1 for the tile's row among three, then a 1 for its column among four.q_values(params, X): a ReLU hidden layer, then four Q-values, for every row ofXat once.ReplayBuffer(capacity):add(tile, action, reward, after, over);len(buffer);sample(size, rng), a list ofsizedifferent memories picked at random. Pastcapacity, the oldest memory goes.targets(target_params, rewards, X_after, overs, gamma=0.9): the reward, plusgammatimes the frozen copy's best Q-value after the move, unless the move ended the race.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.epsilon_at(episode, decay=100): 1.0 at race 0, falling in a straight line to 0.05 at racedecay, then 0.05.copy_params(params): a frozen copy that training doesn't touch.train(episodes, rng): epsilon-greedy races; every move remembered; once there are 32 memories, onetrain_stepper 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.
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
- A rule instead of a table
- Training on targets
- What the move should have been worth
- A memory, sampled at random
- A frozen copy for the targets
- Is it frozen?
- Explore less as it learns
- How Paul would do it
- Fix Riya's version
- Make Deep Q
- The frozen copy
- CartPole, at home
- Ship it