Tutorial Learner1D#

Note

Because this documentation consists of static html, the live_plot and live_info widget is not live. Download the notebook in order to see the real behaviour. [1]

Hide code cell content
import adaptive

adaptive.notebook_extension()

import random
from functools import partial

import numpy as np

scalar output: f:โ„ โ†’ โ„#

We start with the most common use-case: sampling a 1D function f: โ„ โ†’ โ„.

We will use the following function, which is a smooth (linear) background with a sharp peak at a random location:

offset = random.uniform(-0.5, 0.5)


def f(x, offset=offset, wait=True):
    from random import random
    from time import sleep

    a = 0.01
    if wait:
        sleep(random() / 10)
    return x + a**2 / (a**2 + (x - offset) ** 2)

We start by initializing a 1D โ€œlearnerโ€, which will suggest points to evaluate, and adapt its suggestions as more and more points are evaluated.

learner = adaptive.Learner1D(f, bounds=(-1, 1))

Next we create a โ€œrunnerโ€ that will request points from the learner and evaluate โ€˜fโ€™ on them.

By default on Unix-like systems the runner will evaluate the points in parallel using local processes concurrent.futures.ProcessPoolExecutor.

On Windows systems the runner will use a loky.get_reusable_executor. A ProcessPoolExecutor cannot be used on Windows for reasons.

# The end condition is when the "loss" is less than 0.01. In the context of the
# 1D learner this means that we will resolve features in 'func' with width 0.01 or wider.
runner = adaptive.Runner(learner, loss_goal=0.01)
Hide code cell content
await runner.task  # This is not needed in a notebook environment!

When instantiated in a Jupyter notebook the runner does its job in the background and does not block the IPython kernel. We can use this to create a plot that updates as new data arrives:

runner.live_info()
runner.live_plot(update_interval=0.1)

We can now compare the adaptive sampling to a homogeneous sampling with the same number of points:

if not runner.task.done():
    raise RuntimeError(
        "Wait for the runner to finish before executing the cells below!"
    )
learner2 = adaptive.Learner1D(f, bounds=learner.bounds)

xs = np.linspace(*learner.bounds, len(learner.data))
learner2.tell_many(xs, map(partial(f, wait=False), xs))

learner.plot() + learner2.plot()

vector output: f:โ„ โ†’ โ„^N#

Sometimes you may want to learn a function with vector output:

random.seed(0)
offsets = [random.uniform(-0.8, 0.8) for _ in range(3)]

# sharp peaks at random locations in the domain


def f_levels(x, offsets=offsets):
    a = 0.01
    return np.array(
        [offset + x + a**2 / (a**2 + (x - offset) ** 2) for offset in offsets]
    )

adaptive has you covered! The Learner1D can be used for such functions:

learner = adaptive.Learner1D(f_levels, bounds=(-1, 1))
runner = adaptive.Runner(
    learner, loss_goal=0.01
)  # continue until `learner.loss()<=0.01`
Hide code cell content
await runner.task  # This is not needed in a notebook environment!
runner.live_info()
runner.live_plot(update_interval=0.1)

Looking at curvature#

By default adaptive will sample more points where the (normalized) euclidean distance between the neighboring points is large. You may achieve better results sampling more points in regions with high curvature. To do this, you need to tell the learner to look at the curvature by specifying loss_per_interval.

from adaptive.learner.learner1D import (
    curvature_loss_function,
    default_loss,
    uniform_loss,
)

curvature_loss = curvature_loss_function()
learner = adaptive.Learner1D(f, bounds=(-1, 1), loss_per_interval=curvature_loss)
runner = adaptive.Runner(learner, loss_goal=0.01)
Hide code cell content
await runner.task  # This is not needed in a notebook environment!
runner.live_info()
runner.live_plot(update_interval=0.1)

We may see the difference of homogeneous sampling vs only one interval vs including the nearest neighboring intervals in this plot. We will look at 100 points.

def sin_exp(x):
    from math import exp, sin

    return sin(15 * x) * exp(-(x**2) * 2)


learner_h = adaptive.Learner1D(sin_exp, (-1, 1), loss_per_interval=uniform_loss)
learner_1 = adaptive.Learner1D(sin_exp, (-1, 1), loss_per_interval=default_loss)
learner_2 = adaptive.Learner1D(sin_exp, (-1, 1), loss_per_interval=curvature_loss)

# adaptive.runner.simple is a non parallel blocking runner.
adaptive.runner.simple(learner_h, npoints_goal=100)
adaptive.runner.simple(learner_1, npoints_goal=100)
adaptive.runner.simple(learner_2, npoints_goal=100)

(
    learner_h.plot().relabel("homogeneous")
    + learner_1.plot().relabel("euclidean loss")
    + learner_2.plot().relabel("curvature loss")
).cols(2)

