调整控制台输出(报告器)#

默认情况下,Tune 会定期将实验进度报告给命令行,如下所示。

== Status ==
Memory usage on this node: 11.4/16.0 GiB
Using FIFO scheduling algorithm.
Resources requested: 4/12 CPUs, 0/0 GPUs, 0.0/3.17 GiB heap, 0.0/1.07 GiB objects
Result logdir: /Users/foo/ray_results/myexp
Number of trials: 4 (4 RUNNING)
+----------------------+----------+---------------------+-----------+--------+--------+--------+--------+------------------+-------+
| Trial name           | status   | loc                 |    param1 | param2 | param3 |    acc |   loss |   total time (s) |  iter |
|----------------------+----------+---------------------+-----------+--------+--------+--------+--------+------------------+-------|
| MyTrainable_a826033a | RUNNING  | 10.234.98.164:31115 | 0.303706  | 0.0761 | 0.4328 | 0.1289 | 1.8572 |          7.54952 |    15 |
| MyTrainable_a8263fc6 | RUNNING  | 10.234.98.164:31117 | 0.929276  | 0.158  | 0.3417 | 0.4865 | 1.6307 |          7.0501  |    14 |
| MyTrainable_a8267914 | RUNNING  | 10.234.98.164:31111 | 0.068426  | 0.0319 | 0.1147 | 0.9585 | 1.9603 |          7.0477  |    14 |
| MyTrainable_a826b7bc | RUNNING  | 10.234.98.164:31112 | 0.729127  | 0.0748 | 0.1784 | 0.1797 | 1.7161 |          7.05715 |    14 |
+----------------------+----------+---------------------+-----------+--------+--------+--------+--------+------------------+-------+

请注意,如果列完全为空,它们将被隐藏。可以通过实例化 CLIReporter 实例(如果你使用的是 jupyter notebook,则为 JupyterNotebookReporter)来以各种方式配置输出。以下是一个示例:

from ray.train import RunConfig
from ray.tune import CLIReporter

# Limit the number of rows.
reporter = CLIReporter(max_progress_rows=10)
# Add a custom metric column, in addition to the default metrics.
# Note that this must be a metric that is returned in your training results.
reporter.add_metric_column("custom_metric")
tuner = tune.Tuner(my_trainable, run_config=RunConfig(progress_reporter=reporter))
results = tuner.fit()

扩展 CLIReporter 可以让你控制报告频率。例如:

from ray.tune.experiment.trial import Trial

class ExperimentTerminationReporter(CLIReporter):
    def should_report(self, trials, done=False):
        """Reports only on experiment termination."""
        return done

tuner = tune.Tuner(my_trainable, run_config=RunConfig(progress_reporter=ExperimentTerminationReporter()))
results = tuner.fit()

class TrialTerminationReporter(CLIReporter):
    def __init__(self):
        super(TrialTerminationReporter, self).__init__()
        self.num_terminated = 0

    def should_report(self, trials, done=False):
        """Reports only on trial termination events."""
        old_num_terminated = self.num_terminated
        self.num_terminated = len([t for t in trials if t.status == Trial.TERMINATED])
        return self.num_terminated > old_num_terminated

tuner = tune.Tuner(my_trainable, run_config=RunConfig(progress_reporter=TrialTerminationReporter()))
results = tuner.fit()

默认的报告样式也可以通过直接扩展 ProgressReporter 接口来更广泛地覆盖。请注意,您可以打印到任何输出流、文件等。

from ray.tune import ProgressReporter

class CustomReporter(ProgressReporter):

    def should_report(self, trials, done=False):
        return True

    def report(self, trials, *sys_info):
        print(*sys_info)
        print("\n".join([str(trial) for trial in trials]))

tuner = tune.Tuner(my_trainable, run_config=RunConfig(progress_reporter=CustomReporter()))
results = tuner.fit()

报告器接口 (tune.ProgressReporter)#

ProgressReporter

实验进度报告的抽象类。

ProgressReporter.report

跨试验报告进度。

ProgressReporter.should_report

返回是否应报告进度。

调整内置报告器#

CLIReporter

命令行报告器

JupyterNotebookReporter

Jupyter notebook 友好的 Reporter,可以在原地更新显示。