Callbacks API (ng.callbacks)
Callbacks can be registered through the optimizer.register_callback
for call on either ask
or tell
methods. Two of them are available through the
ng.callbacks namespace.
- class nevergrad.callbacks.EarlyStopping(stopping_criterion: Callable[[Optimizer], bool])
Callback for stopping the
minimize
method before the budget is fully used.- Parameters:
stopping_criterion (func(optimizer) -> bool) – function that takes the current optimizer as input and returns True if the minimization must be stopped
Note
This callback must be register on the “ask” method only.
Example
In the following code, the
minimize
method will be stopped at the 4th “ask”>>> early_stopping = ng.callbacks.EarlyStopping(lambda opt: opt.num_ask > 3) >>> optimizer.register_callback("ask", early_stopping) >>> optimizer.minimize(_func, verbosity=2)
A couple other options (equivalent in case of non-noisy optimization) for stopping if the loss is below 12:
>>> early_stopping = ng.callbacks.EarlyStopping(lambda opt: opt.recommend().loss < 12) >>> early_stopping = ng.callbacks.EarlyStopping(lambda opt: opt.current_bests["minimum"].mean < 12)
- classmethod no_improvement_stopper(tolerance_window: int) EarlyStopping
Early stop when loss didn’t reduce during tolerance_window asks
- classmethod timer(max_duration: float) EarlyStopping
Early stop when max_duration seconds has been reached (from the first ask)
- class nevergrad.callbacks.OptimizerDump(filepath: str | Path)
Dumps the optimizer to a pickle file at every call.
- Parameters:
filepath (str or Path) – path to the pickle file
- class nevergrad.callbacks.ParametersLogger(filepath: str | Path, append: bool = True, order: int = 1)
Logs parameter and run information throughout into a file during optimization.
- Parameters:
filepath (str or pathlib.Path) – the path to dump data to
append (bool) – whether to append the file (otherwise it replaces it)
order (int) – order of the internal/model parameters to extract
Example
logger = ParametersLogger(filepath) optimizer.register_callback("tell", logger) optimizer.minimize() list_of_dict_of_data = logger.load()
Note
Arrays are converted to lists
- load() List[Dict[str, Any]]
Loads data from the log file
- load_flattened(max_list_elements: int = 24) List[Dict[str, Any]]
Loads data from the log file, and splits lists (arrays) into multiple arguments
- Parameters:
max_list_elements (int) – Maximum number of elements displayed from the array, each element is given a unique id of type list_name#i0_i1_…
- to_hiplot_experiment(max_list_elements: int = 24) Any
Converts the logs into an hiplot experiment for display.
- Parameters:
max_list_elements (int) – maximum number of elements of list/arrays to export (only the first elements are extracted)
Example
exp = logs.to_hiplot_experiment() exp.display(force_full_width=True)
Note
You can easily change the axes of the XY plot:
exp.display_data(hip.Displays.XY).update({'axis_x': '0#0', 'axis_y': '0#1'})
For more context about hiplot, check:
- class nevergrad.callbacks.ProgressBar
Progress bar to register as callback in an optimizer