More info about using custom loss functions can be found in Custom adaptive logic for 1D and 2D.

Exporting the data#

We can view the raw data by looking at the dictionary learner.data. Alternatively, we can view the data as NumPy array with

learner.to_numpy()
array([[-1.00000000e+00, -9.99707231e-01],
       [-8.75000000e-01, -8.74526300e-01],
       [-7.50000000e-01, -7.49106273e-01],
       [-6.25000000e-01, -6.22723552e-01],
       [-5.62500000e-01, -5.57884343e-01],
       [-5.00000000e-01, -4.86140353e-01],
       [-4.84375000e-01, -4.63642497e-01],
       [-4.68750000e-01, -4.34500833e-01],
       [-4.53125000e-01, -3.86657510e-01],
       [-4.45312500e-01, -3.43267344e-01],
       [-4.37500000e-01, -2.64337443e-01],
       [-4.33593750e-01, -1.96646735e-01],
       [-4.29687500e-01, -9.30960618e-02],
       [-4.25781250e-01,  6.76207898e-02],
       [-4.23828125e-01,  1.75305187e-01],
       [-4.21875000e-01,  2.98739556e-01],
       [-4.17968750e-01,  5.30942040e-01],
       [-4.16015625e-01,  5.82637712e-01],
       [-4.14062500e-01,  5.61403498e-01],
       [-4.10156250e-01,  3.58106477e-01],
       [-4.06250000e-01,  1.24732616e-01],
       [-4.02343750e-01, -4.13510847e-02],
       [-3.98437500e-01, -1.46049514e-01],
       [-3.90625000e-01, -2.52916384e-01],
       [-3.82812500e-01, -2.97937042e-01],
       [-3.75000000e-01, -3.17931922e-01],
       [-3.59375000e-01, -3.28763035e-01],
       [-3.43750000e-01, -3.24772439e-01],
       [-3.12500000e-01, -3.03188661e-01],
       [-2.81250000e-01, -2.75744281e-01],
       [-2.50000000e-01, -2.46368838e-01],
       [-1.25000000e-01, -1.23817638e-01],
       [ 0.00000000e+00,  5.78490789e-04],
       [ 2.50000000e-01,  2.50225638e-01],
       [ 5.00000000e-01,  5.00119259e-01],
       [ 7.50000000e-01,  7.50073592e-01],
       [ 1.00000000e+00,  1.00004990e+00]])

If Pandas is installed (optional dependency), you can also run

df = learner.to_dataframe()
df
x y function.offset function.wait
0 -1.000000 -0.999707 -0.415648 True
1 -0.875000 -0.874526 -0.415648 True
2 -0.750000 -0.749106 -0.415648 True
3 -0.625000 -0.622724 -0.415648 True
4 -0.562500 -0.557884 -0.415648 True
5 -0.500000 -0.486140 -0.415648 True
6 -0.484375 -0.463642 -0.415648 True
7 -0.468750 -0.434501 -0.415648 True
8 -0.453125 -0.386658 -0.415648 True
9 -0.445312 -0.343267 -0.415648 True
10 -0.437500 -0.264337 -0.415648 True
11 -0.433594 -0.196647 -0.415648 True
12 -0.429688 -0.093096 -0.415648 True
13 -0.425781 0.067621 -0.415648 True
14 -0.423828 0.175305 -0.415648 True
15 -0.421875 0.298740 -0.415648 True
16 -0.417969 0.530942 -0.415648 True
17 -0.416016 0.582638 -0.415648 True
18 -0.414062 0.561403 -0.415648 True
19 -0.410156 0.358106 -0.415648 True
20 -0.406250 0.124733 -0.415648 True
21 -0.402344 -0.041351 -0.415648 True
22 -0.398438 -0.146050 -0.415648 True
23 -0.390625 -0.252916 -0.415648 True
24 -0.382812 -0.297937 -0.415648 True
25 -0.375000 -0.317932 -0.415648 True
26 -0.359375 -0.328763 -0.415648 True
27 -0.343750 -0.324772 -0.415648 True
28 -0.312500 -0.303189 -0.415648 True
29 -0.281250 -0.275744 -0.415648 True
30 -0.250000 -0.246369 -0.415648 True
31 -0.125000 -0.123818 -0.415648 True
32 0.000000 0.000578 -0.415648 True
33 0.250000 0.250226 -0.415648 True
34 0.500000 0.500119 -0.415648 True
35 0.750000 0.750074 -0.415648 True
36 1.000000 1.000050 -0.415648 True

and load that data into a new learner with

new_learner = adaptive.Learner1D(learner.function, (-1, 1))  # create an empty learner
new_learner.load_dataframe(df)  # load the pandas.DataFrame's data
new_learner.plot()