Search code examples
pythoncheckpointscikit-optimizeskopt

Resume gaussian process from checkpoint in skopt


I would like to be able to resume a gaussian process from a checkpoint with the library skopt. After some research I couldn't find a way to do so.

Here is a simple code to show what I want to do:

import skopt


LOAD = False        # To continue the optimization from a checkpoint, I would like to be able to set True here

x = skopt.space.Real(low=-10, high=10, name='x')
y = skopt.space.Real(low=-10, high=10, name='y')
dimensions = [x, y]
x0 = [0, 0]


@skopt.utils.use_named_args(dimensions=dimensions)
def f_to_minimize(x, y):
    return (x * y) ** 2 + (x + 2) ** 2


checkpoint_callback = skopt.callbacks.CheckpointSaver(
    checkpoint_path='checkpoint.pkl',
)

saved_checkpoint = None
if LOAD:
    saved_checkpoint = skopt.load('checkpoint.pkl')     # <- How can I use this ?

search_result = skopt.optimizer.gp_minimize(
    func=f_to_minimize,
    dimensions=dimensions,
    x0=x0,
    callback=[checkpoint_callback],
    n_calls=2,
    n_random_starts=1,
)

I would like to be able to load a checkpoint from a previous gaussian process optimization and continue it so the model doesn't have to learn everything from scratch again. Is there a way to do so ?


Solution

  • This example in the skopt docs covers saving and using checkpoints — in short, after you load the checkpoint object, you can use its x_iters and func_vals properties as the x0 and y0 keyword arguments to gp_minimize. If you're not loading a checkpoint, you can let x0 be your initial default and y0 be None.