Browse code

add Learner1D example with vector_output=True

Bas Nijholt authored on 08/11/2017 14:10:13
Showing 1 changed files
... ...
@@ -361,6 +361,69 @@
361 361
     "learner.plot()"
362 362
    ]
363 363
   },
364
+  {
365
+   "cell_type": "markdown",
366
+   "metadata": {},
367
+   "source": [
368
+    "# 1D learner with vector output: `f:ℝ → ℝ^N`"
369
+   ]
370
+  },
371
+  {
372
+   "cell_type": "markdown",
373
+   "metadata": {},
374
+   "source": [
375
+    "Some 1D functions return multiple numbers, such as the following function:"
376
+   ]
377
+  },
378
+  {
379
+   "cell_type": "code",
380
+   "execution_count": null,
381
+   "metadata": {},
382
+   "outputs": [],
383
+   "source": [
384
+    "random.seed(0)\n",
385
+    "offsets = [random.uniform(-0.8, 0.8) for _ in range(3)]\n",
386
+    "def f_levels(x, offsets=offsets):\n",
387
+    "    a = 0.01\n",
388
+    "    return np.array([offset + x + a**2 / (a**2 + (x - offset)**2)\n",
389
+    "                     for offset in offsets])"
390
+   ]
391
+  },
392
+  {
393
+   "cell_type": "markdown",
394
+   "metadata": {},
395
+   "source": [
396
+    "This is again a function with sharp peaks at different x-values and with different constant backgrounds. To learn this function we can use a `Learner1D` with the argument `vector_output=True`."
397
+   ]
398
+  },
399
+  {
400
+   "cell_type": "code",
401
+   "execution_count": null,
402
+   "metadata": {},
403
+   "outputs": [],
404
+   "source": [
405
+    "from adaptive.runner import SequentialExecutor\n",
406
+    "\n",
407
+    "learner = adaptive.Learner1D(f_levels, bounds=(-1, 1), vector_output=True)\n",
408
+    "runner = adaptive.Runner(learner, SequentialExecutor(), goal=lambda l: l.loss() < 0.05)"
409
+   ]
410
+  },
411
+  {
412
+   "cell_type": "markdown",
413
+   "metadata": {},
414
+   "source": [
415
+    "In the plot below we see that the function gets more densely sampled around the peaks, which is the behaviour we want."
416
+   ]
417
+  },
418
+  {
419
+   "cell_type": "code",
420
+   "execution_count": null,
421
+   "metadata": {},
422
+   "outputs": [],
423
+   "source": [
424
+    "adaptive.live_plot(runner)"
425
+   ]
426
+  },
364 427
   {
365 428
    "cell_type": "markdown",
366 429
    "metadata": {},