ray.tune.ExperimentAnalysis
ray.tune.ExperimentAnalysis#
- class ray.tune.ExperimentAnalysis(experiment_checkpoint_path: str, trials: Optional[List[ray.tune.experiment.trial.Trial]] = None, default_metric: Optional[str] = None, default_mode: Optional[str] = None, sync_config: Optional[ray.tune.syncer.SyncConfig] = None)[source]#
Bases:
object
Analyze results from a Tune experiment.
To use this class, the experiment must be executed with the JsonLogger.
- Parameters
experiment_checkpoint_path – Path to a json file or directory representing an experiment state, or a directory containing multiple experiment states (a run’s
local_dir
). Corresponds to Experiment.local_dir/Experiment.name/ experiment_state.jsontrials – List of trials that can be accessed via
analysis.trials
.default_metric – Default metric for comparing results. Can be overwritten with the
metric
parameter in the respective functions.default_mode – Default mode for comparing results. Has to be one of [min, max]. Can be overwritten with the
mode
parameter in the respective functions.
Example
>>> from ray import tune >>> tune.run( ... my_trainable, name="my_exp", local_dir="~/tune_results") >>> analysis = ExperimentAnalysis( ... experiment_checkpoint_path="~/tune_results/my_exp/state.json")
PublicAPI (beta): This API is in beta and may change before becoming stable.
- property best_trial: ray.tune.experiment.trial.Trial#
Get the best trial of the experiment
The best trial is determined by comparing the last trial results using the
metric
andmode
parameters passed totune.run()
.If you didn’t pass these parameters, use
get_best_trial(metric, mode, scope)
instead.
- property best_config: Dict#
Get the config of the best trial of the experiment
The best trial is determined by comparing the last trial results using the
metric
andmode
parameters passed totune.run()
.If you didn’t pass these parameters, use
get_best_config(metric, mode, scope)
instead.
- property best_checkpoint: ray.air.checkpoint.Checkpoint#
Get the checkpoint path of the best trial of the experiment
The best trial is determined by comparing the last trial results using the
metric
andmode
parameters passed totune.run()
.If you didn’t pass these parameters, use
get_best_checkpoint(trial, metric, mode)
instead.- Returns
Checkpoint
object.
- property best_logdir: str#
Get the logdir of the best trial of the experiment
The best trial is determined by comparing the last trial results using the
metric
andmode
parameters passed totune.run()
.If you didn’t pass these parameters, use
get_best_logdir(metric, mode)
instead.
- property best_dataframe: pandas.core.frame.DataFrame#
Get the full result dataframe of the best trial of the experiment
The best trial is determined by comparing the last trial results using the
metric
andmode
parameters passed totune.run()
.If you didn’t pass these parameters, use
get_best_logdir(metric, mode)
and use it to look for the dataframe in theself.trial_dataframes
dict.
- property best_result: Dict#
Get the last result of the best trial of the experiment
The best trial is determined by comparing the last trial results using the
metric
andmode
parameters passed totune.run()
.If you didn’t pass these parameters, use
get_best_trial(metric, mode, scope).last_result
instead.
- property best_result_df: pandas.core.frame.DataFrame#
Get the best result of the experiment as a pandas dataframe.
The best trial is determined by comparing the last trial results using the
metric
andmode
parameters passed totune.run()
.If you didn’t pass these parameters, use
get_best_trial(metric, mode, scope).last_result
instead.
- property results: Dict[str, Dict]#
Get the last result of the all trials of the experiment
- property results_df: pandas.core.frame.DataFrame#
Get all the last results as a pandas dataframe.
- property trial_dataframes: Dict[str, pandas.core.frame.DataFrame]#
List of all dataframes of the trials.
Each dataframe is indexed by iterations and contains reported metrics.
- dataframe(metric: Optional[str] = None, mode: Optional[str] = None) pandas.core.frame.DataFrame [source]#
Returns a pandas.DataFrame object constructed from the trials.
This function will look through all observed results of each trial and return the one corresponding to the passed
metric
andmode
: Ifmode=min
, it returns the result with the lowest ever observedmetric
for this trial (this is not necessarily the last)! Formode=max
, it’s the highest, respectively. Ifmetric=None
ormode=None
, the last result will be returned.- Parameters
metric – Key for trial info to order on. If None, uses last result.
mode – One of [None, “min”, “max”].
- Returns
Constructed from a result dict of each trial.
- Return type
pd.DataFrame
- get_trial_checkpoints_paths(trial: ray.tune.experiment.trial.Trial, metric: Optional[str] = None) List[Tuple[str, numbers.Number]] [source]#
Gets paths and metrics of all persistent checkpoints of a trial.
- Parameters
trial – The log directory of a trial, or a trial instance.
metric – key for trial info to return, e.g. “mean_accuracy”. “training_iteration” is used by default if no value was passed to
self.default_metric
.
- Returns
List of [path, metric] for all persistent checkpoints of the trial.
- get_best_checkpoint(trial: ray.tune.experiment.trial.Trial, metric: Optional[str] = None, mode: Optional[str] = None, return_path: bool = False) Optional[Union[ray.air.checkpoint.Checkpoint, str]] [source]#
Gets best persistent checkpoint path of provided trial.
Any checkpoints with an associated metric value of
nan
will be filtered out.- Parameters
trial – The log directory of a trial, or a trial instance.
metric – key of trial info to return, e.g. “mean_accuracy”. “training_iteration” is used by default if no value was passed to
self.default_metric
.mode – One of [min, max]. Defaults to
self.default_mode
.return_path – If True, only returns the path (and not the
Checkpoint
object). If using Ray client, it is not guaranteed that this path is available on the local (client) node. Can also contain a cloud URI.
- Returns
Checkpoint
object or string ifreturn_path=True
.
- get_all_configs(prefix: bool = False) Dict[str, Dict] [source]#
Returns a list of all configurations.
- Parameters
prefix – If True, flattens the config dict and prepends
config/
.- Returns
- Dict of all configurations of trials, indexed by
their trial dir.
- Return type
Dict[str, Dict]
- get_best_trial(metric: Optional[str] = None, mode: Optional[str] = None, scope: str = 'last', filter_nan_and_inf: bool = True) Optional[ray.tune.experiment.trial.Trial] [source]#
Retrieve the best trial object.
Compares all trials’ scores on
metric
. Ifmetric
is not specified,self.default_metric
will be used. Ifmode
is not specified,self.default_mode
will be used. These values are usually initialized by passing themetric
andmode
parameters totune.run()
.- Parameters
metric – Key for trial info to order on. Defaults to
self.default_metric
.mode – One of [min, max]. Defaults to
self.default_mode
.scope – One of [all, last, avg, last-5-avg, last-10-avg]. If
scope=last
, only look at each trial’s final step formetric
, and compare across trials based onmode=[min,max]
. Ifscope=avg
, consider the simple average over all steps formetric
and compare across trials based onmode=[min,max]
. Ifscope=last-5-avg
orscope=last-10-avg
, consider the simple average over the last 5 or 10 steps formetric
and compare across trials based onmode=[min,max]
. Ifscope=all
, find each trial’s min/max score formetric
based onmode
, and compare trials based onmode=[min,max]
.filter_nan_and_inf – If True (default), NaN or infinite values are disregarded and these trials are never selected as the best trial.
- Returns
- The best trial for the provided metric. If no trials contain the provided
metric, or if the value for the metric is NaN for all trials, then returns None.
- get_best_config(metric: Optional[str] = None, mode: Optional[str] = None, scope: str = 'last') Optional[Dict] [source]#
Retrieve the best config corresponding to the trial.
Compares all trials’ scores on
metric
. Ifmetric
is not specified,self.default_metric
will be used. Ifmode
is not specified,self.default_mode
will be used. These values are usually initialized by passing themetric
andmode
parameters totune.run()
.- Parameters
metric – Key for trial info to order on. Defaults to
self.default_metric
.mode – One of [min, max]. Defaults to
self.default_mode
.scope – One of [all, last, avg, last-5-avg, last-10-avg]. If
scope=last
, only look at each trial’s final step formetric
, and compare across trials based onmode=[min,max]
. Ifscope=avg
, consider the simple average over all steps formetric
and compare across trials based onmode=[min,max]
. Ifscope=last-5-avg
orscope=last-10-avg
, consider the simple average over the last 5 or 10 steps formetric
and compare across trials based onmode=[min,max]
. Ifscope=all
, find each trial’s min/max score formetric
based onmode
, and compare trials based onmode=[min,max]
.
- get_best_logdir(metric: Optional[str] = None, mode: Optional[str] = None, scope: str = 'last') Optional[str] [source]#
Retrieve the logdir corresponding to the best trial.
Compares all trials’ scores on
metric
. Ifmetric
is not specified,self.default_metric
will be used. Ifmode
is not specified,self.default_mode
will be used. These values are usually initialized by passing themetric
andmode
parameters totune.run()
.- Parameters
metric – Key for trial info to order on. Defaults to
self.default_metric
.mode – One of [min, max]. Defaults to
self.default_mode
.scope – One of [all, last, avg, last-5-avg, last-10-avg]. If
scope=last
, only look at each trial’s final step formetric
, and compare across trials based onmode=[min,max]
. Ifscope=avg
, consider the simple average over all steps formetric
and compare across trials based onmode=[min,max]
. Ifscope=last-5-avg
orscope=last-10-avg
, consider the simple average over the last 5 or 10 steps formetric
and compare across trials based onmode=[min,max]
. Ifscope=all
, find each trial’s min/max score formetric
based onmode
, and compare trials based onmode=[min,max]
.
- get_last_checkpoint(trial=None, metric='training_iteration', mode='max')[source]#
Gets the last persistent checkpoint path of the provided trial, i.e., with the highest “training_iteration”.
If no trial is specified, it loads the best trial according to the provided metric and mode (defaults to max. training iteration).
- Parameters
trial – The log directory or an instance of a trial. If None, load the latest trial automatically.
metric – If no trial is specified, use this metric to identify the best trial and load the last checkpoint from this trial.
mode – If no trial is specified, use the metric and this mode to identify the best trial and load the last checkpoint from it.
- Returns
Path for last checkpoint of trial
- fetch_trial_dataframes() Dict[str, pandas.core.frame.DataFrame] [source]#
Fetches trial dataframes from files.
- Returns
A dictionary containing “trial dir” to Dataframe.
- stats() Dict [source]#
Returns a dictionary of the statistics of the experiment.
If
experiment_checkpoint_path
pointed to a directory of experiments, the dict will be in the format of{experiment_session_id: stats}
.