File: interruptible-optimization.rst

package info (click to toggle)
scikit-optimize 0.10.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,684 kB
  • sloc: python: 10,659; javascript: 438; makefile: 136; sh: 6
file content (348 lines) | stat: -rw-r--r-- 16,244 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples\interruptible-optimization.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_interruptible-optimization.py>`
        to download the full example code or to run this example in your browser via Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_interruptible-optimization.py:


================================================
Interruptible optimization runs with checkpoints
================================================

Christian Schell, Mai 2018
Reformatted by Holger Nahrstaedt 2020

.. currentmodule:: skopt

Problem statement
=================

Optimization runs can take a very long time and even run for multiple days.
If for some reason the process has to be interrupted results are irreversibly
lost, and the routine has to start over from the beginning.

With the help of the :class:`callbacks.CheckpointSaver` callback the optimizer's current state
can be saved after each iteration, allowing to restart from that point at any
time.

This is useful, for example,

* if you don't know how long the process will take and cannot hog computational resources forever
* if there might be system failures due to shaky infrastructure (or colleagues...)
* if you want to adjust some parameters and continue with the already obtained results

.. GENERATED FROM PYTHON SOURCE LINES 29-35

.. code-block:: Python


    print(__doc__)
    import numpy as np

    np.random.seed(777)








.. GENERATED FROM PYTHON SOURCE LINES 36-43

Simple example
==============

We will use pretty much the same optimization problem as in the
:ref:`sphx_glr_auto_examples_bayesian-optimization.py`
notebook. Additionally we will instantiate the :class:`callbacks.CheckpointSaver`
and pass it to the minimizer:

.. GENERATED FROM PYTHON SOURCE LINES 43-68

.. code-block:: Python


    from skopt import gp_minimize
    from skopt.callbacks import CheckpointSaver

    noise_level = 0.1


    def obj_fun(x, noise_level=noise_level):
        return np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2)) + np.random.randn() * noise_level


    checkpoint_saver = CheckpointSaver("./checkpoint.pkl", compress=9) # kwargs passed to `skopt.dump`

    gp_minimize(
        obj_fun,  # the function to minimize
        [(-20.0, 20.0)],  # the bounds on each dimension of x
        x0=[-20.0],  # the starting point
        acq_func="LCB",  # the acquisition function (optional)
        n_calls=10,  # number of evaluations of f including at x0
        n_random_starts=3,  # the number of random initial points
        callback=[checkpoint_saver],
        # a list of callbacks including the checkpoint saver
        random_state=777,
    )





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


              fun: -0.17524445239614728
                x: [-18.660711608230713]
        func_vals: [-4.682e-02 -8.228e-02 -6.538e-03 -7.134e-02  9.064e-02
                     7.662e-02  8.261e-02 -1.324e-01 -1.752e-01  1.002e-01]
          x_iters: [[-20.0], [5.857990176187936], [-11.97095004855501], [5.450171667295798], [10.524218484747195], [-17.111120867646253], [7.251301457256783], [-19.16709880389749], [-18.660711608230713], [-18.28429723556215]]
           models: [GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735)]
            space: Space([Real(low=-20.0, high=20.0, prior='uniform', transform='normalize')])
     random_state: RandomState(MT19937)
            specs:     args:                    func: <function obj_fun at 0x0000020BCE5B9940>
                                          dimensions: Space([Real(low=-20.0, high=20.0, prior='uniform', transform='normalize')])
                                      base_estimator: GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5),
                                                                               n_restarts_optimizer=2, noise='gaussian',
                                                                               normalize_y=True, random_state=655685735)
                                             n_calls: 10
                                     n_random_starts: 3
                                    n_initial_points: 10
                             initial_point_generator: random
                                            acq_func: LCB
                                       acq_optimizer: auto
                                                  x0: [-20.0]
                                                  y0: None
                                        random_state: RandomState(MT19937)
                                             verbose: False
                                            callback: [<skopt.callbacks.CheckpointSaver object at 0x0000020BCF20E710>]
                                            n_points: 10000
                                n_restarts_optimizer: 5
                                                  xi: 0.01
                                               kappa: 1.96
                                              n_jobs: 1
                                    model_queue_size: None
                                    space_constraint: None
                   function: base_minimize



.. GENERATED FROM PYTHON SOURCE LINES 69-86

Now let's assume this did not finish at once but took some long time: you
started this on Friday night, went out for the weekend and now, Monday
morning, you're eager to see the results. However, instead of the
notebook server you only see a blank page and your colleague Garry
tells you that he had had an update scheduled for Sunday noon – who
doesn't like updates?

:class:`gp_minimize` did not finish, and there is no `res` variable with the
actual results!

Restoring the last checkpoint
=============================

Luckily we employed the :class:`callbacks.CheckpointSaver` and can now restore the latest
result with :class:`skopt.load`
(see :ref:`sphx_glr_auto_examples_store-and-load-results.py` for more
information on that)

.. GENERATED FROM PYTHON SOURCE LINES 86-93

.. code-block:: Python


    from skopt import load

    res = load('./checkpoint.pkl')

    res.fun





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    -0.17524445239614728



.. GENERATED FROM PYTHON SOURCE LINES 94-98

Continue the search
===================

The previous results can then be used to continue the optimization process:

.. GENERATED FROM PYTHON SOURCE LINES 98-120

.. code-block:: Python


    x0 = res.x_iters
    y0 = res.func_vals

    # To ensure that the base estimator is loaded properly and that the next 
    # parameters are new ones:
    base_estimator = res.specs['args']['base_estimator']
    random_state = res.random_state

    gp_minimize(
        obj_fun,  # the function to minimize
        [(-20.0, 20.0)],  # the bounds on each dimension of x
        base_estimator=base_estimator, # warm-started base-estimator from checkpoint
        x0=x0,  # already examined values for x
        y0=y0,  # observed values for x0
        acq_func="LCB",  # the acquisition function (optional)
        n_calls=10,  # number of evaluations of f including at x0
        n_random_starts=3,  # the number of random initialization points
        callback=[checkpoint_saver],
        random_state=random_state,
    )





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


              fun: -0.17524445239614728
                x: [-18.660711608230713]
        func_vals: [-4.682e-02 -8.228e-02 ...  9.148e-02  2.650e-02]
          x_iters: [[-20.0], [5.857990176187936], [-11.97095004855501], [5.450171667295798], [10.524218484747195], [-17.111120867646253], [7.251301457256783], [-19.16709880389749], [-18.660711608230713], [-18.28429723556215], [-4.89536560655587], [-13.817289687225252], [6.9752035588202865], [-19.137121427347815], [-18.893981612139648], [-19.36136150292367], [-18.95171384338072], [6.383980575487712], [-18.82159785473173], [-19.373206975288387]]
           models: [GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735), GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5) + WhiteKernel(noise_level=1),
                                            n_restarts_optimizer=2, noise='gaussian',
                                            normalize_y=True, random_state=655685735)]
            space: Space([Real(low=-20.0, high=20.0, prior='uniform', transform='normalize')])
     random_state: RandomState(MT19937)
            specs:     args:                    func: <function obj_fun at 0x0000020BCE5B9940>
                                          dimensions: Space([Real(low=-20.0, high=20.0, prior='uniform', transform='normalize')])
                                      base_estimator: GaussianProcessRegressor(kernel=1**2 * Matern(length_scale=1, nu=2.5),
                                                                               n_restarts_optimizer=2, noise='gaussian',
                                                                               normalize_y=True, random_state=655685735)
                                             n_calls: 10
                                     n_random_starts: 3
                                    n_initial_points: 10
                             initial_point_generator: random
                                            acq_func: LCB
                                       acq_optimizer: auto
                                                  x0: [[-20.0], [5.857990176187936], [-11.97095004855501], [5.450171667295798], [10.524218484747195], [-17.111120867646253], [7.251301457256783], [-19.16709880389749], [-18.660711608230713], [-18.28429723556215]]
                                                  y0: [-4.682e-02 -8.228e-02
                                                       -6.538e-03 -7.134e-02
                                                        9.064e-02  7.662e-02
                                                        8.261e-02 -1.324e-01
                                                       -1.752e-01  1.002e-01]
                                        random_state: RandomState(MT19937)
                                             verbose: False
                                            callback: [<skopt.callbacks.CheckpointSaver object at 0x0000020BCF20E710>]
                                            n_points: 10000
                                n_restarts_optimizer: 5
                                                  xi: 0.01
                                               kappa: 1.96
                                              n_jobs: 1
                                    model_queue_size: None
                                    space_constraint: None
                   function: base_minimize



.. GENERATED FROM PYTHON SOURCE LINES 121-133

Possible problems
=================

* **changes in search space:** You can use this technique to interrupt
  the search, tune the search space and continue the optimization. Note
  that the optimizers will complain if `x0` contains parameter values not
  covered by the dimension definitions, so in many cases shrinking the
  search space will not work without deleting the offending runs from
  `x0` and `y0`.
* see :ref:`sphx_glr_auto_examples_store-and-load-results.py`

for more information on how the results get saved and possible caveats


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.988 seconds)


.. _sphx_glr_download_auto_examples_interruptible-optimization.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/holgern/scikit-optimize/master?urlpath=lab/tree/notebooks/auto_examples/interruptible-optimization.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: interruptible-optimization.ipynb <interruptible-optimization.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: interruptible-optimization.py <interruptible-optimization.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_