A guide to callbacks and metrics in Ray Tune#
Use callbacks in Tune#
Ray Tune supports callbacks that it calls at various points during training. Pass your callbacks to RunConfig through its callbacks parameter, and pass the RunConfig to Tuner. Tune then automatically invokes the callback methods you implement.
This callback prints a metric each time Tune receives a result:
from ray import 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_fn(config):
for i in range(10):
tune.report({"metric": i})
tuner = tune.Tuner(
train_fn,
run_config=tune.RunConfig(callbacks=[MyCallback()]))
tuner.fit()
For more details and the available hooks, see the API docs for Ray Tune callbacks.
Log metrics in Tune#
You can log arbitrary values and metrics with both the function API and the class API:
def trainable(config):
for i in range(num_epochs):
...
tune.report({"acc": accuracy, "metric_foo": random_metric_1, "bar": metric_2})
class Trainable(tune.Trainable):
def step(self):
...
# don't call report here!
return dict(acc=accuracy, metric_foo=random_metric_1, bar=metric_2)
Tip
tune.report() isn’t meant to transfer large amounts of data, such as models or datasets. Doing so can incur large overhead and significantly slow down your Tune run.
Which metrics does Tune fill in automatically?#
During training, Tune automatically logs a set of auto-filled metrics in addition to any values you report. You can use any of them as a stopping condition or pass them as a parameter to trial schedulers or search algorithms. Tune fills in the following metrics:
config: The hyperparameter configurationdate: String-formatted date and time when the result was processeddone:Trueif the trial has finished,Falseotherwiseepisodes_total: Total number of episodes, for RLlib trainablesexperiment_id: Unique experiment IDexperiment_tag: Tag of the trial, which includes parameter valueshostname: Hostname of the workeriterations_since_restore: The number of timestune.report()has been called after restoring the worker from a checkpointnode_ip: Host IP of the workerpid: Process ID (PID) of the worker processtime_since_restore: Time in seconds since restoring from a checkpointtime_this_iter_s: Runtime of the current training iteration in seconds, meaning one call to the trainable function or tostep()in the class APItime_total_s: Total runtime in secondstimestamp: Timestamp when the result was processedtimesteps_since_restore: Number of timesteps since restoring from a checkpointtimesteps_total: Total number of timestepstraining_iteration: The number of timestune.report()has been calledtrial_id: Unique trial ID
You can find all of these metrics in the Trial.last_result dictionary.