Tutorial DataSaver#

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()

If the function that you want to learn returns a value along with some metadata, you can wrap your learner in an adaptive.DataSaver.

In the following example the function to be learned returns its result and the execution time in a dictionary:

from operator import itemgetter


def f_dict(x):
    """The function evaluation takes roughly the time we `sleep`."""
    import random
    from time import sleep

    waiting_time = random.random()
    sleep(waiting_time)
    a = 0.01
    y = x + a**2 / (a**2 + x**2)
    return {"y": y, "waiting_time": waiting_time}


# Create the learner with the function that returns a 'dict'
# This learner cannot be run directly, as Learner1D does not know what to do with the 'dict'
_learner = adaptive.Learner1D(f_dict, bounds=(-1, 1))

# Wrapping the learner with 'adaptive.DataSaver' and tell it which key it needs to learn
learner = adaptive.DataSaver(_learner, arg_picker=itemgetter("y"))

learner.learner is the original learner, so learner.learner.loss() will call the correct loss method.

runner = adaptive.Runner(learner, loss_goal=0.1)
Hide code cell content
await runner.task  # This is not needed in a notebook environment!
runner.live_info()
runner.live_plot(plotter=lambda lrn: lrn.learner.plot(), update_interval=0.1)

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.

learner.extra_data
OrderedDict([(-1.0,
              {'y': -0.9999000099990001, 'waiting_time': 0.5078497781013304}),
             (1.0, {'y': 1.000099990001, 'waiting_time': 0.9257565733649581}),
             (0.0, {'y': 1.0, 'waiting_time': 0.6820240840044735}),
             (-0.5,
              {'y': -0.4996001599360256, 'waiting_time': 0.5549671737183022}),
             (-0.25,
              {'y': -0.24840255591054314,
               'waiting_time': 0.22708428563148964}),
             (-0.75,
              {'y': -0.7498222538215429, 'waiting_time': 0.8706456173200764}),
             (0.5,
              {'y': 0.5003998400639744, 'waiting_time': 0.36323732640898276}),
             (-0.125,
              {'y': -0.11864069952305246, 'waiting_time': 0.8662925753687483}),
             (0.75,
              {'y': 0.7501777461784571, 'waiting_time': 0.18141573607747752}),
             (0.25,
              {'y': 0.2515974440894569, 'waiting_time': 0.16267186833438463}),
             (-0.0625,
              {'y': -0.0375390015600624, 'waiting_time': 0.5459970916499688}),
             (-0.03125,
              {'y': 0.06163824383164006, 'waiting_time': 0.04276929304396715}),
             (0.125,
              {'y': 0.13135930047694755, 'waiting_time': 0.6476169577094237}),
             (0.0625,
              {'y': 0.0874609984399376, 'waiting_time': 0.1860993584627212}),
             (-0.015625,
              {'y': 0.27495388762769585, 'waiting_time': 0.9470602251767782}),
             (0.03125,
              {'y': 0.12413824383164006, 'waiting_time': 0.7280361061981273}),
             (0.015625,
              {'y': 0.30620388762769585, 'waiting_time': 0.56360471917994}),
             (-0.0078125,
              {'y': 0.6131699135839903, 'waiting_time': 0.7915191379884645}),
             (0.0078125,
              {'y': 0.6287949135839903, 'waiting_time': 0.37619099854979854}),
             (-0.00390625,
              {'y': 0.8637065438995976, 'waiting_time': 0.5413210036859435}),
             (0.00390625,
              {'y': 0.8715190438995976, 'waiting_time': 0.5122565076623025}),
             (-0.375,
              {'y': -0.3742893942085628, 'waiting_time': 0.45867468951539925}),
             (-0.625,
              {'y': -0.6247440655192271, 'waiting_time': 0.5209174382542118}),
             (-0.875,
              {'y': -0.8748694048124327, 'waiting_time': 0.7958635226421668}),
             (0.875,
              {'y': 0.8751305951875673, 'waiting_time': 0.4433613618515815}),
             (0.625,
              {'y': 0.6252559344807729, 'waiting_time': 0.20248929707704666}),
             (-0.01171875,
              {'y': 0.40963707758975415, 'waiting_time': 0.07096417457046444}),
             (0.375,
              {'y': 0.3757106057914372, 'waiting_time': 0.5068791701232701}),
             (0.01171875,
              {'y': 0.43307457758975415, 'waiting_time': 0.38033578951519875}),
             (0.005859375,
              {'y': 0.7502821111533918, 'waiting_time': 0.36558085060662493}),
             (-0.005859375,
              {'y': 0.7385633611533918, 'waiting_time': 0.7140643807449938}),
             (-0.0234375,
              {'y': 0.1305706215220334, 'waiting_time': 0.3077895928274582}),
             (0.009765625,
              {'y': 0.5216216654886127, 'waiting_time': 0.3941447702789106}),
             (0.0234375,
              {'y': 0.1774456215220334, 'waiting_time': 0.5103118319211017}),
             (-0.009765625,
              {'y': 0.5020904154886127, 'waiting_time': 0.9840335086463282})])