... | ... |
@@ -14,7 +14,7 @@ Tutorial `~adaptive.DataSaver` |
14 | 14 |
:hide-code: |
15 | 15 |
|
16 | 16 |
import adaptive |
17 |
- adaptive.notebook_extension(_inline_js=False) |
|
17 |
+ adaptive.notebook_extension() |
|
18 | 18 |
|
19 | 19 |
If the function that you want to learn returns a value along with some |
20 | 20 |
metadata, you can wrap your learner in an `adaptive.DataSaver`. |
... | ... |
@@ -10,8 +10,6 @@ Tutorial `~adaptive.DataSaver` |
10 | 10 |
The complete source code of this tutorial can be found in |
11 | 11 |
:jupyter-download:notebook:`tutorial.DataSaver` |
12 | 12 |
|
13 |
-.. thebe-button:: Run the code live inside the documentation! |
|
14 |
- |
|
15 | 13 |
.. jupyter-execute:: |
16 | 14 |
:hide-code: |
17 | 15 |
|
... | ... |
@@ -10,6 +10,8 @@ Tutorial `~adaptive.DataSaver` |
10 | 10 |
The complete source code of this tutorial can be found in |
11 | 11 |
:jupyter-download:notebook:`tutorial.DataSaver` |
12 | 12 |
|
13 |
+.. thebe-button:: Run the code live inside the documentation! |
|
14 |
+ |
|
13 | 15 |
.. jupyter-execute:: |
14 | 16 |
:hide-code: |
15 | 17 |
|
... | ... |
@@ -14,7 +14,7 @@ Tutorial `~adaptive.DataSaver` |
14 | 14 |
:hide-code: |
15 | 15 |
|
16 | 16 |
import adaptive |
17 |
- adaptive.notebook_extension() |
|
17 |
+ adaptive.notebook_extension(_inline_js=False) |
|
18 | 18 |
|
19 | 19 |
If the function that you want to learn returns a value along with some |
20 | 20 |
metadata, you can wrap your learner in an `adaptive.DataSaver`. |
... | ... |
@@ -8,11 +8,10 @@ Tutorial `~adaptive.DataSaver` |
8 | 8 |
|
9 | 9 |
.. seealso:: |
10 | 10 |
The complete source code of this tutorial can be found in |
11 |
- :jupyter-download:notebook:`DataSaver` |
|
11 |
+ :jupyter-download:notebook:`tutorial.DataSaver` |
|
12 | 12 |
|
13 |
-.. execute:: |
|
13 |
+.. jupyter-execute:: |
|
14 | 14 |
:hide-code: |
15 |
- :new-notebook: DataSaver |
|
16 | 15 |
|
17 | 16 |
import adaptive |
18 | 17 |
adaptive.notebook_extension() |
... | ... |
@@ -23,7 +22,7 @@ metadata, you can wrap your learner in an `adaptive.DataSaver`. |
23 | 22 |
In the following example the function to be learned returns its result |
24 | 23 |
and the execution time in a dictionary: |
25 | 24 |
|
26 |
-.. execute:: |
|
25 |
+.. jupyter-execute:: |
|
27 | 26 |
|
28 | 27 |
from operator import itemgetter |
29 | 28 |
|
... | ... |
@@ -48,20 +47,20 @@ and the execution time in a dictionary: |
48 | 47 |
``learner.learner`` is the original learner, so |
49 | 48 |
``learner.learner.loss()`` will call the correct loss method. |
50 | 49 |
|
51 |
-.. execute:: |
|
50 |
+.. jupyter-execute:: |
|
52 | 51 |
|
53 | 52 |
runner = adaptive.Runner(learner, goal=lambda l: l.learner.loss() < 0.1) |
54 | 53 |
|
55 |
-.. execute:: |
|
54 |
+.. jupyter-execute:: |
|
56 | 55 |
:hide-code: |
57 | 56 |
|
58 | 57 |
await runner.task # This is not needed in a notebook environment! |
59 | 58 |
|
60 |
-.. execute:: |
|
59 |
+.. jupyter-execute:: |
|
61 | 60 |
|
62 | 61 |
runner.live_info() |
63 | 62 |
|
64 |
-.. execute:: |
|
63 |
+.. jupyter-execute:: |
|
65 | 64 |
|
66 | 65 |
runner.live_plot(plotter=lambda l: l.learner.plot(), update_interval=0.1) |
67 | 66 |
|
... | ... |
@@ -69,6 +68,6 @@ Now the ``DataSavingLearner`` will have an dictionary attribute |
69 | 68 |
``extra_data`` that has ``x`` as key and the data that was returned by |
70 | 69 |
``learner.function`` as values. |
71 | 70 |
|
72 |
-.. execute:: |
|
71 |
+.. jupyter-execute:: |
|
73 | 72 |
|
74 | 73 |
learner.extra_data |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,74 @@ |
1 |
+Tutorial `~adaptive.DataSaver` |
|
2 |
+------------------------------ |
|
3 |
+ |
|
4 |
+.. note:: |
|
5 |
+ Because this documentation consists of static html, the ``live_plot`` |
|
6 |
+ and ``live_info`` widget is not live. Download the notebook |
|
7 |
+ in order to see the real behaviour. |
|
8 |
+ |
|
9 |
+.. seealso:: |
|
10 |
+ The complete source code of this tutorial can be found in |
|
11 |
+ :jupyter-download:notebook:`DataSaver` |
|
12 |
+ |
|
13 |
+.. execute:: |
|
14 |
+ :hide-code: |
|
15 |
+ :new-notebook: DataSaver |
|
16 |
+ |
|
17 |
+ import adaptive |
|
18 |
+ adaptive.notebook_extension() |
|
19 |
+ |
|
20 |
+If the function that you want to learn returns a value along with some |
|
21 |
+metadata, you can wrap your learner in an `adaptive.DataSaver`. |
|
22 |
+ |
|
23 |
+In the following example the function to be learned returns its result |
|
24 |
+and the execution time in a dictionary: |
|
25 |
+ |
|
26 |
+.. execute:: |
|
27 |
+ |
|
28 |
+ from operator import itemgetter |
|
29 |
+ |
|
30 |
+ def f_dict(x): |
|
31 |
+ """The function evaluation takes roughly the time we `sleep`.""" |
|
32 |
+ import random |
|
33 |
+ from time import sleep |
|
34 |
+ |
|
35 |
+ waiting_time = random.random() |
|
36 |
+ sleep(waiting_time) |
|
37 |
+ a = 0.01 |
|
38 |
+ y = x + a**2 / (a**2 + x**2) |
|
39 |
+ return {'y': y, 'waiting_time': waiting_time} |
|
40 |
+ |
|
41 |
+ # Create the learner with the function that returns a 'dict' |
|
42 |
+ # This learner cannot be run directly, as Learner1D does not know what to do with the 'dict' |
|
43 |
+ _learner = adaptive.Learner1D(f_dict, bounds=(-1, 1)) |
|
44 |
+ |
|
45 |
+ # Wrapping the learner with 'adaptive.DataSaver' and tell it which key it needs to learn |
|
46 |
+ learner = adaptive.DataSaver(_learner, arg_picker=itemgetter('y')) |
|
47 |
+ |
|
48 |
+``learner.learner`` is the original learner, so |
|
49 |
+``learner.learner.loss()`` will call the correct loss method. |
|
50 |
+ |
|
51 |
+.. execute:: |
|
52 |
+ |
|
53 |
+ runner = adaptive.Runner(learner, goal=lambda l: l.learner.loss() < 0.1) |
|
54 |
+ |
|
55 |
+.. execute:: |
|
56 |
+ :hide-code: |
|
57 |
+ |
|
58 |
+ await runner.task # This is not needed in a notebook environment! |
|
59 |
+ |
|
60 |
+.. execute:: |
|
61 |
+ |
|
62 |
+ runner.live_info() |
|
63 |
+ |
|
64 |
+.. execute:: |
|
65 |
+ |
|
66 |
+ runner.live_plot(plotter=lambda l: l.learner.plot(), update_interval=0.1) |
|
67 |
+ |
|
68 |
+Now the ``DataSavingLearner`` will have an dictionary attribute |
|
69 |
+``extra_data`` that has ``x`` as key and the data that was returned by |
|
70 |
+``learner.function`` as values. |
|
71 |
+ |
|
72 |
+.. execute:: |
|
73 |
+ |
|
74 |
+ learner.extra_data |