| 3 | |
| 4 | |
| 5 | class Trainer(object): |
| 6 | def __init__(self, agent, env): |
| 7 | """ |
| 8 | An object to facilitate agent training and evaluation. |
| 9 | |
| 10 | Parameters |
| 11 | ---------- |
| 12 | agent : :class:`AgentBase` instance |
| 13 | The agent to train. |
| 14 | env : ``gym.wrappers`` or ``gym.envs`` instance |
| 15 | The environment to run the agent on. |
| 16 | """ |
| 17 | self.env = env |
| 18 | self.agent = agent |
| 19 | self.rewards = {"total": [], "smooth_total": [], "n_steps": [], "duration": []} |
| 20 | |
| 21 | def _train_episode(self, max_steps, render_every=None): |
| 22 | t0 = time() |
| 23 | if "train_episode" in dir(self.agent): |
| 24 | # online training updates over the course of the episode |
| 25 | reward, n_steps = self.agent.train_episode(max_steps) |
| 26 | else: |
| 27 | # offline training updates upon completion of the episode |
| 28 | reward, n_steps = self.agent.run_episode(max_steps) |
| 29 | self.agent.update() |
| 30 | duration = time() - t0 |
| 31 | return reward, duration, n_steps |
| 32 | |
| 33 | def train( |
| 34 | self, |
| 35 | n_episodes, |
| 36 | max_steps, |
| 37 | seed=None, |
| 38 | plot=True, |
| 39 | verbose=True, |
| 40 | render_every=None, |
| 41 | smooth_factor=0.05, |
| 42 | ): |
| 43 | """ |
| 44 | Train an agent on an OpenAI gym environment, logging training |
| 45 | statistics along the way. |
| 46 | |
| 47 | Parameters |
| 48 | ---------- |
| 49 | n_episodes : int |
| 50 | The number of episodes to train the agent across. |
| 51 | max_steps : int |
| 52 | The maximum number of steps the agent can take on each episode. |
| 53 | seed : int or None |
| 54 | A seed for the random number generator. Default is None. |
| 55 | plot : bool |
| 56 | Whether to generate a plot of the cumulative reward as a function |
| 57 | of training episode. Default is True. |
| 58 | verbose : bool |
| 59 | Whether to print intermediate run statistics to stdout during |
| 60 | training. Default is True. |
| 61 | smooth_factor : float in [0, 1] |
| 62 | The amount to smooth the cumulative reward across episodes. Larger |
no outgoing calls