-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tutorial' into 'master'
add tutorials See merge request qt/adaptive!129
- Loading branch information
Showing
22 changed files
with
1,354 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
build/* | ||
source/_static/holoviews.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// This file is empty because we are already loading jQuery with holoviews in the <head> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% extends "!layout.html" %} | ||
{% block extrahead %} | ||
{%- for scriptfile in holoviews_js_files %} | ||
{{ js_tag(scriptfile) }} | ||
{%- endfor %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ | |
:hidden: | ||
|
||
rest_of_readme | ||
tutorial/tutorial | ||
reference/adaptive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
adaptive.runner.simple | ||
====================== | ||
|
||
Simple executor | ||
--------------- | ||
|
||
.. autofunction:: adaptive.runner.simple | ||
|
||
Sequential excecutor | ||
-------------------- | ||
|
||
.. autofunction:: adaptive.runner.SequentialExecutor | ||
|
||
|
||
Replay log | ||
---------- | ||
|
||
.. autofunction:: adaptive.runner.replay_log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Tutorial `~adaptive.AverageLearner` | ||
----------------------------------- | ||
|
||
.. 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. | ||
|
||
.. seealso:: | ||
The complete source code of this tutorial can be found in | ||
:jupyter-download:notebook:`AverageLearner` | ||
|
||
.. execute:: | ||
:hide-code: | ||
:new-notebook: AverageLearner | ||
|
||
import adaptive | ||
adaptive.notebook_extension() | ||
|
||
The next type of learner averages a function until the uncertainty in | ||
the average meets some condition. | ||
|
||
This is useful for sampling a random variable. The function passed to | ||
the learner must formally take a single parameter, which should be used | ||
like a “seed” for the (pseudo-) random variable (although in the current | ||
implementation the seed parameter can be ignored by the function). | ||
|
||
.. execute:: | ||
|
||
def g(n): | ||
import random | ||
from time import sleep | ||
sleep(random.random() / 1000) | ||
# Properly save and restore the RNG state | ||
state = random.getstate() | ||
random.seed(n) | ||
val = random.gauss(0.5, 1) | ||
random.setstate(state) | ||
return val | ||
|
||
.. execute:: | ||
|
||
learner = adaptive.AverageLearner(g, atol=None, rtol=0.01) | ||
runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 2) | ||
|
||
.. execute:: | ||
:hide-code: | ||
|
||
await runner.task # This is not needed in a notebook environment! | ||
|
||
.. execute:: | ||
|
||
runner.live_info() | ||
|
||
.. execute:: | ||
|
||
runner.live_plot(update_interval=0.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
Tutorial `~adaptive.BalancingLearner` | ||
------------------------------------- | ||
|
||
.. 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. | ||
|
||
.. seealso:: | ||
The complete source code of this tutorial can be found in | ||
:jupyter-download:notebook:`BalancingLearner` | ||
|
||
.. execute:: | ||
:hide-code: | ||
:new-notebook: BalancingLearner | ||
|
||
import adaptive | ||
adaptive.notebook_extension() | ||
|
||
import holoviews as hv | ||
import numpy as np | ||
from functools import partial | ||
import random | ||
|
||
The balancing learner is a “meta-learner” that takes a list of learners. | ||
When you request a point from the balancing learner, it will query all | ||
of its “children” to figure out which one will give the most | ||
improvement. | ||
|
||
The balancing learner can for example be used to implement a poor-man’s | ||
2D learner by using the `~adaptive.Learner1D`. | ||
|
||
.. execute:: | ||
|
||
def h(x, offset=0): | ||
a = 0.01 | ||
return x + a**2 / (a**2 + (x - offset)**2) | ||
|
||
learners = [adaptive.Learner1D(partial(h, offset=random.uniform(-1, 1)), | ||
bounds=(-1, 1)) for i in range(10)] | ||
|
||
bal_learner = adaptive.BalancingLearner(learners) | ||
runner = adaptive.Runner(bal_learner, goal=lambda l: l.loss() < 0.01) | ||
|
||
.. execute:: | ||
:hide-code: | ||
|
||
await runner.task # This is not needed in a notebook environment! | ||
|
||
.. execute:: | ||
|
||
runner.live_info() | ||
|
||
.. execute:: | ||
|
||
plotter = lambda learner: hv.Overlay([L.plot() for L in learner.learners]) | ||
runner.live_plot(plotter=plotter, update_interval=0.1) | ||
|
||
Often one wants to create a set of ``learner``\ s for a cartesian | ||
product of parameters. For that particular case we’ve added a | ||
``classmethod`` called ``~adaptive.BalancingLearner.from_product``. | ||
See how it works below | ||
|
||
.. execute:: | ||
|
||
from scipy.special import eval_jacobi | ||
|
||
def jacobi(x, n, alpha, beta): return eval_jacobi(n, alpha, beta, x) | ||
|
||
combos = { | ||
'n': [1, 2, 4, 8], | ||
'alpha': np.linspace(0, 2, 3), | ||
'beta': np.linspace(0, 1, 5), | ||
} | ||
|
||
learner = adaptive.BalancingLearner.from_product( | ||
jacobi, adaptive.Learner1D, dict(bounds=(0, 1)), combos) | ||
|
||
runner = adaptive.BlockingRunner(learner, goal=lambda l: l.loss() < 0.01) | ||
|
||
# The `cdims` will automatically be set when using `from_product`, so | ||
# `plot()` will return a HoloMap with correctly labeled sliders. | ||
learner.plot().overlay('beta').grid().select(y=(-1, 3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
Tutorial `~adaptive.DataSaver` | ||
------------------------------ | ||
|
||
.. 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. | ||
|
||
.. seealso:: | ||
The complete source code of this tutorial can be found in | ||
:jupyter-download:notebook:`DataSaver` | ||
|
||
.. execute:: | ||
:hide-code: | ||
:new-notebook: DataSaver | ||
|
||
import adaptive | ||
adaptive.notebook_extension() | ||
|
||
If the function that you want to learn returns a value along with some | ||
metadata, you can wrap your learner in an `adaptive.DataSaver`. | ||
|
||
In the following example the function to be learned returns its result | ||
and the execution time in a dictionary: | ||
|
||
.. execute:: | ||
|
||
from operator import itemgetter | ||
|
||
def f_dict(x): | ||
"""The function evaluation takes roughly the time we `sleep`.""" | ||
import random | ||
from time import sleep | ||
|
||
waiting_time = random.random() | ||
sleep(waiting_time) | ||
a = 0.01 | ||
y = x + a**2 / (a**2 + x**2) | ||
return {'y': y, 'waiting_time': waiting_time} | ||
|
||
# Create the learner with the function that returns a 'dict' | ||
# This learner cannot be run directly, as Learner1D does not know what to do with the 'dict' | ||
_learner = adaptive.Learner1D(f_dict, bounds=(-1, 1)) | ||
|
||
# Wrapping the learner with 'adaptive.DataSaver' and tell it which key it needs to learn | ||
learner = adaptive.DataSaver(_learner, arg_picker=itemgetter('y')) | ||
|
||
``learner.learner`` is the original learner, so | ||
``learner.learner.loss()`` will call the correct loss method. | ||
|
||
.. execute:: | ||
|
||
runner = adaptive.Runner(learner, goal=lambda l: l.learner.loss() < 0.1) | ||
|
||
.. execute:: | ||
:hide-code: | ||
|
||
await runner.task # This is not needed in a notebook environment! | ||
|
||
.. execute:: | ||
|
||
runner.live_info() | ||
|
||
.. execute:: | ||
|
||
runner.live_plot(plotter=lambda l: l.learner.plot(), update_interval=0.1) | ||
|
||
Now the ``DataSavingLearner`` will have an dictionary attribute | ||
``extra_data`` that has ``x`` as key and the data that was returned by | ||
``learner.function`` as values. | ||
|
||
.. execute:: | ||
|
||
learner.extra_data |
Oops, something went wrong.