Project 10 of 12, for Aaesha
Policy Gradients
The law of effect, in code: REINFORCE on the racing grid, a softmax policy nudged towards whatever went well, and a baseline that steadies every nudge.
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
- AaeshaWe've had values, Q-values, targets and frozen copies, and I've got a simpler question.
- AaeshaOver a century ago Thorndike watched cats learn puzzle boxes, and whatever worked, they did more often.
- AaeshaOn each tile, what should the crab actually do? I don't mean what it's worth, I mean what to do.
- PaulThen let's learn that and nothing else, and just do whatever went well more often.
Write reinforce.py, REINFORCE on the racing grid in track.py. The crab's policy is theta, an array of shape (3, 4, 4): four preferences for every tile, in the order of ACTIONS.
chances(theta, tile): the softmax of the tile's four preferences.grad_log_chance(theta, tile, action): the gradient of the log of that move's chance, for every number intheta.race(theta, rng, limit=50): one race from the start, every move drawn from its chances, as a list of(tile, action, reward).returns_to_go(rewards, gamma=0.9): for every step, the discounted return from there to the end.reinforce_gradient(theta, races, gamma=0.9, baseline=None): for every move of every race, its return to go, lessbaseline[tile]if there is a baseline, timesgrad_log_chance; added up, and averaged over the races.train(episodes, rng, rate=0.1, gamma=0.9, use_baseline=True): one race and one step uphill per episode. The baseline is a(3, 4)array, each tile's number nudged 0.1 of the way towards every return that followed it. Returns(theta, baseline).
Run as a program, it compares how much 200 estimates disagree without and with a baseline, then trains for 500 races and races its policy 1000 times.
Spread of 200 estimates at random, no baseline: 39.26 Spread of 200 estimates at random, with a baseline: 16.72 After 500 races with a baseline, its most likely moves > > > F ^ # ^ P ^ < > v Average return over 1000 races: 1.12
What you learn
What's in it
- Preferences, not values
- Chances from preferences
- Whatever went well, more often
- A poor race
- Better or worse than usual
- When every race costs
- Steadier estimates
- How Paul would do it
- Make REINFORCE
- Ship it