Tutorial SKOptLearner
¶
Note
Because this documentation consists of static html, the live_plot
and live_info
widget is not live. Download the notebook
in order to see the real behaviour.
See also
The complete source code of this tutorial can be found in tutorial.SKOptLearner.ipynb
We have wrapped the Optimizer
class from
scikit-optimize,
to show how existing libraries can be integrated with adaptive
.
The SKOptLearner
attempts to “optimize” the given function g
(i.e. find the global minimum of g
in the window of interest).
Here we use the same example as in the scikit-optimize
tutorial.
Although SKOptLearner
can optimize functions of arbitrary
dimensionality, we can only plot the learner if a 1D function is being
learned.
def F(x, noise_level=0.1):
return (np.sin(5 * x) * (1 - np.tanh(x ** 2))
+ np.random.randn() * noise_level)
learner = adaptive.SKOptLearner(F, dimensions=[(-2., 2.)],
base_estimator="GP",
acq_func="gp_hedge",
acq_optimizer="lbfgs",
)
runner = adaptive.Runner(learner, ntasks=1, goal=lambda l: l.npoints > 40)
runner.live_info()
%%opts Overlay [legend_position='top']
xs = np.linspace(*learner.space.bounds[0])
to_learn = hv.Curve((xs, [F(x, 0) for x in xs]), label='to learn')
runner.live_plot().relabel('prediction', depth=2) * to_learn
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
968
969 if method is not None:
--> 970 return method(include=include, exclude=exclude)
971 return None
972 else:
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/core/dimension.py in _repr_mimebundle_(self, include, exclude)
1391 combined and returned.
1392 """
-> 1393 return Store.render(self)
1394
1395
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/core/options.py in render(cls, obj)
1451 data, metadata = {}, {}
1452 for hook in hooks:
-> 1453 ret = hook(obj)
1454 if ret is None:
1455 continue
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in pprint_display(obj)
270 if not ip.display_formatter.formatters['text/plain'].pprint:
271 return None
--> 272 return display(obj, raw_output=True)
273
274
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in display(obj, raw_output, **kwargs)
246 elif isinstance(obj, (HoloMap, DynamicMap)):
247 with option_state(obj):
--> 248 output = map_display(obj)
249 elif isinstance(obj, Plot):
250 output = render(obj)
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in wrapped(element)
140 try:
141 max_frames = OutputSettings.options['max_frames']
--> 142 mimebundle = fn(element, max_frames=max_frames)
143 if mimebundle is None:
144 return {}, {}
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in map_display(vmap, max_frames)
200 return None
201
--> 202 return render(vmap)
203
204
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in render(obj, **kwargs)
63 renderer = renderer.instance(fig='png')
64
---> 65 return renderer.components(obj, **kwargs)
66
67
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/bokeh/renderer.py in components(self, obj, fmt, comm, **kwargs)
247 # Bokeh has to handle comms directly in <0.12.15
248 comm = False if bokeh_version < '0.12.15' else comm
--> 249 return super(BokehRenderer, self).components(obj,fmt, comm, **kwargs)
250
251
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/renderer.py in components(self, obj, fmt, comm, **kwargs)
319 plot = obj
320 else:
--> 321 plot, fmt = self._validate(obj, fmt)
322
323 widget_id = None
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/renderer.py in _validate(self, obj, fmt, **kwargs)
218 if isinstance(obj, tuple(self.widgets.values())):
219 return obj, 'html'
--> 220 plot = self.get_plot(obj, renderer=self, **kwargs)
221
222 fig_formats = self.mode_formats['fig'][self.mode]
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/bokeh/renderer.py in get_plot(self_or_cls, obj, doc, renderer, **kwargs)
132 curdoc().theme = self_or_cls.theme
133 doc.theme = self_or_cls.theme
--> 134 plot = super(BokehRenderer, self_or_cls).get_plot(obj, renderer, **kwargs)
135 plot.document = doc
136 return plot
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/renderer.py in get_plot(self_or_cls, obj, renderer, **kwargs)
205 init_key = tuple(v if d is None else d for v, d in
206 zip(plot.keys[0], defaults))
--> 207 plot.update(init_key)
208 else:
209 plot = obj
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/plot.py in update(self, key)
593 def update(self, key):
594 if len(self) == 1 and ((key == 0) or (key == self.keys[0])) and not self.drawn:
--> 595 return self.initialize_plot()
596 item = self.__getitem__(key)
597 self.traverse(lambda x: setattr(x, '_updated', True))
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/bokeh/element.py in initialize_plot(self, ranges, plot, plots)
1847 self.handles['plot'] = Tabs(tabs=panels)
1848 elif not self.overlaid:
-> 1849 self._process_legend()
1850 self._set_active_tools(plot)
1851 self.drawn = True
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/holoviews/plotting/bokeh/element.py in _process_legend(self)
1720 elif pos in ['above', 'below', 'right', 'left']:
1721 plot.legend.pop(plot.legend.index(legend))
-> 1722 legend.plot = None
1723 legend.location = self.legend_offset
1724 plot.add_layout(legend, pos)
~/checkouts/readthedocs.org/user_builds/adaptive/conda/v0.8.1/lib/python3.6/site-packages/bokeh/core/has_props.py in __setattr__(self, name, value)
286
287 raise AttributeError("unexpected attribute '%s' to %s, %s attributes are %s" %
--> 288 (name, self.__class__.__name__, text, nice_join(matches)))
289
290 def __str__(self):
AttributeError: unexpected attribute 'plot' to Legend, possible attributes are background_fill_alpha, background_fill_color, border_line_alpha, border_line_cap, border_line_color, border_line_dash, border_line_dash_offset, border_line_join, border_line_width, click_policy, glyph_height, glyph_width, inactive_fill_alpha, inactive_fill_color, items, js_event_callbacks, js_property_callbacks, label_height, label_standoff, label_text_align, label_text_alpha, label_text_baseline, label_text_color, label_text_font, label_text_font_size, label_text_font_style, label_text_line_height, label_width, level, location, margin, name, orientation, padding, spacing, subscribed_events, tags or visible
:DynamicMap []
:Overlay
.Scatter.I :Scatter [x] (y)
.Curve.I :Curve [x] (y)
.Area.I :Area [x] (y,y2)
.Curve.To_learn :Curve [x] (y)