nip.utils.experiments.SequentialHyperparameterExperiment#
- class nip.utils.experiments.SequentialHyperparameterExperiment(experiment_fn: Callable[[dict, str, Namespace, Callable, LoggerAdapter, str], None], param_grid: dict | None = None, run_id_fn: Callable[[RunIDFunctionArguments], str] | None = None, run_preparer_fn: Callable[[dict, Namespace], PreparedExperimentInfo] | None = None, experiment_name: str = 'EXPERIMENT', arg_parser_description: str = 'Run hyperparameter experiments sequentially', default_config_filename: str | None = None, config_file_base_path: Path | str | None = None, default_wandb_project: str | None = None, allow_resuming_wandb_run: bool = False, add_run_infix_argument: bool = True, output_width: int = 70)[source]#
A class to run an experiment over a grid of hyperparameters in sequence.
Runs each combination of hyperparameters in the grid as a separate experiment. If there is an error in one of the experiments, all subsequent experiments are skipped.
A summary of the results is printed at the end.
The workflow is as follows:
Call the constructor with the hyperparameter grid and the experiment function.
(Optional) Add any additional arguments to the arg parser using
self.parser.add_argument.Call
self.run()to run the experiment.
- Parameters:
experiment_fn (Callable[[ExperimentFunctionArguments], None]) – A function that takes a single hyperparameter combination and runs the experiment. The arguments are specified in the
ExperimentFunctionArgumentsdataclass.param_grid (dict, optional) – A dictionary mapping hyperparameter names to lists of values to try. If not given, a positional argument “config_file” will be added to the parser, and this will be used to load the hyperparameter grid from a config file.
run_id_fn (Callable[[RunIDFunctionArguments], str], optional) – A function that takes a single hyperparameter combination and returns a unique identifier for the run. If None, the default is to use the experiment name and the combination index. The arguments are specified in the
RunIDFunctionArgumentsdataclass.run_preparer_fn (Callable[[dict, Namespace], PreparedExperimentInfo], optional) –
A function that takes a single hyperparameter combination and prepares the run for it. It should return a
PreparedExperimentInfoinstance. This is optional. It should take the form:run_preparer_fn(combo, cmd_args)
where
combois a single combination of hyperparameters andcmd_argsis the command line arguments.experiment_name (str, default="EXPERIMENT") – The name of the experiment.
arg_parser_description (str, default="Run hyperparameter experiments sequentially") – The description of the argument parser.
default_config_filename (Optional[str], default=None) – The default config filename to use if the param_grid is not given. If None, no default is set, and if the user does not provide a config file, an empty grid is used.
config_file_base_path (Optional[str | Path], default=None) – The base path to use for the config file. If None, the current working directory is used. If the config file is specified as a relative path, it will be resolved relative to this base path. If this is an absolute path, it will be used as is.
default_wandb_project (Optional[str], default=None) – The default W&B project to use. If None, the default is to use the WANDB_PROJECT environment variable.
allow_resuming_wandb_run (bool, default=False) – Whether to allow resuming a W&B run with the same ID as a run in this experiment.
add_run_infix_argument (bool, default=True) – Whether to add an argument to the parser for adding an infix to the run ID.
output_width (int, default=70) – The width of the output to print (after the logging prefix).
Methods Summary
__init__(experiment_fn[, param_grid, ...])_load_param_grid(config_filename)Load the hyperparameter grid from a config file.
_run()Run the experiment.
_run_single_experiment(combinations, combo, ...)Run an experiment for a single combination of hyperparameters.
_setup_logger(combo_index, num_combos)Set up the logger for a single run.
Make sure there are no runs with the same ID as any run in this experiment.
run()Run the experiment.
Attributes
combinationsAn iterator over the combinations of hyperparameters.
common_run_nameA name for the experiment that is common to all runs.
enumerated_combinationsAn iterator over the combinations of hyperparameters plus an enumeration.
Methods
- __init__(experiment_fn: Callable[[dict, str, Namespace, Callable, LoggerAdapter, str], None], param_grid: dict | None = None, run_id_fn: Callable[[RunIDFunctionArguments], str] | None = None, run_preparer_fn: Callable[[dict, Namespace], PreparedExperimentInfo] | None = None, experiment_name: str = 'EXPERIMENT', arg_parser_description: str = 'Run hyperparameter experiments sequentially', default_config_filename: str | None = None, config_file_base_path: Path | str | None = None, default_wandb_project: str | None = None, allow_resuming_wandb_run: bool = False, add_run_infix_argument: bool = True, output_width: int = 70)[source]#
- _load_param_grid(config_filename: str)[source]#
Load the hyperparameter grid from a config file.
- Parameters:
config_filename (str) – The path to the config file given as a command line argument. If this is an absolute path, it will be used as is. If it is a relative path, it will be resolved relative to the config_file_base_path given in the constructor.
- Raises:
ValueError – If the config file format is not supported, or the content of the file does not match the expected format.
- _run()[source]#
Run the experiment.
This is the function that actually runs the experiment, and should be implemented by subclasses.
- _run_single_experiment(combinations: list[tuple[int, dict]], combo: dict, combo_index: int, cmd_args: Namespace) bool[source]#
Run an experiment for a single combination of hyperparameters.
- check_no_extant_runs()[source]#
Make sure there are no runs with the same ID as any run in this experiment.
- Raises:
ValueError – If there is a run with the same ID as any run in this experiment.