Robot Learning, step 4
Copying an expert
Fit a reaching policy to an expert's demonstrations by least squares, and see why the features that fit the data best park the arm short of the target.
An expert to copy
Q-learning needed hundreds of thousands of moves. Often there's an expert to watch instead: a person with a joystick or, here, the damped least-squares controller from Jacobians & Velocity Control. rl.expert_demos(20, seed=0) records 20 of its reaches. Each row of obs (N × 7) is an observation , the joint angles (rad) and then = target − gripper (m); the same row of actions (N × 4) is the expert's joint velocity (rad/s). Behaviour cloning fits a policy to those pairs: plain supervised learning.
Least squares
Take a linear policy : features , numbers worked out from , times weights (). Stack every row's features into () and the actions into (). Least squares picks the that minimises , the sum of the squared action errors. Setting its gradient to zero gives the normal equations
a system: np.linalg.solve(X.T @ X, X.T @ Y).
A better fit isn't a better controller
The program fits two feature sets. The given features_everything is the whole observation and a constant 1. Yours, features_error, is alone. Everything contains the error, so it fits the demonstrations at least as well: its training RMS (root mean square action error) is lower. But on the target, where , its terms and the 1 don't vanish, so the policy keeps commanding a little motion, and the arm settles where the error terms cancel it: short of the target. With error features the policy is exactly zero there, as the expert is. Only running the policy, closed loop, shows that.
Your task
- Write
least_squares(X, Y)by the normal equations. - Write
features_error(obs). - Write
fit(obs, actions, features): build , solve for , and returnpolicy(o), which givesfeatures(o) @ W.
The arm then reaches for 6 new targets with everything and 10 with your features, 2 s each; the Plots tab shows the distance. 8 of your 10 must end within 1 cm.