MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / train

Method train

numpy_ml/rl_models/trainer.py:33–101  ·  view source on GitHub ↗

Train an agent on an OpenAI gym environment, logging training statistics along the way. Parameters ---------- n_episodes : int The number of episodes to train the agent across. max_steps : int The maximum number of steps the a

(
        self,
        n_episodes,
        max_steps,
        seed=None,
        plot=True,
        verbose=True,
        render_every=None,
        smooth_factor=0.05,
    )

Source from the content-addressed store, hash-verified

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
63 values correspond to less smoothing.
64 """
65 if seed:
66 np.random.seed(seed)
67 self.env.seed(seed=seed)
68
69 t0 = time()
70 render_every = np.inf if render_every is None else render_every
71 sf = smooth_factor
72
73 for ep in range(n_episodes):
74 tot_rwd, duration, n_steps = self._train_episode(max_steps)
75 smooth_tot = tot_rwd if ep == 0 else (1 - sf) * smooth_tot + sf * tot_rwd
76
77 if verbose:
78 fstr = "[Ep. {:2}] {:<6.2f} Steps | Total Reward: {:<7.2f}"
79 fstr += " | Smoothed Total: {:<7.2f} | Duration: {:<6.2f}s"
80 print(fstr.format(ep + 1, n_steps, tot_rwd, smooth_tot, duration))
81
82 if (ep + 1) % render_every == 0:
83 fstr = "\tGreedy policy total reward: {:.2f}, n_steps: {:.2f}"
84 total, n_steps = self.agent.greedy_policy(max_steps)
85 print(fstr.format(total, n_steps))
86
87 self.rewards["total"].append(tot_rwd)
88 self.rewards["n_steps"].append(n_steps)
89 self.rewards["duration"].append(duration)
90 self.rewards["smooth_total"].append(smooth_tot)

Callers 4

test_cross_entropy_agentFunction · 0.95
test_monte_carlo_agentFunction · 0.95
test_dyna_agentFunction · 0.95

Calls 3

_train_episodeMethod · 0.95
plot_rewardsMethod · 0.95
greedy_policyMethod · 0.45

Tested by 4

test_cross_entropy_agentFunction · 0.76
test_monte_carlo_agentFunction · 0.76
test_dyna_agentFunction · 0.76