-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathask-and-tell.html
455 lines (413 loc) · 35.5 KB
/
ask-and-tell.html
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="Description" content="scikit-optimize: machine learning in Python">
<title>Async optimization Loop — scikit-optimize 0.9.0 documentation</title>
<link rel="canonical" href="https://scikit-optimize.github.io/auto_examples/ask-and-tell.html" />
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<link rel="stylesheet" href="../_static/css/vendor/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/sg_gallery.css" type="text/css" />
<link rel="stylesheet" href="../_static/sg_gallery-binder.css" type="text/css" />
<link rel="stylesheet" href="../_static/sg_gallery-dataframe.css" type="text/css" />
<link rel="stylesheet" href="../_static/sg_gallery-rendered-html.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
</head>
<body>
<nav id="navbar" class="sk-docs-navbar navbar navbar-expand-md navbar-light bg-light py-0">
<div class="container-fluid sk-docs-container px-0">
<a class="navbar-brand py-0" href="../index.html">
<img
class="sk-brand-img"
src="../_static/logo.png"
alt="logo"/>
</a>
<button
id="sk-navbar-toggler"
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="sk-navbar-collapse collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="sk-nav-link nav-link" href="../install.html">Install</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link" href="../user_guide.html">User Guide</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link" href="../modules/classes.html">API</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link" href="index.html">Examples</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../getting_started.html">Getting Started</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../development.html">Development</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://github.com/scikit-optimize/scikit-optimize">GitHub</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://scikit-optimize.github.io/dev/versions.html">Other Versions</a>
</li>
<li class="nav-item dropdown nav-more-item-dropdown">
<a class="sk-nav-link nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">More</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="sk-nav-dropdown-item dropdown-item" href="../getting_started.html">Getting Started</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../development.html">Development</a>
<a class="sk-nav-dropdown-item dropdown-item" href="https://github.com/scikit-optimize/scikit-optimize">GitHub</a>
<a class="sk-nav-dropdown-item dropdown-item" href="https://scikit-optimize.github.io/dev/versions.html">Other Versions</a>
</div>
</li>
</ul>
<div id="searchbox" role="search">
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input class="sk-search-text-input" type="text" name="q" aria-labelledby="searchlabel" />
<input class="sk-search-text-btn" type="submit" value="Go" />
</form>
</div>
</div>
</div>
</div>
</nav>
<div class="d-flex" id="sk-doc-wrapper">
<input type="checkbox" name="sk-toggle-checkbox" id="sk-toggle-checkbox">
<label id="sk-sidemenu-toggle" class="sk-btn-toggle-toc btn sk-btn-primary" for="sk-toggle-checkbox">Toggle Menu</label>
<div id="sk-sidebar-wrapper" class="border-right">
<div class="sk-sidebar-toc-wrapper">
<div class="sk-sidebar-toc-logo">
<a href="../index.html">
<img
class="sk-brand-img"
src="../_static/logo.png"
alt="logo"/>
</a>
</div>
<div class="btn-group w-100 mb-2" role="group" aria-label="rellinks">
<a href="hyperparameter-optimization.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Tuning a scikit-learn estimator with skopt">Prev</a><a href="index.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Examples">Up</a>
<a href="strategy-comparison.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Comparing surrogate models">Next</a>
</div>
<div class="alert alert-danger p-1 mb-2" role="alert">
<p class="text-center mb-0">
<strong>scikit-optimize 0.9.0</strong><br/>
<a href="https://scikit-optimize.github.io/dev/versions.html">Other versions</a>
</p>
</div>
<div class="sk-sidebar-toc">
<ul>
<li><a class="reference internal" href="#">Async optimization Loop</a><ul>
<li><a class="reference internal" href="#the-setup">The Setup</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div id="sk-page-content-wrapper">
<div class="sk-page-content container-fluid body px-md-3" role="main">
<div class="sphx-glr-download-link-note admonition note">
<p class="admonition-title">Note</p>
<p>Click <a class="reference internal" href="#sphx-glr-download-auto-examples-ask-and-tell-py"><span class="std std-ref">here</span></a>
to download the full example code or to run this example in your browser via Binder</p>
</div>
<section class="sphx-glr-example-title" id="async-optimization-loop">
<span id="sphx-glr-auto-examples-ask-and-tell-py"></span><h1>Async optimization Loop<a class="headerlink" href="#async-optimization-loop" title="Permalink to this headline">¶</a></h1>
<p>Bayesian optimization is used to tune parameters for walking robots or other
experiments that are not a simple (expensive) function call.</p>
<p>Tim Head, February 2017.
Reformatted by Holger Nahrstaedt 2020</p>
<p>They often follow a pattern a bit like this:</p>
<ol class="arabic simple">
<li><p>ask for a new set of parameters</p></li>
<li><p>walk to the experiment and program in the new parameters</p></li>
<li><p>observe the outcome of running the experiment</p></li>
<li><p>walk back to your laptop and tell the optimizer about the outcome</p></li>
<li><p>go to step 1</p></li>
</ol>
<p>A setup like this is difficult to implement with the <strong>*_minimize()</strong> function
interface. This is why <strong>scikit-optimize</strong> has a ask-and-tell interface that
you can use when you want to control the execution of the optimization loop.</p>
<p>This notebook demonstrates how to use the ask and tell interface.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="vm">__doc__</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<a href="https://numpy.org/doc/stable/reference/random/generated/numpy.random.seed.html#numpy.random.seed" title="numpy.random.seed" class="sphx-glr-backref-module-numpy-random sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span></a><span class="p">(</span><span class="mi">1234</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">skopt.plots</span> <span class="kn">import</span> <a href="../modules/generated/skopt.plots.plot_gaussian_process.html#skopt.plots.plot_gaussian_process" title="skopt.plots.plot_gaussian_process" class="sphx-glr-backref-module-skopt-plots sphx-glr-backref-type-py-function"><span class="n">plot_gaussian_process</span></a>
</pre></div>
</div>
<section id="the-setup">
<h2>The Setup<a class="headerlink" href="#the-setup" title="Permalink to this headline">¶</a></h2>
<p>We will use a simple 1D problem to illustrate the API. This is a little bit
artificial as you normally would not use the ask-and-tell interface if you
had a function you can call to evaluate the objective.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">skopt.learning</span> <span class="kn">import</span> <span class="n">ExtraTreesRegressor</span>
<span class="kn">from</span> <span class="nn">skopt</span> <span class="kn">import</span> <a href="../modules/generated/skopt.Optimizer.html#skopt.Optimizer" title="skopt.Optimizer" class="sphx-glr-backref-module-skopt sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">Optimizer</span></a>
<span class="n">noise_level</span> <span class="o">=</span> <span class="mf">0.1</span>
</pre></div>
</div>
<p>Our 1D toy problem, this is the function we are trying to
minimize</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">objective</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">noise_level</span><span class="o">=</span><span class="n">noise_level</span><span class="p">):</span>
<span class="k">return</span> <a href="https://numpy.org/doc/stable/reference/generated/numpy.sin.html#numpy.sin" title="numpy.sin" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-data"><span class="n">np</span><span class="o">.</span><span class="n">sin</span></a><span class="p">(</span><span class="mi">5</span> <span class="o">*</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <a href="https://numpy.org/doc/stable/reference/generated/numpy.tanh.html#numpy.tanh" title="numpy.tanh" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-data"><span class="n">np</span><span class="o">.</span><span class="n">tanh</span></a><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span>\
<span class="o">+</span> <a href="https://numpy.org/doc/stable/reference/random/generated/numpy.random.randn.html#numpy.random.randn" title="numpy.random.randn" class="sphx-glr-backref-module-numpy-random sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span></a><span class="p">()</span> <span class="o">*</span> <span class="n">noise_level</span>
<span class="k">def</span> <span class="nf">objective_wo_noise</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">noise_level</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="k">return</span> <span class="n">objective</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">noise_level</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>Here a quick plot to visualize what the function looks like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Plot f(x) + contours</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.set_cmap.html#matplotlib.pyplot.set_cmap" title="matplotlib.pyplot.set_cmap" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">set_cmap</span></a><span class="p">(</span><span class="s2">"viridis"</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <a href="https://numpy.org/doc/stable/reference/generated/numpy.linspace.html#numpy.linspace" title="numpy.linspace" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">linspace</span></a><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">400</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">fx</span> <span class="o">=</span> <a href="https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array" title="numpy.array" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">array</span></a><span class="p">([</span><span class="n">objective</span><span class="p">(</span><span class="n">x_i</span><span class="p">,</span> <span class="n">noise_level</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span> <span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">])</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot" title="matplotlib.pyplot.plot" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">plot</span></a><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">fx</span><span class="p">,</span> <span class="s2">"r--"</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"True (unknown)"</span><span class="p">)</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.fill.html#matplotlib.pyplot.fill" title="matplotlib.pyplot.fill" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">fill</span></a><span class="p">(</span><a href="https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html#numpy.concatenate" title="numpy.concatenate" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">concatenate</span></a><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]]),</span>
<a href="https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html#numpy.concatenate" title="numpy.concatenate" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">concatenate</span></a><span class="p">(([</span><span class="n">fx_i</span> <span class="o">-</span> <span class="mf">1.9600</span> <span class="o">*</span> <span class="n">noise_level</span> <span class="k">for</span> <span class="n">fx_i</span> <span class="ow">in</span> <span class="n">fx</span><span class="p">],</span>
<span class="p">[</span><span class="n">fx_i</span> <span class="o">+</span> <span class="mf">1.9600</span> <span class="o">*</span> <span class="n">noise_level</span> <span class="k">for</span> <span class="n">fx_i</span> <span class="ow">in</span> <span class="n">fx</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]])),</span>
<span class="n">alpha</span><span class="o">=</span><span class="mf">.2</span><span class="p">,</span> <span class="n">fc</span><span class="o">=</span><span class="s2">"r"</span><span class="p">,</span> <span class="n">ec</span><span class="o">=</span><span class="s2">"None"</span><span class="p">)</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html#matplotlib.pyplot.legend" title="matplotlib.pyplot.legend" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">legend</span></a><span class="p">()</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.grid.html#matplotlib.pyplot.grid" title="matplotlib.pyplot.grid" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">grid</span></a><span class="p">()</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">show</span></a><span class="p">()</span>
</pre></div>
</div>
<img src="../_images/sphx_glr_ask-and-tell_001.png" srcset="../_images/sphx_glr_ask-and-tell_001.png" alt="ask and tell" class = "sphx-glr-single-img"/><p>Now we setup the <a class="reference internal" href="../modules/generated/skopt.Optimizer.html#skopt.Optimizer" title="skopt.Optimizer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Optimizer</span></code></a> class. The arguments follow the meaning and
naming of the <strong>*_minimize()</strong> functions. An important difference is that
you do not pass the objective function to the optimizer.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">opt</span> <span class="o">=</span> <a href="../modules/generated/skopt.Optimizer.html#skopt.Optimizer" title="skopt.Optimizer" class="sphx-glr-backref-module-skopt sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">Optimizer</span></a><span class="p">([(</span><span class="o">-</span><span class="mf">2.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">)],</span> <span class="s2">"GP"</span><span class="p">,</span> <span class="n">acq_func</span><span class="o">=</span><span class="s2">"EI"</span><span class="p">,</span>
<span class="n">acq_optimizer</span><span class="o">=</span><span class="s2">"sampling"</span><span class="p">,</span>
<span class="n">initial_point_generator</span><span class="o">=</span><span class="s2">"lhs"</span><span class="p">)</span>
<span class="c1"># To obtain a suggestion for the point at which to evaluate the objective</span>
<span class="c1"># you call the ask() method of opt:</span>
<span class="n">next_x</span> <span class="o">=</span> <span class="n">opt</span><span class="o">.</span><span class="n">ask</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">next_x</span><span class="p">)</span>
</pre></div>
</div>
<p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>[-0.7315058981975282]
</pre></div>
</div>
<p>In a real world use case you would probably go away and use this
parameter in your experiment and come back a while later with the
result. In this example we can simply evaluate the objective function
and report the value back to the optimizer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">f_val</span> <span class="o">=</span> <span class="n">objective</span><span class="p">(</span><span class="n">next_x</span><span class="p">)</span>
<span class="n">opt</span><span class="o">.</span><span class="n">tell</span><span class="p">(</span><span class="n">next_x</span><span class="p">,</span> <span class="n">f_val</span><span class="p">)</span>
</pre></div>
</div>
<p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span> fun: 0.2071864923643295
func_vals: array([0.20718649])
models: []
random_state: RandomState(MT19937) at 0x7F9023FF2E40
space: Space([Real(low=-2.0, high=2.0, prior='uniform', transform='normalize')])
specs: {'args': {'dimensions': [(-2.0, 2.0)], 'base_estimator': 'GP', 'n_random_starts': None, 'n_initial_points': 10, 'initial_point_generator': 'lhs', 'n_jobs': 1, 'acq_func': 'EI', 'acq_optimizer': 'sampling', 'random_state': None, 'model_queue_size': None, 'acq_func_kwargs': None, 'acq_optimizer_kwargs': None}, 'function': 'Optimizer'}
x: [-0.7315058981975282]
x_iters: [[-0.7315058981975282]]
</pre></div>
</div>
<p>Like <strong>*_minimize()</strong> the first few points are suggestions from
the initial point generator as there
is no data yet with which to fit a surrogate model.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">9</span><span class="p">):</span>
<span class="n">next_x</span> <span class="o">=</span> <span class="n">opt</span><span class="o">.</span><span class="n">ask</span><span class="p">()</span>
<span class="n">f_val</span> <span class="o">=</span> <span class="n">objective</span><span class="p">(</span><span class="n">next_x</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">opt</span><span class="o">.</span><span class="n">tell</span><span class="p">(</span><span class="n">next_x</span><span class="p">,</span> <span class="n">f_val</span><span class="p">)</span>
</pre></div>
</div>
<p>We can now plot the random suggestions and the first model that has been
fit:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">_</span> <span class="o">=</span> <a href="../modules/generated/skopt.plots.plot_gaussian_process.html#skopt.plots.plot_gaussian_process" title="skopt.plots.plot_gaussian_process" class="sphx-glr-backref-module-skopt-plots sphx-glr-backref-type-py-function"><span class="n">plot_gaussian_process</span></a><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">objective</span><span class="o">=</span><span class="n">objective_wo_noise</span><span class="p">,</span>
<span class="n">noise_level</span><span class="o">=</span><span class="n">noise_level</span><span class="p">,</span>
<span class="n">show_next_point</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="n">show_acq_func</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">show</span></a><span class="p">()</span>
</pre></div>
</div>
<img src="../_images/sphx_glr_ask-and-tell_002.png" srcset="../_images/sphx_glr_ask-and-tell_002.png" alt="x* = -0.3201, f(x*) = -0.9482" class = "sphx-glr-single-img"/><p>Let us sample a few more points and plot the optimizer again:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="n">next_x</span> <span class="o">=</span> <span class="n">opt</span><span class="o">.</span><span class="n">ask</span><span class="p">()</span>
<span class="n">f_val</span> <span class="o">=</span> <span class="n">objective</span><span class="p">(</span><span class="n">next_x</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">opt</span><span class="o">.</span><span class="n">tell</span><span class="p">(</span><span class="n">next_x</span><span class="p">,</span> <span class="n">f_val</span><span class="p">)</span>
<span class="n">_</span> <span class="o">=</span> <a href="../modules/generated/skopt.plots.plot_gaussian_process.html#skopt.plots.plot_gaussian_process" title="skopt.plots.plot_gaussian_process" class="sphx-glr-backref-module-skopt-plots sphx-glr-backref-type-py-function"><span class="n">plot_gaussian_process</span></a><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">objective</span><span class="o">=</span><span class="n">objective_wo_noise</span><span class="p">,</span>
<span class="n">noise_level</span><span class="o">=</span><span class="n">noise_level</span><span class="p">,</span>
<span class="n">show_next_point</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">show_acq_func</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<a href="https://matplotlib.org/api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">show</span></a><span class="p">()</span>
</pre></div>
</div>
<img src="../_images/sphx_glr_ask-and-tell_003.png" srcset="../_images/sphx_glr_ask-and-tell_003.png" alt="x* = -0.3201, f(x*) = -0.9482" class = "sphx-glr-single-img"/><p>By using the <a class="reference internal" href="../modules/generated/skopt.Optimizer.html#skopt.Optimizer" title="skopt.Optimizer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Optimizer</span></code></a> class directly you get control over the
optimization loop.</p>
<p>You can also pickle your <a class="reference internal" href="../modules/generated/skopt.Optimizer.html#skopt.Optimizer" title="skopt.Optimizer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Optimizer</span></code></a> instance if you want to end the
process running it and resume it later. This is handy if your experiment
takes a very long time and you want to shutdown your computer in the
meantime:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pickle</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'my-optimizer.pkl'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<a href="https://docs.python.org/3/library/pickle.html#pickle.dump" title="pickle.dump" class="sphx-glr-backref-module-pickle sphx-glr-backref-type-py-function"><span class="n">pickle</span><span class="o">.</span><span class="n">dump</span></a><span class="p">(</span><span class="n">opt</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'my-optimizer.pkl'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">opt_restored</span> <span class="o">=</span> <a href="https://docs.python.org/3/library/pickle.html#pickle.load" title="pickle.load" class="sphx-glr-backref-module-pickle sphx-glr-backref-type-py-function"><span class="n">pickle</span><span class="o">.</span><span class="n">load</span></a><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p class="sphx-glr-timing"><strong>Total running time of the script:</strong> ( 0 minutes 2.654 seconds)</p>
<p><strong>Estimated memory usage:</strong> 15 MB</p>
<div class="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-auto-examples-ask-and-tell-py">
<div class="binder-badge docutils container">
<a class="reference external image-reference" href="https://mybinder.org/v2/gh/scikit-optimize/scikit-optimize/master?urlpath=lab/tree/notebooks/auto_examples/ask-and-tell.ipynb"><img alt="Launch binder" src="../_images/binder_badge_logo.svg" width="150px" /></a>
</div>
<div class="sphx-glr-download sphx-glr-download-python docutils container">
<p><a class="reference download internal" download="" href="../_downloads/55bac0b8fb5ddffe9b26fe03c5087713/ask-and-tell.py"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">ask-and-tell.py</span></code></a></p>
</div>
<div class="sphx-glr-download sphx-glr-download-jupyter docutils container">
<p><a class="reference download internal" download="" href="../_downloads/16e24a5afc2d92e961c3c58e7d73c6e6/ask-and-tell.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">ask-and-tell.ipynb</span></code></a></p>
</div>
</div>
<p class="sphx-glr-signature"><a class="reference external" href="https://sphinx-gallery.github.io">Gallery generated by Sphinx-Gallery</a></p>
</section>
</section>
</div>
<div class="container">
<footer class="sk-content-footer">
© 2017 - 2020, scikit-optimize contributors (BSD License).
<a href="../_sources/auto_examples/ask-and-tell.rst.txt" rel="nofollow">Show this page source</a>
</footer>
</div>
</div>
</div>
<script src="../_static/js/vendor/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
/* Add a [>>>] button on the top-right corner of code sampler to hide
* the >>> and ... prompts and the output and thus make the code
* copyable. */
var div = $('.highlight-python .highlight,' +
'.highlight-python3 .highlight,' +
'.highlight-pycon .highlight,' +
'.highlight-default .highlight')
var pre = div.find('pre');
// get the styles from the current theme
pre.parent().parent().css('position', 'relative');
var hide_text = 'Hide prompts and outputs';
var show_text = 'Show prompts and outputs';
// create and add the button to all the code blocks that contain >>>
div.each(function(index) {
var jthis = $(this);
if (jthis.find('.gp').length > 0) {
var button = $('<span class="copybutton">>>></span>');
button.attr('title', hide_text);
button.data('hidden', 'false');
jthis.prepend(button);
}
// tracebacks (.gt) contain bare text elements that need to be
// wrapped in a span to work with .nextUntil() (see later)
jthis.find('pre:has(.gt)').contents().filter(function() {
return ((this.nodeType == 3) && (this.data.trim().length > 0));
}).wrap('<span>');
});
// define the behavior of the button when it's clicked
$('.copybutton').click(function(e){
e.preventDefault();
var button = $(this);
if (button.data('hidden') === 'false') {
// hide the code output
button.parent().find('.go, .gp, .gt').hide();
button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
button.css('text-decoration', 'line-through');
button.attr('title', show_text);
button.data('hidden', 'true');
} else {
// show the code output
button.parent().find('.go, .gp, .gt').show();
button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
button.css('text-decoration', 'none');
button.attr('title', hide_text);
button.data('hidden', 'false');
}
});
/*** Add permalink buttons next to glossary terms ***/
$('dl.glossary > dt[id]').append(function() {
return ('<a class="headerlink" href="#' +
this.getAttribute('id') +
'" title="Permalink to this term">¶</a>');
});
/*** Hide navbar when scrolling down ***/
// Returns true when headerlink target matches hash in url
(function() {
hashTargetOnTop = function() {
var hash = window.location.hash;
if ( hash.length < 2 ) { return false; }
var target = document.getElementById( hash.slice(1) );
if ( target === null ) { return false; }
var top = target.getBoundingClientRect().top;
return (top < 2) && (top > -2);
};
// Hide navbar on load if hash target is on top
var navBar = document.getElementById("navbar");
var navBarToggler = document.getElementById("sk-navbar-toggler");
var navBarHeightHidden = "-" + navBar.getBoundingClientRect().height + "px";
var $window = $(window);
hideNavBar = function() {
navBar.style.top = navBarHeightHidden;
};
showNavBar = function() {
navBar.style.top = "0";
}
if (hashTargetOnTop()) {
hideNavBar()
}
var prevScrollpos = window.pageYOffset;
hideOnScroll = function(lastScrollTop) {
if (($window.width() < 768) && (navBarToggler.getAttribute("aria-expanded") === 'true')) {
return;
}
if (lastScrollTop > 2 && (prevScrollpos <= lastScrollTop) || hashTargetOnTop()){
hideNavBar()
} else {
showNavBar()
}
prevScrollpos = lastScrollTop;
};
/*** high performance scroll event listener***/
var raf = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame;
var lastScrollTop = $window.scrollTop();
if (raf) {
loop();
}
function loop() {
var scrollTop = $window.scrollTop();
if (lastScrollTop === scrollTop) {
raf(loop);
return;
} else {
lastScrollTop = scrollTop;
hideOnScroll(lastScrollTop);
raf(loop);
}
}
})();
});
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<script src="https://scikit-optimize.github.io/versionwarning.js"></script>
</body>
</html>