... | ... |
@@ -4,5 +4,6 @@ from .notebook_integration import notebook_extension, live_plot |
4 | 4 |
from . import learner |
5 | 5 |
from . import runner |
6 | 6 |
|
7 |
-from .learner import Learner1D, AverageLearner |
|
7 |
+from .learner import (Learner1D, Learner2D, AverageLearner, |
|
8 |
+ BalancingLearner, DataSaver, IntegratorLearner) |
|
8 | 9 |
from .runner import Runner |
... | ... |
@@ -18,8 +18,8 @@ |
18 | 18 |
"`adaptive` needs the following packages:\n", |
19 | 19 |
"\n", |
20 | 20 |
"+ Python 3.6\n", |
21 |
- "+ holowiews\n", |
|
22 |
- "+ bokeh" |
|
21 |
+ "+ `holoviews`\n", |
|
22 |
+ "+ `bokeh`" |
|
23 | 23 |
] |
24 | 24 |
}, |
25 | 25 |
{ |
... | ... |
@@ -29,7 +29,13 @@ |
29 | 29 |
"outputs": [], |
30 | 30 |
"source": [ |
31 | 31 |
"import adaptive\n", |
32 |
- "adaptive.notebook_extension()" |
|
32 |
+ "adaptive.notebook_extension()\n", |
|
33 |
+ "\n", |
|
34 |
+ "# Import modules that are used in multiple cells\n", |
|
35 |
+ "import holoviews as hv\n", |
|
36 |
+ "import numpy as np\n", |
|
37 |
+ "from functools import partial\n", |
|
38 |
+ "import random" |
|
33 | 39 |
] |
34 | 40 |
}, |
35 | 41 |
{ |
... | ... |
@@ -54,12 +60,9 @@ |
54 | 60 |
"metadata": {}, |
55 | 61 |
"outputs": [], |
56 | 62 |
"source": [ |
57 |
- "from functools import partial\n", |
|
58 |
- "from random import random\n", |
|
63 |
+ "offset = random.uniform(-0.5, 0.5)\n", |
|
59 | 64 |
"\n", |
60 |
- "offset = random() - 0.5\n", |
|
61 |
- "\n", |
|
62 |
- "def f(x, offset=0, wait=True):\n", |
|
65 |
+ "def f(x, offset=offset, wait=True):\n", |
|
63 | 66 |
" from time import sleep\n", |
64 | 67 |
" from random import random\n", |
65 | 68 |
"\n", |
... | ... |
@@ -82,7 +85,7 @@ |
82 | 85 |
"metadata": {}, |
83 | 86 |
"outputs": [], |
84 | 87 |
"source": [ |
85 |
- "learner = adaptive.learner.Learner1D(f, bounds=(-1.0, 1.0))" |
|
88 |
+ "learner = adaptive.Learner1D(f, bounds=(-1, 1))" |
|
86 | 89 |
] |
87 | 90 |
}, |
88 | 91 |
{ |
... | ... |
@@ -145,11 +148,9 @@ |
145 | 148 |
"metadata": {}, |
146 | 149 |
"outputs": [], |
147 | 150 |
"source": [ |
148 |
- "import numpy as np\n", |
|
151 |
+ "learner2 = adaptive.Learner1D(f, bounds=learner.bounds)\n", |
|
149 | 152 |
"\n", |
150 |
- "learner2 = adaptive.learner.Learner1D(f, bounds=(-1.01, 1.0))\n", |
|
151 |
- "\n", |
|
152 |
- "xs = np.linspace(-1.0, 1.0, len(learner.data))\n", |
|
153 |
+ "xs = np.linspace(*learner.bounds, len(learner.data))\n", |
|
153 | 154 |
"learner2.add_data(xs, map(partial(f, wait=False), xs))\n", |
154 | 155 |
"\n", |
155 | 156 |
"learner.plot() + learner2.plot()" |
... | ... |
@@ -175,7 +176,7 @@ |
175 | 176 |
"metadata": {}, |
176 | 177 |
"outputs": [], |
177 | 178 |
"source": [ |
178 |
- "def func(xy, wait=True):\n", |
|
179 |
+ "def ring(xy, wait=True):\n", |
|
179 | 180 |
" import numpy as np\n", |
180 | 181 |
" from time import sleep\n", |
181 | 182 |
" from random import random\n", |
... | ... |
@@ -185,7 +186,7 @@ |
185 | 186 |
" a = 0.2\n", |
186 | 187 |
" return x + np.exp(-(x**2 + y**2 - 0.75**2)**2/a**4)\n", |
187 | 188 |
"\n", |
188 |
- "learner = adaptive.learner.Learner2D(func, bounds=[(-1, 1), (-1, 1)])" |
|
189 |
+ "learner = adaptive.Learner2D(ring, bounds=[(-1, 1), (-1, 1)])" |
|
189 | 190 |
] |
190 | 191 |
}, |
191 | 192 |
{ |
... | ... |
@@ -205,7 +206,6 @@ |
205 | 206 |
"source": [ |
206 | 207 |
"%%output size=100\n", |
207 | 208 |
"%%opts Contours (alpha=0.3)\n", |
208 |
- "import holoviews as hv\n", |
|
209 | 209 |
"\n", |
210 | 210 |
"def plot(learner):\n", |
211 | 211 |
" tri = learner.ip().tri\n", |
... | ... |
@@ -227,11 +227,12 @@ |
227 | 227 |
"metadata": {}, |
228 | 228 |
"outputs": [], |
229 | 229 |
"source": [ |
230 |
- "import numpy as np\n", |
|
231 |
- "learner2 = adaptive.learner.Learner2D(func, bounds=[(-1, 1), (-1, 1)])\n", |
|
232 |
- "lin = np.linspace(-1, 1, len(learner.points)**0.5)\n", |
|
233 |
- "xy = [(x, y) for x in lin for y in lin]\n", |
|
234 |
- "learner2.add_data(xy, map(partial(func, wait=False), xy))\n", |
|
230 |
+ "import itertools\n", |
|
231 |
+ "learner2 = adaptive.Learner2D(ring, bounds=learner.bounds)\n", |
|
232 |
+ "xs = np.linspace(*learner.bounds[0], learner.n**0.5)\n", |
|
233 |
+ "ys = np.linspace(*learner.bounds[1], learner.n**0.5)\n", |
|
234 |
+ "xys = list(itertools.product(xs, ys))\n", |
|
235 |
+ "learner2.add_data(xys, map(partial(ring, wait=False), xys))\n", |
|
235 | 236 |
"learner2.plot().relabel('Homogeneous grid') + learner.plot().relabel('With adaptive')" |
236 | 237 |
] |
237 | 238 |
}, |
... | ... |
@@ -276,7 +277,7 @@ |
276 | 277 |
"metadata": {}, |
277 | 278 |
"outputs": [], |
278 | 279 |
"source": [ |
279 |
- "learner = adaptive.learner.AverageLearner(g, None, 0.01)\n", |
|
280 |
+ "learner = adaptive.AverageLearner(g, atol=None, rtol=0.01)\n", |
|
280 | 281 |
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 1)\n", |
281 | 282 |
"adaptive.live_plot(runner)" |
282 | 283 |
] |
... | ... |
@@ -303,9 +304,6 @@ |
303 | 304 |
"metadata": {}, |
304 | 305 |
"outputs": [], |
305 | 306 |
"source": [ |
306 |
- "import numpy as np\n", |
|
307 |
- "import holoviews as hv\n", |
|
308 |
- "\n", |
|
309 | 307 |
"def f24(x):\n", |
310 | 308 |
" return np.floor(np.exp(x))\n", |
311 | 309 |
"\n", |
... | ... |
@@ -343,8 +341,9 @@ |
343 | 341 |
"metadata": {}, |
344 | 342 |
"outputs": [], |
345 | 343 |
"source": [ |
346 |
- "learner = adaptive.learner.IntegratorLearner(f24, bounds=(0, 3), tol=1e-10)\n", |
|
347 |
- "runner = adaptive.Runner(learner, executor=adaptive.runner.SequentialExecutor(), goal=lambda l: l.done())" |
|
344 |
+ "from adaptive.runner import SequentialExecutor\n", |
|
345 |
+ "learner = adaptive.IntegratorLearner(f24, bounds=(0, 3), tol=1e-10)\n", |
|
346 |
+ "runner = adaptive.Runner(learner, executor=SequentialExecutor(), goal=lambda l: l.done())" |
|
348 | 347 |
] |
349 | 348 |
}, |
350 | 349 |
{ |
... | ... |
@@ -396,11 +395,15 @@ |
396 | 395 |
"metadata": {}, |
397 | 396 |
"outputs": [], |
398 | 397 |
"source": [ |
399 |
- "from adaptive.learner import Learner1D, BalancingLearner\n", |
|
398 |
+ "def f(x, offset):\n", |
|
399 |
+ " a = 0.01\n", |
|
400 |
+ " return x + a**2 / (a**2 + (x - offset)**2)\n", |
|
401 |
+ "\n", |
|
402 |
+ "learners = [adaptive.Learner1D(partial(f, offset=random.uniform(-1, 1)),\n", |
|
403 |
+ " bounds=(-1, 1)) for i in range(10)]\n", |
|
400 | 404 |
"\n", |
401 |
- "learners = [Learner1D(partial(f, offset=2*random()-1, wait=False), bounds=(-1.0, 1.0)) for i in range(10)]\n", |
|
402 |
- "learner = BalancingLearner(learners)\n", |
|
403 |
- "runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.02)" |
|
405 |
+ "bal_learner = adaptive.BalancingLearner(learners)\n", |
|
406 |
+ "runner = adaptive.Runner(bal_learner, goal=lambda l: l.loss() < 0.01)" |
|
404 | 407 |
] |
405 | 408 |
}, |
406 | 409 |
{ |
... | ... |
@@ -409,8 +412,8 @@ |
409 | 412 |
"metadata": {}, |
410 | 413 |
"outputs": [], |
411 | 414 |
"source": [ |
412 |
- "import holoviews as hv\n", |
|
413 |
- "adaptive.live_plot(runner, plotter=lambda learner: hv.Overlay([L.plot() for L in learner.learners]))" |
|
415 |
+ "plotter = lambda learner: hv.Overlay([L.plot() for L in learner.learners])\n", |
|
416 |
+ "adaptive.live_plot(runner, plotter=plotter)" |
|
414 | 417 |
] |
415 | 418 |
}, |
416 | 419 |
{ |
... | ... |
@@ -437,10 +440,9 @@ |
437 | 440 |
"metadata": {}, |
438 | 441 |
"outputs": [], |
439 | 442 |
"source": [ |
440 |
- "from adaptive.learner import DataSaver, Learner1D\n", |
|
441 | 443 |
"from operator import itemgetter\n", |
442 | 444 |
"\n", |
443 |
- "def f(x):\n", |
|
445 |
+ "def f_dict(x):\n", |
|
444 | 446 |
" \"\"\"The function evaluation takes roughly the time we `sleep`.\"\"\"\n", |
445 | 447 |
" import random\n", |
446 | 448 |
" from time import sleep\n", |
... | ... |
@@ -453,10 +455,10 @@ |
453 | 455 |
"\n", |
454 | 456 |
"# Create the learner with the function that returns a `dict`\n", |
455 | 457 |
"# Note that this learner cannot be passed to a runner.\n", |
456 |
- "_learner = Learner1D(f, bounds=(-1.0, 1.0))\n", |
|
458 |
+ "_learner = adaptive.Learner1D(f_dict, bounds=(-1, 1))\n", |
|
457 | 459 |
"\n", |
458 | 460 |
"# Wrap the learner in the `DataSavingLearner` and tell it which key it needs to learn\n", |
459 |
- "learner = DataSaver(_learner, arg_picker=itemgetter('y'))" |
|
461 |
+ "learner = adaptive.DataSaver(_learner, arg_picker=itemgetter('y'))" |
|
460 | 462 |
] |
461 | 463 |
}, |
462 | 464 |
{ |
... | ... |
@@ -548,7 +550,7 @@ |
548 | 550 |
"\n", |
549 | 551 |
"executor = ProcessPoolExecutor(max_workers=4)\n", |
550 | 552 |
"\n", |
551 |
- "learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))\n", |
|
553 |
+ "learner = adaptive.Learner1D(f, bounds=(-1, 1))\n", |
|
552 | 554 |
"runner = adaptive.Runner(learner, executor=executor, goal=lambda l: l.loss() < 0.1)\n", |
553 | 555 |
"adaptive.live_plot(runner)" |
554 | 556 |
] |
... | ... |
@@ -572,7 +574,7 @@ |
572 | 574 |
"# f is a closure, so we have to use cloudpickle -- this is independent of 'adaptive'\n", |
573 | 575 |
"client[:].use_cloudpickle()\n", |
574 | 576 |
"\n", |
575 |
- "learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))\n", |
|
577 |
+ "learner = adaptive.Learner1D(f, bounds=(-1, 1))\n", |
|
576 | 578 |
"runner = adaptive.Runner(learner, executor=client, goal=lambda l: l.loss() < 0.1)\n", |
577 | 579 |
"adaptive.live_plot(runner)" |
578 | 580 |
] |
... | ... |
@@ -613,7 +615,7 @@ |
613 | 615 |
"metadata": {}, |
614 | 616 |
"outputs": [], |
615 | 617 |
"source": [ |
616 |
- "learner = adaptive.learner.Learner1D(f, bounds=(-1.0, 1.0))\n", |
|
618 |
+ "learner = adaptive.Learner1D(f, bounds=(-1, 1))\n", |
|
617 | 619 |
"runner = adaptive.Runner(learner)\n", |
618 | 620 |
"adaptive.live_plot(runner)" |
619 | 621 |
] |
... | ... |
@@ -717,7 +719,7 @@ |
717 | 719 |
"metadata": {}, |
718 | 720 |
"outputs": [], |
719 | 721 |
"source": [ |
720 |
- "learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))\n", |
|
722 |
+ "learner = adaptive.Learner1D(f, bounds=(-1, 1))\n", |
|
721 | 723 |
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.1,\n", |
722 | 724 |
" log=True)\n", |
723 | 725 |
"adaptive.live_plot(runner)" |
... | ... |
@@ -738,7 +740,7 @@ |
738 | 740 |
"metadata": {}, |
739 | 741 |
"outputs": [], |
740 | 742 |
"source": [ |
741 |
- "reconstructed_learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))\n", |
|
743 |
+ "reconstructed_learner = adaptive.Learner1D(f, bounds=(-1, 1))\n", |
|
742 | 744 |
"adaptive.runner.replay_log(reconstructed_learner, runner.log)" |
743 | 745 |
] |
744 | 746 |
}, |
... | ... |
@@ -789,9 +791,8 @@ |
789 | 791 |
"\n", |
790 | 792 |
"ioloop = asyncio.get_event_loop()\n", |
791 | 793 |
"\n", |
792 |
- "learner = adaptive.learner.IntegratorLearner(f24, bounds=(0, 3), tol=1e-3)\n", |
|
793 |
- "runner = adaptive.Runner(learner, executor=adaptive.runner.SequentialExecutor(),\n", |
|
794 |
- " goal=lambda l: l.done())\n", |
|
794 |
+ "learner = adaptive.Learner1D(f, bounds=(-1, 1))\n", |
|
795 |
+ "runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.01)\n", |
|
795 | 796 |
"\n", |
796 | 797 |
"timer = ioloop.create_task(time(runner))" |
797 | 798 |
] |