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

Method plot_rewards

numpy_ml/rl_models/trainer.py:103–148  ·  view source on GitHub ↗

Plot the cumulative reward per episode as a function of episode number. Notes ----- Saves plot to the file ``./img/ - .png`` Parameters ---------- rwd_greedy : float The cumulative reward earned with a final execution o

(self, rwd_greedy)

Source from the content-addressed store, hash-verified

101 self.plot_rewards(rwd_greedy)
102
103 def plot_rewards(self, rwd_greedy):
104 """
105 Plot the cumulative reward per episode as a function of episode number.
106
107 Notes
108 -----
109 Saves plot to the file ``./img/<agent>-<env>.png``
110
111 Parameters
112 ----------
113 rwd_greedy : float
114 The cumulative reward earned with a final execution of a greedy
115 target policy.
116 """
117 try:
118 import matplotlib.pyplot as plt
119 import seaborn as sns
120
121 # https://seaborn.pydata.org/generated/seaborn.set_context.html
122 # https://seaborn.pydata.org/generated/seaborn.set_style.html
123 sns.set_style("white")
124 sns.set_context("notebook", font_scale=1)
125 except:
126 fstr = "Error importing `matplotlib` and `seaborn` -- plotting functionality is disabled"
127 raise ImportError(fstr)
128
129 R = self.rewards
130 fig, ax = plt.subplots()
131 x = np.arange(len(R["total"]))
132 y = R["smooth_total"]
133 y_raw = R["total"]
134
135 ax.plot(x, y, label="smoothed")
136 ax.plot(x, y_raw, alpha=0.5, label="raw")
137 ax.axhline(y=rwd_greedy, xmin=min(x), xmax=max(x), ls=":", label="final greedy")
138 ax.legend()
139 sns.despine()
140
141 env = self.agent.env_info["id"]
142 agent = self.agent.hyperparameters["agent"]
143
144 ax.set_xlabel("Episode")
145 ax.set_ylabel("Cumulative reward")
146 ax.set_title("{} on '{}'".format(agent, env))
147 plt.savefig("img/{}-{}.png".format(agent, env))
148 plt.close("all")

Callers 1

trainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected