Robot Learning, step 6
Learning to throw
Train a throw into the cup from its own attempts with the REINFORCE policy gradient, and see how a baseline tames the gradient's noise.
Reward alone
Cloning needed an expert and Q-learning a small table of states. The throw has neither. Three numbers = [aim, power, release] decide a throw, and the only feedback is the reward: minus the distance (m) from the cup's centre to where the ball comes down. rl.rollout(theta) simulates a throw off-screen and returns its reward; rl.throw(theta) throws the real ball, once.
A random policy, and its gradient
REINFORCE makes the policy random: , a Gaussian around a mean with the same spread in every parameter. It climbs the expected reward by gradient ascent. can't be differentiated, but the policy's log-probability can: , the score. That gives
for samples with rewards . Samples that did better than pull towards them; worse ones push it away. Then , with learning rate .
The baseline
Any constant leaves the expectation alone, because , but it changes the noise. With every throw misses, so every is negative and every sample pushes away from itself, good or bad; only the small differences between rewards carry information, buried under that push. The baseline = the batch's mean reward keeps just the differences. The Plots tab shows 20 estimates at , made from the same throws both ways.
Your task
- Write
policy_gradient(thetas, rewards, mu, sigma, baseline), with =rewards.mean()ifbaselineelse 0. - Write
reinforce(rollout, mu, baseline).ITERStimes: drawBATCHsamplesmu + SIGMA * np.random.randn(BATCH, 3), roll each out withrollout, stepmu += lr * gand thenlr *= DECAY, withlrstarting atLR. Returnmu.
The program trains twice from with the same random numbers, without and with the baseline, and throws the real ball with the baseline's . It must land in the cup.