de7ba87d |
{
"cells": [
{
"cell_type": "markdown",
|
c395daf0 |
"metadata": {},
|
de7ba87d |
"source": [
|
067f64c7 |
"![](docs/source/_static/logo_docs.png)\n",
|
de7ba87d |
"# Adaptive"
]
},
|
9e091088 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
cbfe6144 |
"[`adaptive`](https://github.com/python-adaptive/adaptive) is a package for adaptively sampling functions with support for parallel evaluation.\n",
|
9e091088 |
"\n",
|
067f64c7 |
"This is an introductory notebook that shows some basic use cases."
|
9e091088 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import adaptive\n",
|
60589534 |
"adaptive.notebook_extension()\n",
"\n",
"# Import modules that are used in multiple cells\n",
"import holoviews as hv\n",
"import numpy as np\n",
"from functools import partial\n",
"import random"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1D function learner"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start with the most common use-case: sampling a 1D function $\\ f: ℝ → ℝ$.\n",
"\n",
"We will use the following function, which is a smooth (linear) background with a sharp peak at a random location:"
]
},
|
e3a7009c |
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
e3a7009c |
"outputs": [],
"source": [
|
60589534 |
"offset = random.uniform(-0.5, 0.5)\n",
|
e3a7009c |
"\n",
|
067f64c7 |
"def peak(x, offset=offset, wait=True):\n",
|
18212a81 |
" from time import sleep\n",
|
9e091088 |
" from random import random\n",
|
18212a81 |
"\n",
|
9e091088 |
" a = 0.01\n",
|
067f64c7 |
" if wait: \n",
" # we pretend that this is a slow function\n",
|
fed53617 |
" sleep(random())\n",
|
067f64c7 |
"\n",
|
9e091088 |
" return x + a**2 / (a**2 + (x - offset)**2)"
|
de7ba87d |
]
},
{
"cell_type": "markdown",
|
9e091088 |
"metadata": {},
"source": [
"We start by initializing a 1D \"learner\", which will suggest points to evaluate, and adapt its suggestions as more and more points are evaluated."
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
|
de7ba87d |
"source": [
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
1f0e04fa |
"Next we create a \"runner\" that will request points from the learner and evaluate 'f' on them.\n",
|
9e091088 |
"\n",
|
257a4367 |
"By default on Unix-like systems the runner will evaluate the points in parallel using local processes ([`concurrent.futures.ProcessPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#processpoolexecutor)).\n",
"\n",
"On Windows systems the runner will try to use a [`distributed.Client`](https://distributed.readthedocs.io/en/latest/client.html) if [`distributed`](https://distributed.readthedocs.io/en/latest/index.html) is installed. A `ProcessPoolExecutor` cannot be used on Windows for reasons."
|
de7ba87d |
]
},
|
ea41ee7a |
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
|
f2d0c0ac |
"# The end condition is when the \"loss\" is less than 0.01. In the context of the\n",
"# 1D learner this means that we will resolve features in 'func' with width 0.01 or wider.\n",
|
250b7093 |
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.01)\n",
|
d3337f04 |
"runner.live_info()"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When instantiated in a Jupyter notebook the runner does its job in the background and does not block the IPython kernel.\n",
"We can use this to create a plot that updates as new data arrives:"
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
ea41ee7a |
"outputs": [],
"source": [
|
c4f5c69d |
"runner.live_plot(update_interval=0.1)"
|
ea41ee7a |
]
},
|
9e091088 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now compare the adaptive sampling to a homogeneous sampling with the same number of points:"
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
|
067f64c7 |
"if runner.status() != \"finished\":\n",
|
a34cac81 |
" print(\"WARNING: The runner hasn't reached it goal yet!\")\n",
|
067f64c7 |
"\n",
"learner2 = adaptive.Learner1D(peak, bounds=learner.bounds)\n",
|
c395daf0 |
"\n",
|
60589534 |
"xs = np.linspace(*learner.bounds, len(learner.data))\n",
|
067f64c7 |
"ys = [peak(x, wait=False) for x in xs]\n",
"learner2.tell_many(xs, ys)\n",
|
9e091088 |
"\n",
|
8faeb420 |
"learner.plot() + learner2.plot()"
|
18212a81 |
]
},
{
"cell_type": "markdown",
|
9e091088 |
"metadata": {},
"source": [
|
569168f5 |
"# 2D function learner"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
569168f5 |
"Besides 1D functions, we can also learn 2D functions: $\\ f: ℝ^2 → ℝ$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
60589534 |
"def ring(xy, wait=True):\n",
|
569168f5 |
" import numpy as np\n",
|
beead16a |
" from time import sleep\n",
" from random import random\n",
|
067f64c7 |
"\n",
|
39fca22a |
" if wait:\n",
|
067f64c7 |
" # we pretend that this is a slow function\n",
" sleep(random() / 10)\n",
|
569168f5 |
" x, y = xy\n",
" a = 0.2\n",
|
067f64c7 |
" return x + np.exp(-(x ** 2 + y ** 2 - 0.75 ** 2) ** 2 / a ** 4)\n",
"\n",
|
9e091088 |
"\n",
|
60589534 |
"learner = adaptive.Learner2D(ring, bounds=[(-1, 1), (-1, 1)])"
|
9e091088 |
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
|
18212a81 |
"source": [
|
5b1cd171 |
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.01)\n",
"runner.live_info()"
|
9e091088 |
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
|
15fc16a2 |
"def plot(learner):\n",
|
7a75cbad |
" plot = learner.plot(tri_alpha=0.2)\n",
|
d5774ffc |
" return plot.Image + plot.EdgePaths.I + plot\n",
|
15fc16a2 |
"\n",
|
c4f5c69d |
"runner.live_plot(plotter=plot, update_interval=0.1)"
|
9e091088 |
]
},
|
569168f5 |
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
4b33dbe7 |
"%%opts EdgePaths (color='w')\n",
|
8310cc04 |
"\n",
|
60589534 |
"import itertools\n",
|
8310cc04 |
"\n",
"# Create a learner and add data on homogeneous grid, so that we can plot it\n",
|
60589534 |
"learner2 = adaptive.Learner2D(ring, bounds=learner.bounds)\n",
|
067f64c7 |
"n = int(learner.npoints ** 0.5)\n",
|
7206e381 |
"xs, ys = [np.linspace(*bounds, n) for bounds in learner.bounds]\n",
|
60589534 |
"xys = list(itertools.product(xs, ys))\n",
|
067f64c7 |
"zs = [ring(xy, wait=False) for xy in xys]\n",
"learner2.tell_many(xys, zs)\n",
|
8310cc04 |
"\n",
|
067f64c7 |
"(\n",
" learner2.plot(n).relabel(\"Homogeneous grid\")\n",
" + learner.plot().relabel(\"With adaptive\")\n",
" + learner2.plot(n, tri_alpha=0.4)\n",
" + learner.plot(tri_alpha=0.4)\n",
").cols(2)"
|
569168f5 |
]
},
|
31b99e9d |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
569168f5 |
"# Averaging learner"
|
31b99e9d |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
569168f5 |
"The next type of learner averages a function until the uncertainty in the average meets some condition.\n",
|
31b99e9d |
"\n",
|
569168f5 |
"This is useful for sampling a random variable. The function passed to the learner must formally take a single parameter,\n",
"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)."
|
31b99e9d |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
569168f5 |
"def g(n):\n",
" import random\n",
" from time import sleep\n",
|
8310cc04 |
" sleep(random.random() / 1000)\n",
|
569168f5 |
" # Properly save and restore the RNG state\n",
" state = random.getstate()\n",
" random.seed(n)\n",
" val = random.gauss(0.5, 1)\n",
" random.setstate(state)\n",
" return val"
|
31b99e9d |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
60589534 |
"learner = adaptive.AverageLearner(g, atol=None, rtol=0.01)\n",
|
8310cc04 |
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 2)\n",
|
5b1cd171 |
"runner.live_info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
8310cc04 |
"runner.live_plot(update_interval=0.1)"
|
31b99e9d |
]
},
|
0cc979de |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1D integration learner with `cquad`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This learner learns a 1D function and calculates the integral and error of the integral with it. It is based on Pedro Gonnet's [implementation](https://www.academia.edu/1976055/Adaptive_quadrature_re-revisited).\n",
"\n",
"Let's try the following function with cusps (that is difficult to integrate):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f24(x):\n",
" return np.floor(np.exp(x))\n",
"\n",
"xs = np.linspace(0, 3, 200)\n",
"hv.Scatter((xs, [f24(x) for x in xs]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just to prove that this really is a difficult to integrate function, let's try a familiar function integrator `scipy.integrate.quad`, which will give us warnings that it encounters difficulties."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import scipy.integrate\n",
"scipy.integrate.quad(f24, 0, 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We initialize a learner again and pass the bounds and relative tolerance we want to reach. Then in the `Runner` we pass `goal=lambda l: l.done()` where `learner.done()` is `True` when the relative tolerance has been reached."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
60589534 |
"from adaptive.runner import SequentialExecutor\n",
|
8310cc04 |
"\n",
|
18daf469 |
"learner = adaptive.IntegratorLearner(f24, bounds=(0, 3), tol=1e-8)\n",
|
8310cc04 |
"\n",
|
067f64c7 |
"# We use a SequentialExecutor, which runs the function to be learned in *this* process only.\n",
"# This means we don't pay the overhead of evaluating the function in another process.\n",
"executor = SequentialExecutor()\n",
"runner = adaptive.Runner(learner, executor=executor, goal=lambda l: l.done())\n",
|
5b1cd171 |
"runner.live_info()"
|
0cc979de |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we could do the live plotting again, but lets just wait untill the runner is done."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
067f64c7 |
"if runner.status() != \"finished\":\n",
" print(\"WARINING: The runner hasn't reached it goal yet!\")\n",
"\n",
|
0cc979de |
"print('The integral value is {} with the corresponding error of {}'.format(learner.igral, learner.err))\n",
"learner.plot()"
]
},
|
71f79367 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1D learner with vector output: `f:ℝ → ℝ^N`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
8310cc04 |
"Sometimes you may want to learn a function with vector output:"
|
71f79367 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"random.seed(0)\n",
"offsets = [random.uniform(-0.8, 0.8) for _ in range(3)]\n",
|
8310cc04 |
"\n",
"# sharp peaks at random locations in the domain\n",
|
71f79367 |
"def f_levels(x, offsets=offsets):\n",
" a = 0.01\n",
|
067f64c7 |
" return np.array([offset + peak(x, offset, wait=False) for offset in offsets])"
|
71f79367 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
8310cc04 |
"`adaptive` has you covered! The `Learner1D` can be used for such functions:"
|
71f79367 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
e2c879af |
"learner = adaptive.Learner1D(f_levels, bounds=(-1, 1))\n",
|
5b8f6015 |
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.01)\n",
"runner.live_info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"runner.live_plot(update_interval=0.1)"
|
71f79367 |
]
},
|
6896de47 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# N-dimensional function learner (beta)\n",
"Besides 1 and 2 dimensional functions, we can also learn N-D functions: $\\ f: ℝ^N → ℝ, N \\ge 2$\n",
"\n",
"Do keep in mind the speed and [effectiveness](https://en.wikipedia.org/wiki/Curse_of_dimensionality) of the learner drops quickly with increasing number of dimensions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# this step takes a lot of time, it will finish at about 3300 points, which can take up to 6 minutes\n",
"def sphere(xyz):\n",
" x, y, z = xyz\n",
" a = 0.4\n",
" return x + z**2 + np.exp(-(x**2 + y**2 + z**2 - 0.75**2)**2/a**4)\n",
"\n",
"learner = adaptive.LearnerND(sphere, bounds=[(-1, 1), (-1, 1), (-1, 1)])\n",
|
067f64c7 |
"runner = adaptive.Runner(learner, goal=lambda l: l.npoints > 2000)\n",
|
6896de47 |
"runner.live_info()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's plot 2D slices of the 3D function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def plot_cut(x, direction, learner=learner):\n",
" cut_mapping = {'xyz'.index(direction): x}\n",
" return learner.plot_slice(cut_mapping, n=100)\n",
"\n",
"dm = hv.DynamicMap(plot_cut, kdims=['value', 'direction'])\n",
"dm.redim.values(value=np.linspace(-1, 1), direction=list('xyz'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or we can plot 1D slices"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%opts Path {+framewise}\n",
"def plot_cut(x1, x2, directions, learner=learner):\n",
" cut_mapping = {'xyz'.index(d): x for d, x in zip(directions, [x1, x2])}\n",
" return learner.plot_slice(cut_mapping)\n",
"\n",
"dm = hv.DynamicMap(plot_cut, kdims=['v1', 'v2', 'directions'])\n",
"dm.redim.values(v1=np.linspace(-1, 1),\n",
" v2=np.linspace(-1, 1),\n",
" directions=['xy', 'xz', 'yz'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plots show some wobbles while the original function was smooth, this is a result of the fact that the learner chooses points in 3 dimensions and the simplices are not in the same face as we try to interpolate our lines. However, as always, when you sample more points the graph will become gradually smoother."
]
},
|
067f64c7 |
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"learner.plot_3D()"
]
},
|
0a412ad8 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
6298d464 |
"# Custom adaptive logic for 1D and 2D"
|
0a412ad8 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
6298d464 |
"`Learner1D` and `Learner2D` both work on the principle of subdividing their domain into subdomains, and assigning a property to each subdomain, which we call the *loss*. The algorithm for choosing the best place to evaluate our function is then simply *take the subdomain with the largest loss and add a point in the center, creating new subdomains around this point*. \n",
|
0a412ad8 |
"\n",
|
6298d464 |
"The *loss function* that defines the loss per subdomain is the canonical place to define what regions of the domain are \"interesting\".\n",
"The default loss function for `Learner1D` and `Learner2D` is sufficient for a wide range of common cases, but it is by no means a panacea. For example, the default loss function will tend to get stuck on divergences.\n",
|
0a412ad8 |
"\n",
|
6298d464 |
"Both the `Learner1D` and `Learner2D` allow you to specify a *custom loss function*. Below we illustrate how you would go about writing your own loss function. The documentation for `Learner1D` and `Learner2D` specifies the signature that your loss function needs to have in order for it to work with `adaptive`.\n",
|
0a412ad8 |
"\n",
"\n",
|
6298d464 |
"Say we want to properly sample a function that contains divergences. A simple (but naive) strategy is to *uniformly* sample the domain:\n"
|
0a412ad8 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
067f64c7 |
"def uniform_sampling_1d(xs, ys):\n",
|
49d67d7f |
" # Note that we never use 'data'; the loss is just the size of the subdomain\n",
|
067f64c7 |
" dx = xs[1] - xs[0]\n",
|
0a412ad8 |
" return dx\n",
"\n",
"def f_divergent_1d(x):\n",
" return 1 / x**2\n",
"\n",
"learner = adaptive.Learner1D(f_divergent_1d, (-1, 1), loss_per_interval=uniform_sampling_1d)\n",
"runner = adaptive.BlockingRunner(learner, goal=lambda l: l.loss() < 0.01)\n",
"learner.plot().select(y=(0, 10000))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
6298d464 |
"%%opts EdgePaths (color='w') Image [logz=True]\n",
"\n",
"from adaptive.runner import SequentialExecutor\n",
|
0a412ad8 |
"\n",
"def uniform_sampling_2d(ip):\n",
" from adaptive.learner.learner2D import areas\n",
" A = areas(ip)\n",
" return np.sqrt(A)\n",
"\n",
"def f_divergent_2d(xy):\n",
" x, y = xy\n",
" return 1 / (x**2 + y**2)\n",
"\n",
"learner = adaptive.Learner2D(f_divergent_2d, [(-1, 1), (-1, 1)], loss_per_triangle=uniform_sampling_2d)\n",
|
6298d464 |
"\n",
"# this takes a while, so use the async Runner so we know *something* is happening\n",
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.02)\n",
"runner.live_info()\n",
"runner.live_plot(update_interval=0.2,\n",
" plotter=lambda l: l.plot(tri_alpha=0.3).relabel('1 / (x^2 + y^2) in log scale'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The uniform sampling strategy is a common case to benchmark against, so the 1D and 2D versions are included in `adaptive` as `adaptive.learner.learner1D.uniform_sampling` and `adaptive.learner.learner2D.uniform_sampling`."
|
0a412ad8 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
6298d464 |
"### Doing better\n",
|
56c07f5a |
"\n",
|
6298d464 |
"Of course, using `adaptive` for uniform sampling is a bit of a waste!\n",
"\n",
"Let's see if we can do a bit better. Below we define a loss per subdomain that scales with the degree of nonlinearity of the function (this is very similar to the default loss function for `Learner2D`), but which is 0 for subdomains smaller than a certain area, and infinite for subdomains larger than a certain area.\n",
"\n",
"A loss defined in this way means that the adaptive algorithm will first prioritise subdomains that are too large (infinite loss). After all subdomains are appropriately small it will prioritise places where the function is very nonlinear, but will ignore subdomains that are too small (0 loss)."
|
0a412ad8 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%opts EdgePaths (color='w') Image [logz=True]\n",
"\n",
|
56c07f5a |
"def resolution_loss(ip, min_distance=0, max_distance=1):\n",
" \"\"\"min_distance and max_distance should be in between 0 and 1\n",
" because the total area is normalized to 1.\"\"\"\n",
|
6298d464 |
"\n",
|
0a412ad8 |
" from adaptive.learner.learner2D import areas, deviations\n",
|
6298d464 |
"\n",
|
0a412ad8 |
" A = areas(ip)\n",
"\n",
|
6298d464 |
" # 'deviations' returns an array of shape '(n, len(ip))', where\n",
" # 'n' is the is the dimension of the output of the learned function\n",
" # In this case we know that the learned function returns a scalar,\n",
" # so 'deviations' returns an array of shape '(1, len(ip))'.\n",
" # It represents the deviation of the function value from a linear estimate\n",
" # over each triangular subdomain.\n",
|
0a412ad8 |
" dev = deviations(ip)[0]\n",
" \n",
" # we add terms of the same dimension: dev == [distance], A == [distance**2]\n",
" loss = np.sqrt(A) * dev + A\n",
" \n",
" # Setting areas with a small area to zero such that they won't be chosen again\n",
|
56c07f5a |
" loss[A < min_distance**2] = 0 \n",
" \n",
" # Setting triangles that have a size larger than max_distance to infinite loss\n",
" loss[A > max_distance**2] = np.inf\n",
"\n",
|
0a412ad8 |
" return loss\n",
"\n",
|
56c07f5a |
"loss = partial(resolution_loss, min_distance=0.01)\n",
"\n",
"learner = adaptive.Learner2D(f_divergent_2d, [(-1, 1), (-1, 1)], loss_per_triangle=loss)\n",
|
0a412ad8 |
"runner = adaptive.BlockingRunner(learner, goal=lambda l: l.loss() < 0.02)\n",
|
56c07f5a |
"learner.plot(tri_alpha=0.3).relabel('1 / (x^2 + y^2) in log scale')"
|
0a412ad8 |
]
},
|
6298d464 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Awesome! We zoom in on the singularity, but not at the expense of sampling the rest of the domain a reasonable amount.\n",
"\n",
"The above strategy is available as `adaptive.learner.learner2D.resolution_loss`."
]
},
|
42dac9a1 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
569168f5 |
"# Balancing learner"
|
42dac9a1 |
]
},
{
|
569168f5 |
"cell_type": "markdown",
|
42dac9a1 |
"metadata": {},
"source": [
|
8310cc04 |
"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.\n",
|
42dac9a1 |
"\n",
|
569168f5 |
"The balancing learner can for example be used to implement a poor-man's 2D learner by using the `Learner1D`."
|
42dac9a1 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
7ab8518d |
"def h(x, offset=0):\n",
|
60589534 |
" a = 0.01\n",
" return x + a**2 / (a**2 + (x - offset)**2)\n",
"\n",
|
7ab8518d |
"learners = [adaptive.Learner1D(partial(h, offset=random.uniform(-1, 1)),\n",
|
60589534 |
" bounds=(-1, 1)) for i in range(10)]\n",
|
569168f5 |
"\n",
|
60589534 |
"bal_learner = adaptive.BalancingLearner(learners)\n",
|
1e1788e0 |
"runner = adaptive.Runner(bal_learner, goal=lambda l: l.loss() < 0.01)\n",
"runner.live_info()"
|
42dac9a1 |
]
},
|
b2260aef |
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
60589534 |
"plotter = lambda learner: hv.Overlay([L.plot() for L in learner.learners])\n",
|
8310cc04 |
"runner.live_plot(plotter=plotter, update_interval=0.1)"
|
b2260aef |
]
},
|
da311af9 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
312a1c21 |
"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 `from_product`. See how it works below"
|
da311af9 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from scipy.special import eval_jacobi\n",
"\n",
|
067f64c7 |
"def jacobi(x, n, alpha, beta):\n",
" return eval_jacobi(n, alpha, beta, x)\n",
|
da311af9 |
"\n",
"combos = {\n",
" 'n': [1, 2, 4, 8],\n",
" 'alpha': np.linspace(0, 2, 3),\n",
" 'beta': np.linspace(0, 1, 5),\n",
"}\n",
"\n",
|
312a1c21 |
"learner = adaptive.BalancingLearner.from_product(\n",
|
da311af9 |
" jacobi, adaptive.Learner1D, dict(bounds=(0, 1)), combos)\n",
"\n",
"runner = adaptive.BlockingRunner(learner, goal=lambda l: l.loss() < 0.01)\n",
"\n",
|
35aae864 |
"# The `cdims` will automatically be set when using `from_product`, so\n",
"# `plot()` will return a HoloMap with correctly labeled sliders.\n",
|
497c5764 |
"learner.plot().overlay('beta').grid().select(y=(-1, 3))"
|
da311af9 |
]
},
|
642d7f59 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# DataSaver"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
8310cc04 |
"If the function that you want to learn returns a value along with some metadata, you can wrap your learner in an `adaptive.DataSaver`.\n",
|
642d7f59 |
"\n",
|
8310cc04 |
"In the following example the function to be learned returns its result and the execution time in a dictionary:"
|
642d7f59 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from operator import itemgetter\n",
"\n",
|
60589534 |
"def f_dict(x):\n",
|
642d7f59 |
" \"\"\"The function evaluation takes roughly the time we `sleep`.\"\"\"\n",
" import random\n",
" from time import sleep\n",
"\n",
" waiting_time = random.random()\n",
" sleep(waiting_time)\n",
" a = 0.01\n",
" y = x + a**2 / (a**2 + x**2)\n",
" return {'y': y, 'waiting_time': waiting_time}\n",
"\n",
|
8310cc04 |
"# Create the learner with the function that returns a 'dict'\n",
"# This learner cannot be run directly, as Learner1D does not know what to do with the 'dict'\n",
|
60589534 |
"_learner = adaptive.Learner1D(f_dict, bounds=(-1, 1))\n",
|
642d7f59 |
"\n",
|
8310cc04 |
"# Wrapping the learner with 'adaptive.DataSaver' and tell it which key it needs to learn\n",
|
60589534 |
"learner = adaptive.DataSaver(_learner, arg_picker=itemgetter('y'))"
|
642d7f59 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`learner.learner` is the original learner, so `learner.learner.loss()` will call the correct loss method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
8310cc04 |
"runner = adaptive.Runner(learner, goal=lambda l: l.learner.loss() < 0.05)\n",
"runner.live_info()"
|
642d7f59 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
8310cc04 |
"runner.live_plot(plotter=lambda l: l.learner.plot(), update_interval=0.1)"
|
642d7f59 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
e34c88a7 |
"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."
|
642d7f59 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
e34c88a7 |
"learner.extra_data"
|
642d7f59 |
]
},
|
2edfd59f |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# `Scikit-Optimize`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have wrapped the `Optimizer` class from [`scikit-optimize`](https://github.com/scikit-optimize/scikit-optimize), to show how existing libraries can be integrated with `adaptive`.\n",
"\n",
"The `SKOptLearner` attempts to \"optimize\" the given function `g` (i.e. find the global minimum of `g` in the window of interest).\n",
"\n",
"Here we use the same example as in the `scikit-optimize` [tutorial](https://github.com/scikit-optimize/scikit-optimize/blob/master/examples/ask-and-tell.ipynb). Although `SKOptLearner` can optimize functions of arbitrary dimensionality, we can only plot the learner if a 1D function is being learned."
]
},
{
"cell_type": "code",
"execution_count": null,
|
0e491fd5 |
"metadata": {},
|
2edfd59f |
"outputs": [],
"source": [
|
497c5764 |
"def F(x, noise_level=0.1):\n",
|
2edfd59f |
" return (np.sin(5 * x) * (1 - np.tanh(x ** 2))\n",
" + np.random.randn() * noise_level)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
497c5764 |
"learner = adaptive.SKOptLearner(F, dimensions=[(-2., 2.)],\n",
|
e8c4d7eb |
" base_estimator=\"GP\",\n",
" acq_func=\"gp_hedge\",\n",
" acq_optimizer=\"lbfgs\",\n",
" )\n",
"runner = adaptive.Runner(learner, ntasks=1, goal=lambda l: l.npoints > 40)\n",
|
2edfd59f |
"runner.live_info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
2f64f6ad |
"%%opts Overlay [legend_position='top']\n",
"xs = np.linspace(*learner.space.bounds[0])\n",
|
497c5764 |
"to_learn = hv.Curve((xs, [F(x, 0) for x in xs]), label='to learn')\n",
|
2f64f6ad |
"\n",
"runner.live_plot().relabel('prediction', depth=2) * to_learn"
|
2edfd59f |
]
},
|
9e091088 |
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
|
8310cc04 |
"# Using multiple cores"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Often you will want to evaluate the function on some remote computing resources. `adaptive` works out of the box with any framework that implements a [PEP 3148](https://www.python.org/dev/peps/pep-3148/) compliant executor that returns `concurrent.futures.Future` objects."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
8310cc04 |
"### [`concurrent.futures`](https://docs.python.org/3/library/concurrent.futures.html)"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
257a4367 |
"On Unix-like systems by default `adaptive.Runner` creates a `ProcessPoolExecutor`, but you can also pass one explicitly e.g. to limit the number of workers:"
|
9e091088 |
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
"from concurrent.futures import ProcessPoolExecutor\n",
"\n",
"executor = ProcessPoolExecutor(max_workers=4)\n",
"\n",
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
|
1e1788e0 |
"runner = adaptive.Runner(learner, executor=executor, goal=lambda l: l.loss() < 0.05)\n",
"runner.live_info()\n",
|
8310cc04 |
"runner.live_plot(update_interval=0.1)"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
8310cc04 |
"### [`ipyparallel`](https://ipyparallel.readthedocs.io/en/latest/intro.html)"
|
de7ba87d |
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
de7ba87d |
"outputs": [],
"source": [
|
18212a81 |
"import ipyparallel\n",
"\n",
|
ebbb944e |
"client = ipyparallel.Client() # You will need to start an `ipcluster` to make this work\n",
|
18212a81 |
"\n",
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
|
1e1788e0 |
"runner = adaptive.Runner(learner, executor=client, goal=lambda l: l.loss() < 0.01)\n",
"runner.live_info()\n",
|
5b1cd171 |
"runner.live_plot()"
|
e3a7009c |
]
|
ea41ee7a |
},
|
a1cf4583 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
257a4367 |
"### [`distributed`](https://distributed.readthedocs.io/en/latest/)\n",
"\n",
"On Windows by default `adaptive.Runner` uses a `distributed.Client`."
|
a1cf4583 |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import distributed\n",
"\n",
"client = distributed.Client()\n",
"\n",
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
|
a1cf4583 |
"runner = adaptive.Runner(learner, executor=client, goal=lambda l: l.loss() < 0.01)\n",
"runner.live_info()\n",
|
8310cc04 |
"runner.live_plot(update_interval=0.1)"
|
a1cf4583 |
]
},
|
ea41ee7a |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
9e091088 |
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced Topics"
]
},
|
a17c9212 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Saving and loading learners"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every learner has a `save` and `load` method that can be used to save and load **only** the data of a learner.\n",
"\n",
"There are __two ways__ of naming the files:\n",
"1. Using the `fname` argument in `learner.save(fname=...)`\n",
"2. Setting the `fname` attribute, like `learner.fname = 'data/example.p` and then `learner.save()`\n",
"\n",
"The second way _must be used_ when saving the `learner`s of a `BalancingLearner`.\n",
"\n",
"By default the resulting pickle files are compressed, to turn this off use `learner.save(fname=..., compress=False)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's create two learners and run only one.\n",
|
067f64c7 |
"learner = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
"control = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
|
a17c9212 |
"\n",
"# Let's only run the learner\n",
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.01)\n",
"runner.live_info()"
]
},
|
067f64c7 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can save the data with"
]
},
|
a17c9212 |
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fname = 'data/example_file.p'\n",
|
067f64c7 |
"learner.save(fname)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and load (and then plot) the other empty learner with"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
a17c9212 |
"control.load(fname)\n",
"learner.plot().relabel('saved learner') + control.plot().relabel('loaded learner')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or just (without saving):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
067f64c7 |
"control = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
|
a17c9212 |
"control.copy_from(learner)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One can also periodically save the learner while it's run in a `Runner`. You can use it like:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def slow_f(x):\n",
" from time import sleep\n",
" sleep(5)\n",
" return x\n",
|
067f64c7 |
"\n",
|
a17c9212 |
"learner = adaptive.Learner1D(slow_f, bounds=[0, 1])\n",
"runner = adaptive.Runner(learner, goal=lambda l: l.npoints > 100)\n",
|
067f64c7 |
"\n",
|
a17c9212 |
"runner.start_periodic_saving(save_kwargs=dict(fname='data/periodic_example.p'), interval=6)\n",
|
067f64c7 |
"\n",
|
a17c9212 |
"runner.live_info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# See the data after 6 seconds with\n",
"!ls -lah data # only works on macOS and Linux systems"
]
},
|
8310cc04 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A watched pot never boils!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`adaptive.Runner` does its work in an `asyncio` task that runs concurrently with the IPython kernel, when using `adaptive` from a Jupyter notebook. This is advantageous because it allows us to do things like live-updating plots, however it can trip you up if you're not careful.\n",
"\n",
"Notably: **if you block the IPython kernel, the runner will not do any work**.\n",
"\n",
"For example if you wanted to wait for a runner to complete, **do not wait in a busy loop**:\n",
"```python\n",
"while not runner.task.done():\n",
" pass\n",
"```\n",
"\n",
"If you do this then **the runner will never finish**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What to do if you don't care about live plotting, and just want to run something until its done?\n",
"\n",
"The simplest way to accomplish this is to use `adaptive.BlockingRunner`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
067f64c7 |
"learner = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
|
8310cc04 |
"adaptive.BlockingRunner(learner, goal=lambda l: l.loss() < 0.005)\n",
"# This will only get run after the runner has finished\n",
"learner.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reproducibility"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default `adaptive` runners evaluate the learned function in parallel across several cores. The runners are also opportunistic, in that as soon as a result is available they will feed it to the learner and request another point to replace the one that just finished.\n",
"\n",
"Because the order in which computations complete is non-deterministic, this means that the runner behaves in a non-deterministic way. Adaptive makes this choice because in many cases the speedup from parallel execution is worth sacrificing the \"purity\" of exactly reproducible computations.\n",
"\n",
"Nevertheless it is still possible to run a learner in a deterministic way with adaptive.\n",
"\n",
"The simplest way is to use `adaptive.runner.simple` to run your learner:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
067f64c7 |
"learner = adaptive.Learner1D(partial(peak, wait=False), bounds=(-1, 1))\n",
|
8310cc04 |
"\n",
"# blocks until completion\n",
"adaptive.runner.simple(learner, goal=lambda l: l.loss() < 0.002)\n",
"\n",
"learner.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that unlike `adaptive.Runner`, `adaptive.runner.simple` *blocks* until it is finished.\n",
"\n",
"If you want to enable determinism, want to continue using the non-blocking `adaptive.Runner`, you can use the `adaptive.runner.SequentialExecutor`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from adaptive.runner import SequentialExecutor\n",
"\n",
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
|
8310cc04 |
"\n",
"# blocks until completion\n",
"runner = adaptive.Runner(learner, executor=SequentialExecutor(), goal=lambda l: l.loss() < 0.002)\n",
"runner.live_info()\n",
"runner.live_plot(update_interval=0.1)"
]
},
|
9e091088 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cancelling a runner"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes you want to interactively explore a parameter space, and want the function to be evaluated at finer and finer resolution and manually control when the calculation stops.\n",
"\n",
|
8310cc04 |
"If no `goal` is provided to a runner then the runner will run until cancelled.\n",
"\n",
"`runner.live_info()` will provide a button that can be clicked to stop the runner. You can also stop the runner programatically using `runner.cancel()`."
|
9e091088 |
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
|
9e091088 |
"runner = adaptive.Runner(learner)\n",
|
8310cc04 |
"runner.live_info()\n",
"runner.live_plot(update_interval=0.1)"
|
9e091088 |
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
|
1e1788e0 |
"runner.cancel()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(runner.status())"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Debugging Problems "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Runners work in the background with respect to the IPython kernel, which makes it convenient, but also means that inspecting errors is more difficult because exceptions will not be raised directly in the notebook. Often the only indication you will have that something has gone wrong is that nothing will be happening.\n",
"\n",
"Let's look at the following example, where the function to be learned will raise an exception 10% of the time."
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
"def will_raise(x):\n",
" from random import random\n",
" from time import sleep\n",
" \n",
" sleep(random())\n",
" if random() < 0.1:\n",
" raise RuntimeError('something went wrong!')\n",
" return x**2\n",
" \n",
"learner = adaptive.Learner1D(will_raise, (-1, 1))\n",
"runner = adaptive.Runner(learner) # without 'goal' the runner will run forever unless cancelled\n",
|
8310cc04 |
"runner.live_info()\n",
|
5b1cd171 |
"runner.live_plot()"
|
9e091088 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above runner should continue forever, but we notice that it stops after a few points are evaluated.\n",
"\n",
"First we should check that the runner has really finished:"
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
9e091088 |
"outputs": [],
"source": [
"runner.task.done()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If it has indeed finished then we should check the `result` of the runner. This should be `None` if the runner stopped successfully. If the runner stopped due to an exception then asking for the result will raise the exception with the stack trace:"
|
ea41ee7a |
]
},
{
"cell_type": "code",
"execution_count": null,
|
08a1ae93 |
"metadata": {},
|
ea41ee7a |
"outputs": [],
"source": [
|
9e091088 |
"runner.task.result()"
]
},
|
9ba68ad1 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logging runners"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Runners do their job in the background, which makes introspection quite cumbersome. One way to inspect runners is to instantiate one with `log=True`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.1, log=True)\n",
|
1e1788e0 |
"runner.live_info()"
|
9ba68ad1 |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This gives a the runner a `log` attribute, which is a list of the `learner` methods that were called, as well as their arguments. This is useful because executors typically execute their tasks in a non-deterministic order.\n",
"\n",
"This can be used with `adaptive.runner.replay_log` to perfom the same set of operations on another runner:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
067f64c7 |
"reconstructed_learner = adaptive.Learner1D(peak, bounds=learner.bounds)\n",
|
9ba68ad1 |
"adaptive.runner.replay_log(reconstructed_learner, runner.log)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
|
7ab8518d |
"learner.plot().Scatter.I.opts(style=dict(size=6)) * reconstructed_learner.plot()"
|
9ba68ad1 |
]
},
|
0cd9011f |
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
067f64c7 |
"### Adding coroutines"
|
0cd9011f |
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
067f64c7 |
"In the following example we'll add a `asyncio.Task` that times the runner.\n",
"This is *only* for demonstration purposes because one can simply\n",
"check ``runner.elapsed_time()`` or use the ``runner.live_info()``\n",
"widget to see the time since the runner has started.\n",
"\n",
"So let's get on with the example. To time the runner\n",
"you **cannot** simply use\n",
"\n",
|
0cd9011f |
"```python\n",
"now = datetime.now()\n",
"runner = adaptive.Runner(...)\n",
"print(datetime.now() - now)\n",
"```\n",
"\n",
|
067f64c7 |
"because this will be done immediately. Also blocking the kernel with\n",
"``while not runner.task.done()`` will not work because the runner will\n",
"not do anything when the kernel is blocked.\n",
"\n",
"Therefore you need to create an ``async`` function and hook it into the\n",
"``ioloop`` like so:\n"
|
0cd9011f |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"\n",
"async def time(runner):\n",
" from datetime import datetime\n",
" now = datetime.now()\n",
" await runner.task\n",
" return datetime.now() - now\n",
"\n",
"ioloop = asyncio.get_event_loop()\n",
"\n",
|
067f64c7 |
"learner = adaptive.Learner1D(peak, bounds=(-1, 1))\n",
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.1)\n",
|
0cd9011f |
"\n",
|
067f64c7 |
"timer = ioloop.create_task(time(runner))\n",
"runner.live_info()"
|
0cd9011f |
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The result will only be set when the runner is done.\n",
"timer.result()"
]
},
|
9e091088 |
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using Runners from a script "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
|
8310cc04 |
"Runners can also be used from a Python script independently of the notebook.\n",
"\n",
"The simplest way to accomplish this is simply to use the `BlockingRunner`:\n",
|
9e091088 |
"\n",
"```python\n",
"import adaptive\n",
"\n",
"def f(x):\n",
" return x\n",
"\n",
|
067f64c7 |
"learner = adaptive.Learner1D(peak, (-1, 1))\n",
|
9e091088 |
"\n",
|
8310cc04 |
"adaptive.BlockingRunner(learner, goal=lambda: l: l.loss() < 0.1)\n",
|
9e091088 |
"```\n",
"\n",
|
8310cc04 |
"If you use `asyncio` already in your script and want to integrate `adaptive` into it, then you can use the default `Runner` as you would from a notebook. If you want to wait for the runner to finish, then you can simply\n",
|
9e091088 |
"```python\n",
" await runner.task\n",
"```\n",
|
8310cc04 |
"from within a coroutine."
|
ea41ee7a |
]
|
de7ba87d |
}
],
"metadata": {
"language_info": {
"name": "python",
|
98d82968 |
"pygments_lexer": "ipython3"
|
de7ba87d |
}
},
"nbformat": 4,
"nbformat_minor": 1
}
|