Example #1
0
    def process_eval_package(self, log_writer: SummaryWriter,
                             pkg: LoggingPackage):
        training_steps = pkg.training_steps
        checkpoint_file_name = pkg.checkpoint_file_name
        render = pkg.viz_data
        task_outputs = pkg.metric_dicts

        num_tasks = pkg.num_non_empty_metrics_dicts_added
        metric_means = pkg.metrics_tracker.means()

        mode = pkg.mode

        log_writer.add_scalar(f"{mode}/num_tasks_evaled", num_tasks,
                              training_steps)

        message = [f"{mode} {training_steps} steps:"]
        for k in sorted(metric_means.keys()):
            log_writer.add_scalar(f"{mode}/{k}", metric_means[k],
                                  training_steps)
            message.append(f"{k} {metric_means[k]}")
        message.append(f"tasks {num_tasks} checkpoint {checkpoint_file_name}")
        get_logger().info(" ".join(message))

        if self.visualizer is not None:
            self.visualizer.log(
                log_writer=log_writer,
                task_outputs=task_outputs,
                render=render,
                num_steps=training_steps,
            )
Example #2
0
    def process_train_packages(
        self,
        log_writer: SummaryWriter,
        pkgs: List[LoggingPackage],
        last_steps=0,
        last_offpolicy_steps=0,
        last_time=0.0,
    ):
        assert self.mode == "train"

        current_time = time.time()

        training_steps = pkgs[0].training_steps
        offpolicy_steps = pkgs[0].off_policy_steps
        log_writer.add_scalar(
            tag="train/pipeline_stage",
            scalar_value=pkgs[0].pipeline_stage,
            global_step=training_steps,
        )

        metrics_and_train_info_tracker = ScalarMeanTracker()
        for pkg in pkgs:
            metrics_and_train_info_tracker.add_scalars(
                scalars=pkg.metrics_tracker.means(),
                n=pkg.metrics_tracker.counts())
            metrics_and_train_info_tracker.add_scalars(
                scalars=pkg.train_info_tracker.means(),
                n=pkg.train_info_tracker.counts(),
            )

        message = [
            "train {} steps {} offpolicy:".format(training_steps,
                                                  offpolicy_steps)
        ]
        means = metrics_and_train_info_tracker.means()
        for k in sorted(means.keys(),
                        key=lambda mean_key: ("/" in mean_key, mean_key)):
            if "offpolicy" not in k:
                log_writer.add_scalar("{}/".format(self.mode) + k, means[k],
                                      training_steps)
            else:
                log_writer.add_scalar(k, means[k], training_steps)
            message.append(k + " {:.3g}".format(means[k]))
        message += ["elapsed_time {:.3g}s".format(current_time - last_time)]

        if last_steps > 0:
            fps = (training_steps - last_steps) / (current_time - last_time)
            message += ["approx_fps {:.3g}".format(fps)]
            log_writer.add_scalar("train/approx_fps", fps, training_steps)

        if last_offpolicy_steps > 0:
            fps = (offpolicy_steps - last_offpolicy_steps) / (current_time -
                                                              last_time)
            message += ["offpolicy/approx_fps {:.3g}".format(fps)]
            log_writer.add_scalar("offpolicy/approx_fps", fps, training_steps)

        get_logger().info(" ".join(message))

        return training_steps, offpolicy_steps, current_time
Example #3
0
    def process_test_packages(
        self,
        log_writer: SummaryWriter,
        pkgs: List[LoggingPackage],
        all_results: Optional[List[Any]] = None,
    ):
        mode = pkgs[0].mode
        assert mode == "test"

        training_steps = pkgs[0].training_steps

        all_metrics_tracker = ScalarMeanTracker()
        metric_dicts_list, render, checkpoint_file_name = [], {}, []
        for pkg in pkgs:
            all_metrics_tracker.add_scalars(
                scalars=pkg.metrics_tracker.means(),
                n=pkg.metrics_tracker.counts())
            metric_dicts_list.extend(pkg.metric_dicts)
            if pkg.viz_data is not None:
                render.update(pkg.viz_data)
            checkpoint_file_name.append(pkg.checkpoint_file_name)

        assert all_equal(checkpoint_file_name)

        message = [f"{mode} {training_steps} steps:"]

        metric_means = all_metrics_tracker.means()
        for k in sorted(metric_means.keys()):
            log_writer.add_scalar(f"{mode}/{k}", metric_means[k],
                                  training_steps)
            message.append(k + " {:.3g}".format(metric_means[k]))

        if all_results is not None:
            results = copy.deepcopy(metric_means)
            results.update({
                "training_steps": training_steps,
                "tasks": metric_dicts_list
            })
            all_results.append(results)

        num_tasks = sum(
            [pkg.num_non_empty_metrics_dicts_added for pkg in pkgs])
        log_writer.add_scalar(f"{mode}/num_tasks_evaled", num_tasks,
                              training_steps)

        message.append("tasks {} checkpoint {}".format(
            num_tasks, checkpoint_file_name[0]))
        get_logger().info(" ".join(message))

        if self.visualizer is not None:
            self.visualizer.log(
                log_writer=log_writer,
                task_outputs=metric_dicts_list,
                render=render,
                num_steps=training_steps,
            )