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.99801904e-01],
       [-7.50000000e-01, -7.49528508e-01],
       [-6.25000000e-01, -6.24111987e-01],
       [-5.00000000e-01, -4.97746698e-01],
       [-4.37500000e-01, -4.32950876e-01],
       [-3.75000000e-01, -3.61482268e-01],
       [-3.59375000e-01, -3.39263391e-01],
       [-3.43750000e-01, -3.10802110e-01],
       [-3.28125000e-01, -2.65082041e-01],
       [-3.20312500e-01, -2.24608423e-01],
       [-3.12500000e-01, -1.52659962e-01],
       [-3.08593750e-01, -9.20360994e-02],
       [-3.04687500e-01, -2.12050136e-04],
       [-3.00781250e-01,  1.42453291e-01],
       [-2.96875000e-01,  3.55388622e-01],
       [-2.92968750e-01,  6.03668023e-01],
       [-2.91015625e-01,  6.88610194e-01],
       [-2.89062500e-01,  7.08333329e-01],
       [-2.85156250e-01,  5.51580577e-01],
       [-2.81250000e-01,  3.09486326e-01],
       [-2.77343750e-01,  1.23351518e-01],
       [-2.73437500e-01,  4.05527779e-03],
       [-2.65625000e-01, -1.17153410e-01],
       [-2.57812500e-01, -1.67621626e-01],
       [-2.50000000e-01, -1.89978154e-01],
       [-2.34375000e-01, -2.02597411e-01],
       [-2.18750000e-01, -1.99203345e-01],
       [-1.87500000e-01, -1.77993388e-01],
       [-1.25000000e-01, -1.21321424e-01],
       [-6.25000000e-02, -6.05643549e-02],
       [ 0.00000000e+00,  1.19114552e-03],
       [ 1.25000000e-01,  1.25581493e-01],
       [ 2.50000000e-01,  2.50343360e-01],
       [ 5.00000000e-01,  5.00160378e-01],
       [ 7.50000000e-01,  7.50092523e-01],
       [ 1.00000000e+00,  1.00006013e+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.999802 -0.289573 True
1 -0.750000 -0.749529 -0.289573 True
2 -0.625000 -0.624112 -0.289573 True
3 -0.500000 -0.497747 -0.289573 True
4 -0.437500 -0.432951 -0.289573 True
5 -0.375000 -0.361482 -0.289573 True
6 -0.359375 -0.339263 -0.289573 True
7 -0.343750 -0.310802 -0.289573 True
8 -0.328125 -0.265082 -0.289573 True
9 -0.320312 -0.224608 -0.289573 True
10 -0.312500 -0.152660 -0.289573 True
11 -0.308594 -0.092036 -0.289573 True
12 -0.304688 -0.000212 -0.289573 True
13 -0.300781 0.142453 -0.289573 True
14 -0.296875 0.355389 -0.289573 True
15 -0.292969 0.603668 -0.289573 True
16 -0.291016 0.688610 -0.289573 True
17 -0.289062 0.708333 -0.289573 True
18 -0.285156 0.551581 -0.289573 True
19 -0.281250 0.309486 -0.289573 True
20 -0.277344 0.123352 -0.289573 True
21 -0.273438 0.004055 -0.289573 True
22 -0.265625 -0.117153 -0.289573 True
23 -0.257812 -0.167622 -0.289573 True
24 -0.250000 -0.189978 -0.289573 True
25 -0.234375 -0.202597 -0.289573 True
26 -0.218750 -0.199203 -0.289573 True
27 -0.187500 -0.177993 -0.289573 True
28 -0.125000 -0.121321 -0.289573 True
29 -0.062500 -0.060564 -0.289573 True
30 0.000000 0.001191 -0.289573 True
31 0.125000 0.125581 -0.289573 True
32 0.250000 0.250343 -0.289573 True
33 0.500000 0.500160 -0.289573 True
34 0.750000 0.750093 -0.289573 True
35 1.000000 1.000060 -0.289573 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()