ray.tune.Callback#

class ray.tune.Callback[source]#

Tune base callback that can be extended and passed to a TrialRunner

Tune callbacks are called from within the TrialRunner class. There are several hooks that can be used, all of which are found in the submethod definitions of this base class.

The parameters passed to the **info dict vary between hooks. The parameters passed are described in the docstrings of the methods.

This example will print a metric each time a result is received:

from ray import train, tune
from ray.tune import Callback


class MyCallback(Callback):
    def on_trial_result(self, iteration, trials, trial, result,
                        **info):
        print(f"Got result: {result['metric']}")


def train_func(config):
    for i in range(10):
        tune.report(metric=i)

tuner = tune.Tuner(
    train_func,
    run_config=train.RunConfig(
        callbacks=[MyCallback()]
    )
)
tuner.fit()

PublicAPI (beta): This API is in beta and may change before becoming stable.

Methods

__init__

get_state

Get the state of the callback.

on_checkpoint

Called after a trial saved a checkpoint with Tune.

on_experiment_end

Called after experiment is over and all trials have concluded.

on_step_begin

Called at the start of each tuning loop step.

on_step_end

Called at the end of each tuning loop step.

on_trial_complete

Called after a trial instance completed.

on_trial_error

Called after a trial instance failed (errored).

on_trial_recover

Called after a trial instance failed (errored) but the trial is scheduled for retry.

on_trial_restore

Called after restoring a trial instance.

on_trial_result

Called after receiving a result from a trial.

on_trial_save

Called after receiving a checkpoint from a trial.

on_trial_start

Called after starting a trial instance.

set_state

Set the state of the callback.

setup

Called once at the very beginning of training.