adaptive.BalancingLearner¶
BalancingLearner
¶
BalancingLearner(learners: list[BaseLearner], *, cdims: CDIMS_TYPE = None, strategy: STRATEGY_TYPE = 'loss_improvements')
Bases: BaseLearner
Choose the optimal points from a set of learners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learners
|
sequence of adaptive.BaseLearner\s
|
The learners from which to choose. These must all have the same type. |
required |
cdims
|
sequence of dicts, or (keys, iterable of values)
|
Constant dimensions; the parameters that label the learners. Used
in
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
learners |
list
|
The sequence of |
function |
callable
|
A function that calls the functions of the underlying learners.
Its signature is |
strategy |
'loss_improvements' (default), 'loss', 'npoints', or 'cycle'.
|
The points that the |
Notes
This learner compares the loss calculated from the "child" learners.
This requires that the 'loss' from different learners can be meaningfully
compared. For the moment we enforce this restriction by requiring that
all learners are the same type but (depending on the internals of the
learner) it may be that the loss cannot be compared even between learners
of the same type. In this case the BalancingLearner will
behave in an undefined way. Change the strategy in that case.
Source code in adaptive/learner/balancing_learner.py
strategy
property
writable
¶
Can be either 'loss_improvements' (default), 'loss', 'npoints', or
'cycle'. The points that the BalancingLearner choses can be either
based on: the best 'loss_improvements', the smallest total 'loss' of
the child learners, the number of points per learner, using 'npoints',
or by going through all learners one by one using 'cycle'.
One can dynamically change the strategy while the simulation is
running by changing the learner.strategy attribute.
new
¶
new() -> BalancingLearner
Create a new BalancingLearner with the same parameters.
Source code in adaptive/learner/balancing_learner.py
ask
¶
Chose points for learners.
Source code in adaptive/learner/balancing_learner.py
tell
¶
tell_pending
¶
loss
¶
plot
¶
plot(cdims: CDIMS_TYPE = None, plotter: Callable[[BaseLearner], Any] | None = None, dynamic: bool = True)
Returns a DynamicMap with sliders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cdims
|
sequence of dicts, or (keys, iterable of values)
|
Constant dimensions; the parameters that label the learners. Example inputs that all give identical results:
|
None
|
plotter
|
callable
|
A function that takes the learner as a argument and returns a
holoviews object. By default |
None
|
dynamic
|
bool
|
Return a |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dm |
DynamicMap(default) or HoloMap
|
A |
Source code in adaptive/learner/balancing_learner.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
remove_unfinished
¶
from_product
classmethod
¶
from_product(f, learner_type: LearnerType, learner_kwargs: dict[str, Any], combos: dict[str, Sequence[Any]]) -> BalancingLearner
Create a BalancingLearner with learners of all combinations of
named variables’ values. The cdims will be set correctly, so calling
learner.plot will be a holoviews.core.HoloMap with the correct labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
callable
|
Function to learn, must take arguments provided in in |
required |
learner_type
|
BaseLearner
|
The learner that should wrap the function. For example |
required |
learner_kwargs
|
dict
|
Keyword argument for the |
required |
combos
|
dict (mapping individual fn arguments -> sequence of values)
|
For all combinations of each argument a learner will be instantiated. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
learner |
BalancingLearner
|
A |
Example
def f(x, n, alpha, beta): ... return scipy.special.eval_jacobi(n, alpha, beta, x)
combos = { ... 'n': [1, 2, 4, 8, 16], ... 'alpha': np.linspace(0, 2, 3), ... 'beta': np.linspace(0, 1, 5), ... }
learner = BalancingLearner.from_product( ... f, Learner1D, dict(bounds=(0, 1)), combos)
Notes
The order of the child learners inside learner.learners is the same
as adaptive.utils.named_product(**combos).
Source code in adaptive/learner/balancing_learner.py
to_dataframe
¶
to_dataframe(index_name: str = 'learner_index', **kwargs)
Return the data as a concatenated pandas.DataFrame from child learners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_name
|
str
|
The name of the index column indicating the learner index, by default "learner_index". |
'learner_index'
|
**kwargs
|
dict
|
Keyword arguments passed to each |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in adaptive/learner/balancing_learner.py
load_dataframe
¶
Load the data from a pandas.DataFrame into the child learners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame with the data to load. |
required |
index_name
|
str
|
The |
'learner_index'
|
**kwargs
|
dict
|
Keyword arguments passed to each |
{}
|
Source code in adaptive/learner/balancing_learner.py
save
¶
Save the data of the child learners into pickle files in a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
Callable[[BaseLearner], str] | Sequence[str]
|
Given a learner, returns a filename into which to save the data. Or a list (or iterable) with filenames. |
required |
compress
|
bool
|
Compress the data upon saving using |
True
|
Example
def combo_fname(learner): ... val = learner.function.keywords # because functools.partial ... fname = '__'.join([f'{k}_{v}.pickle' for k, v in val.items()]) ... return 'data_folder/' + fname
def f(x, a, b): return a * x**2 + b
learners = [Learner1D(functools.partial(f, **combo), (-1, 1)) ... for combo in adaptive.utils.named_product(a=[1, 2], b=[1])]
learner = BalancingLearner(learners)
Run the learner¶
runner = adaptive.Runner(learner)
Then save¶
learner.save(combo_fname) # use 'load' in the same way
Source code in adaptive/learner/balancing_learner.py
load
¶
Load the data of the child learners from pickle files in a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
Callable[[BaseLearner], str] | Sequence[str]
|
Given a learner, returns a filename from which to load the data. Or a list (or iterable) with filenames. |
required |
compress
|
bool
|
If the data is compressed when saved, one must load it with compression too. |
True
|
Example
See the example in the BalancingLearner.save doc-string.