... | ... |
@@ -10,8 +10,6 @@ Tutorial `~adaptive.Learner2D` |
10 | 10 |
The complete source code of this tutorial can be found in |
11 | 11 |
:jupyter-download:notebook:`tutorial.Learner2D` |
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.Learner2D` |
10 | 10 |
The complete source code of this tutorial can be found in |
11 | 11 |
:jupyter-download:notebook:`tutorial.Learner2D` |
12 | 12 |
|
13 |
+.. thebe-button:: Run the code live inside the documentation! |
|
14 |
+ |
|
13 | 15 |
.. jupyter-execute:: |
14 | 16 |
:hide-code: |
15 | 17 |
|
... | ... |
@@ -8,11 +8,10 @@ Tutorial `~adaptive.Learner2D` |
8 | 8 |
|
9 | 9 |
.. seealso:: |
10 | 10 |
The complete source code of this tutorial can be found in |
11 |
- :jupyter-download:notebook:`Learner2D` |
|
11 |
+ :jupyter-download:notebook:`tutorial.Learner2D` |
|
12 | 12 |
|
13 |
-.. execute:: |
|
13 |
+.. jupyter-execute:: |
|
14 | 14 |
:hide-code: |
15 |
- :new-notebook: Learner2D |
|
16 | 15 |
|
17 | 16 |
import adaptive |
18 | 17 |
adaptive.notebook_extension() |
... | ... |
@@ -23,7 +22,7 @@ Tutorial `~adaptive.Learner2D` |
23 | 22 |
Besides 1D functions, we can also learn 2D functions: |
24 | 23 |
:math:`\ f: ℝ^2 → ℝ`. |
25 | 24 |
|
26 |
-.. execute:: |
|
25 |
+.. jupyter-execute:: |
|
27 | 26 |
|
28 | 27 |
def ring(xy, wait=True): |
29 | 28 |
import numpy as np |
... | ... |
@@ -37,20 +36,20 @@ Besides 1D functions, we can also learn 2D functions: |
37 | 36 |
|
38 | 37 |
learner = adaptive.Learner2D(ring, bounds=[(-1, 1), (-1, 1)]) |
39 | 38 |
|
40 |
-.. execute:: |
|
39 |
+.. jupyter-execute:: |
|
41 | 40 |
|
42 | 41 |
runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.01) |
43 | 42 |
|
44 |
-.. execute:: |
|
43 |
+.. jupyter-execute:: |
|
45 | 44 |
:hide-code: |
46 | 45 |
|
47 | 46 |
await runner.task # This is not needed in a notebook environment! |
48 | 47 |
|
49 |
-.. execute:: |
|
48 |
+.. jupyter-execute:: |
|
50 | 49 |
|
51 | 50 |
runner.live_info() |
52 | 51 |
|
53 |
-.. execute:: |
|
52 |
+.. jupyter-execute:: |
|
54 | 53 |
|
55 | 54 |
def plot(learner): |
56 | 55 |
plot = learner.plot(tri_alpha=0.2) |
... | ... |
@@ -58,7 +57,7 @@ Besides 1D functions, we can also learn 2D functions: |
58 | 57 |
|
59 | 58 |
runner.live_plot(plotter=plot, update_interval=0.1) |
60 | 59 |
|
61 |
-.. execute:: |
|
60 |
+.. jupyter-execute:: |
|
62 | 61 |
|
63 | 62 |
%%opts EdgePaths (color='w') |
64 | 63 |
|
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,75 @@ |
1 |
+Tutorial `~adaptive.Learner2D` |
|
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:`Learner2D` |
|
12 |
+ |
|
13 |
+.. execute:: |
|
14 |
+ :hide-code: |
|
15 |
+ :new-notebook: Learner2D |
|
16 |
+ |
|
17 |
+ import adaptive |
|
18 |
+ adaptive.notebook_extension() |
|
19 |
+ |
|
20 |
+ import numpy as np |
|
21 |
+ from functools import partial |
|
22 |
+ |
|
23 |
+Besides 1D functions, we can also learn 2D functions: |
|
24 |
+:math:`\ f: ℝ^2 → ℝ`. |
|
25 |
+ |
|
26 |
+.. execute:: |
|
27 |
+ |
|
28 |
+ def ring(xy, wait=True): |
|
29 |
+ import numpy as np |
|
30 |
+ from time import sleep |
|
31 |
+ from random import random |
|
32 |
+ if wait: |
|
33 |
+ sleep(random()/10) |
|
34 |
+ x, y = xy |
|
35 |
+ a = 0.2 |
|
36 |
+ return x + np.exp(-(x**2 + y**2 - 0.75**2)**2/a**4) |
|
37 |
+ |
|
38 |
+ learner = adaptive.Learner2D(ring, bounds=[(-1, 1), (-1, 1)]) |
|
39 |
+ |
|
40 |
+.. execute:: |
|
41 |
+ |
|
42 |
+ runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.01) |
|
43 |
+ |
|
44 |
+.. execute:: |
|
45 |
+ :hide-code: |
|
46 |
+ |
|
47 |
+ await runner.task # This is not needed in a notebook environment! |
|
48 |
+ |
|
49 |
+.. execute:: |
|
50 |
+ |
|
51 |
+ runner.live_info() |
|
52 |
+ |
|
53 |
+.. execute:: |
|
54 |
+ |
|
55 |
+ def plot(learner): |
|
56 |
+ plot = learner.plot(tri_alpha=0.2) |
|
57 |
+ return (plot.Image + plot.EdgePaths.I + plot).cols(2) |
|
58 |
+ |
|
59 |
+ runner.live_plot(plotter=plot, update_interval=0.1) |
|
60 |
+ |
|
61 |
+.. execute:: |
|
62 |
+ |
|
63 |
+ %%opts EdgePaths (color='w') |
|
64 |
+ |
|
65 |
+ import itertools |
|
66 |
+ |
|
67 |
+ # Create a learner and add data on homogeneous grid, so that we can plot it |
|
68 |
+ learner2 = adaptive.Learner2D(ring, bounds=learner.bounds) |
|
69 |
+ n = int(learner.npoints**0.5) |
|
70 |
+ xs, ys = [np.linspace(*bounds, n) for bounds in learner.bounds] |
|
71 |
+ xys = list(itertools.product(xs, ys)) |
|
72 |
+ learner2.tell_many(xys, map(partial(ring, wait=False), xys)) |
|
73 |
+ |
|
74 |
+ (learner2.plot(n).relabel('Homogeneous grid') + learner.plot().relabel('With adaptive') + |
|
75 |
+ learner2.plot(n, tri_alpha=0.4) + learner.plot(tri_alpha=0.4)).cols(2) |