action,\n# solved in closed form by least squares. Add a bias column so it can shift.\ndef fit_linear(Xtr, Ytr):\n A = np.concatenate([Xtr, np.ones((Xtr.shape[0],1))], axis=1)\n Wb, *_ = np.linalg.lstsq(A, Ytr, rcond=None) # (8,3)\n return Wb\ndef pred_linear(Wb, Xq):\n A = np.concatenate([Xq, np.ones((Xq.shape[0],1))], axis=1)\n return A @ Wb\nWb = fit_linear(X[tr], act[tr])\nval_mse = float(((pred_linear(Wb, X[val]) - act[val])**2).mean())\nbase = float(((act[val] - act[tr].mean(0))**2).mean())\nprint(f\"linear BC held-out MSE {val_mse:.2e} baseline {base:.2e} \"\n f\"variance explained {100*(1-val_mse/base):.1f}%\")","label":"Least-squares behavior cloning"},{"code":"# A linear policy already beats the mean baseline by a lot. It's our floor.\nassert val_mse < 0.35*base, \"linear BC should clearly beat the mean baseline\"\nprint(\"PASS - a linear policy, learned from data, beats the hand baseline.\")","label":"It beats the baseline"}],"intro":"Fit a linear behavior-cloning policy with least squares and evaluate it on held-out episodes.","key":"robot-learning/linear-behavior-cloning","kind":"python","title":"Linear Behavior Cloning"}">
PYTHON · NUMPY · IN-BROWSER

Linear Behavior Cloning

Fit a linear behavior-cloning policy with least squares and evaluate it on held-out episodes.

You read

the arrays and values already in scope

You change

the code you write in each cell

Fixed

the dataset and the checks that grade you