Tutorial LearnerND#

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 holoviews as hv
import numpy as np


def dynamicmap_to_holomap(dm):
    # XXX: change when https://github.com/ioam/holoviews/issues/3085
    # is fixed.
    vals = {d.name: d.values for d in dm.dimensions() if d.values}
    return hv.HoloMap(dm.select(**vals))

Besides 1 and 2 dimensional functions, we can also learn N-D functions: $f: โ„^N โ†’ โ„^M, N \ge 2, M \ge 1$.

Do keep in mind the speed and effectiveness of the learner drops quickly with increasing number of dimensions.

def sphere(xyz):
    x, y, z = xyz
    a = 0.4
    return x + z**2 + np.exp(-((x**2 + y**2 + z**2 - 0.75**2) ** 2) / a**4)


learner = adaptive.LearnerND(sphere, bounds=[(-1, 1), (-1, 1), (-1, 1)])
runner = adaptive.Runner(learner, loss_goal=1e-3)
Hide code cell content
await runner.task  # This is not needed in a notebook environment!
runner.live_info()

Letโ€™s plot 2D slices of the 3D function

def plot_cut(x, direction, learner=learner):
    cut_mapping = {"XYZ".index(direction): x}
    return learner.plot_slice(cut_mapping, n=100)


dm = hv.DynamicMap(plot_cut, kdims=["val", "direction"])
dm = dm.redim.values(val=np.linspace(-1, 1, 11), direction=list("XYZ"))

# In a notebook one would run `dm` however we want a statically generated
# html, so we use a HoloMap to display it here
dynamicmap_to_holomap(dm)