Beispiel #1
0
 def compare(self, runs_directories_a, runs_directories_b):
     runs_a = {
         self.suffix(directory): MonitorV2.load_results(directory)
         for directory in runs_directories_a
     }
     runs_b = {
         self.suffix(directory): MonitorV2.load_results(directory)
         for directory in runs_directories_b
     }
     f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
     self.plot_all(runs_a,
                   field='episode_rewards',
                   title='rewards',
                   axes=ax1)
     self.plot_all(runs_b,
                   field='episode_rewards',
                   title='rewards',
                   axes=ax2)
     plt.show()
    def get_run_dataframe(self,
                          directory,
                          agent_name='',
                          gamma=0.95,
                          subsample=10):
        run_data = MonitorV2.load_results(directory)
        if not run_data:
            return pd.DataFrame()

        # Common fields
        data = {
            "episode":
            np.arange(np.size(run_data["episode_rewards"])),
            "total reward":
            run_data["episode_rewards"],
            "discounted rewards": [
                np.sum([episode[t] * gamma**t for t in range(len(episode))])
                for episode in run_data["episode_rewards_"]
            ],
            "length":
            run_data["episode_lengths"],
        }

        # Additional highway-env fields
        try:
            dt = 1.0
            data.update({
                "crashed":
                [np.any(episode) for episode in run_data["episode_crashed"]],
                "velocity":
                [np.mean(episode) for episode in run_data["episode_velocity"]],
                "distance": [
                    np.sum(episode) * dt
                    for episode in run_data["episode_velocity"]
                ],
            })
        except KeyError as e:
            print(e)

        # Tags
        df = pd.DataFrame(data)
        df["run"] = str(directory.name)
        df["agent"] = agent_name

        # Filtering
        for field in [
                "total reward", "discounted rewards", "length", "crashed",
                "velocity", "distance"
        ]:
            df[field] = df[field].rolling(subsample).mean()

        # Subsample
        df = df.iloc[self.episodes_range[0]:self.episodes_range[1]:subsample]
        return df
Beispiel #3
0
 def analyze(self, run_directories):
     runs = {self.suffix(directory): MonitorV2.load_results(directory) for directory in run_directories}
     runs = {key: value for (key, value) in runs.items() if value is not None}
     self.plot_all(runs, field='episode_rewards', title='rewards')
     self.histogram_all(runs, field='episode_rewards', title='rewards')
     self.describe_all(runs, field='episode_rewards', title='rewards')
     self.histogram_all(runs, field='episode_lengths', title='lengths')
     self.describe_all(runs, field='episode_lengths', title='lengths')
     self.histogram_all(runs, field='episode_costs', title='costs', preprocess=lambda c: [sum(e) for e in c])
     self.describe_all(runs, field='episode_costs', title='costs', preprocess=lambda c: [sum(e) for e in c])
     self.compare(runs)
     plt.show()