Evaluate the policy, e.g. save/print progress. :param epoch: :return:
(self, epoch)
| 596 | pass |
| 597 | |
| 598 | def evaluate(self, epoch): |
| 599 | """ |
| 600 | Evaluate the policy, e.g. save/print progress. |
| 601 | :param epoch: |
| 602 | :return: |
| 603 | """ |
| 604 | statistics = OrderedDict() |
| 605 | try: |
| 606 | statistics.update(self.eval_statistics) |
| 607 | self.eval_statistics = None |
| 608 | except Exception as e: |
| 609 | print("No Stats to Eval", str(e)) |
| 610 | |
| 611 | logger.log("Collecting samples for evaluation") |
| 612 | test_paths = self.eval_sampler.obtain_samples() |
| 613 | |
| 614 | statistics.update( |
| 615 | eval_util.get_generic_path_information( |
| 616 | test_paths, |
| 617 | stat_prefix="Test", |
| 618 | ) |
| 619 | ) |
| 620 | statistics.update( |
| 621 | eval_util.get_generic_path_information( |
| 622 | self._exploration_paths, |
| 623 | stat_prefix="Exploration", |
| 624 | ) |
| 625 | ) |
| 626 | |
| 627 | if hasattr(self.env, "log_diagnostics"): |
| 628 | self.env.log_diagnostics(test_paths) |
| 629 | if hasattr(self.env, "log_statistics"): |
| 630 | statistics.update(self.env.log_statistics(test_paths)) |
| 631 | if int(epoch) % self.freq_log_visuals == 0: |
| 632 | if hasattr(self.env, "log_visuals"): |
| 633 | self.env.log_visuals(test_paths, epoch, logger.get_snapshot_dir()) |
| 634 | |
| 635 | average_returns = eval_util.get_average_returns(test_paths) |
| 636 | statistics["AverageReturn"] = average_returns |
| 637 | for key, value in statistics.items(): |
| 638 | try: |
| 639 | logger.record_tabular(key, np.mean(value)) |
| 640 | except Exception: |
| 641 | print(f"Log error with key: {key}, value: {value}") |
| 642 | |
| 643 | best_statistic = statistics[self.best_key] |
| 644 | data_to_save = {"epoch": epoch, "statistics": statistics} |
| 645 | data_to_save.update(self.get_epoch_snapshot(epoch)) |
| 646 | if self.save_epoch: |
| 647 | logger.save_extra_data(data_to_save, "epoch{}.pkl".format(epoch)) |
| 648 | print("\n\nSAVED MODEL AT EPOCH {}\n\n".format(epoch)) |
| 649 | if best_statistic > self.best_statistic_so_far: |
| 650 | self.best_statistic_so_far = best_statistic |
| 651 | if self.save_best and epoch >= self.save_best_starting_from_epoch: |
| 652 | data_to_save = {"epoch": epoch, "statistics": statistics} |
| 653 | data_to_save.update(self.get_epoch_snapshot(epoch)) |
| 654 | logger.save_extra_data(data_to_save, "best.pkl") |
| 655 | print("\n\nSAVED BEST\n\n") |
no test coverage detected