| 42 | |
| 43 | |
| 44 | class Model: |
| 45 | def __init__(self, env): |
| 46 | # fit the featurizer to data |
| 47 | self.env = env |
| 48 | samples = gather_samples(env) |
| 49 | self.featurizer = RBFSampler() |
| 50 | self.featurizer.fit(samples) |
| 51 | dims = self.featurizer.n_components |
| 52 | |
| 53 | # initialize linear model weights |
| 54 | self.w = np.zeros(dims) |
| 55 | |
| 56 | def predict(self, s, a): |
| 57 | sa = np.concatenate((s, [a])) |
| 58 | x = self.featurizer.transform([sa])[0] |
| 59 | return x @ self.w |
| 60 | |
| 61 | def predict_all_actions(self, s): |
| 62 | return [self.predict(s, a) for a in range(self.env.action_space.n)] |
| 63 | |
| 64 | def grad(self, s, a): |
| 65 | sa = np.concatenate((s, [a])) |
| 66 | x = self.featurizer.transform([sa])[0] |
| 67 | return x |
| 68 | |
| 69 | |
| 70 | def test_agent(model, env, n_episodes=20): |
no outgoing calls