Tutorial BalancingLearner#

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

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.

The balancing learner can for example be used to implement a poor-manโ€™s 2D learner by using the Learner1D.

def h(x, offset=0):
    a = 0.01
    return x + a**2 / (a**2 + (x - offset) ** 2)


learners = [
    adaptive.Learner1D(partial(h, offset=random.uniform(-1, 1)), bounds=(-1, 1))
    for i in range(10)
]

bal_learner = adaptive.BalancingLearner(learners)
runner = adaptive.Runner(bal_learner, loss_goal=0.01)
Hide code cell content
await runner.task  # This is not needed in a notebook environment!
runner.live_info()
def plotter(learner):
    return hv.Overlay([L.plot() for L in learner.learners])


runner.live_plot(plotter=plotter, update_interval=0.1)

Often one wants to create a set of learners for a cartesian product of parameters. For that particular case weโ€™ve added a classmethod called from_product. See how it works below

from scipy.special import eval_jacobi


def jacobi(x, n, alpha, beta):
    return eval_jacobi(n, alpha, beta, x)


combos = {
    "n": [1, 2, 4, 8],
    "alpha": np.linspace(0, 2, 3),
    "beta": np.linspace(0, 1, 5),
}

learner = adaptive.BalancingLearner.from_product(
    jacobi, adaptive.Learner1D, {"bounds": (0, 1)}, combos
)

runner = adaptive.BlockingRunner(learner, loss_goal=0.01)

# The `cdims` will automatically be set when using `from_product`, so
# `plot()` will return a HoloMap with correctly labeled sliders.
learner.plot().overlay("beta").grid().select(y=(-1, 3))