Runner extras#

Stopping Criteria#

Runners allow you to specify the stopping criterion by providing a goal as a function that takes the learner and returns a boolean: False for β€œcontinue running” and True for β€œstop”. This gives you a lot of flexibility for defining your own stopping conditions, however we also provide some common stopping conditions as a convenience. For example, to continue until the loss is below a threshold x, you may specify loss_goal=x. Similarly, to continue until n points have been sampled, you may specify npoints_goal=n. See the Runner docstring for details.

adaptive.runner.auto_goal(*, loss: Optional[float] = None, npoints: Optional[int] = None, end_time: Optional[datetime] = None, duration: Optional[Union[timedelta, int, float]] = None, learner: Optional[LearnerType] = None, allow_running_forever: bool = True) Callable[[LearnerType], bool][source]#

Extract a goal from the learners.

Parameters:
  • loss (float, optional) – Stop when the loss is smaller than this value.

  • npoints (int, optional) – Stop when the number of points is larger or equal than this value.

  • end_time (datetime, optional) – Stop when the current time is larger or equal than this value.

  • duration (timedelta or number, optional) – Stop when the current time is larger or equal than start_time + duration. duration can be a number indicating the number of seconds.

  • learner – Learner for which to determine the goal. Only used if the learner type is BalancingLearner, DataSaver, SequenceLearner, or IntegratorLearner.

  • allow_running_forever – If True, and the goal is None and the learner is not a SequenceLearner, then a goal that never stops is returned, otherwise an exception is raised.

Return type:

Callable[[adaptive.BaseLearner], bool]

adaptive.runner.stop_after(*, seconds=0, minutes=0, hours=0) Callable[[LearnerType], bool][source]#

Stop a runner after a specified time.

For example, to specify a runner that should stop after 5 minutes, one could do the following:

>>> runner = Runner(learner, goal=stop_after(minutes=5))

To stop a runner after 2 hours, 10 minutes and 3 seconds, one could do the following:

>>> runner = Runner(learner, goal=stop_after(hours=2, minutes=10, seconds=3))
Parameters:
  • seconds (float, default: 0) – If more than one is specified, then they are added together

  • minutes (float, default: 0) – If more than one is specified, then they are added together

  • hours (float, default: 0) – If more than one is specified, then they are added together

Returns:

goal – Can be used as the goal parameter when constructing a Runner.

Return type:

callable

Notes

The duration specified is only a lower bound on the time that the runner will run for, because the runner only checks its goal when it adds points to its learner

Simple executor#

adaptive.runner.simple(learner: LearnerType, goal: Optional[Callable[[LearnerType], bool]] = None, *, loss_goal: Optional[float] = None, npoints_goal: Optional[int] = None, end_time_goal: Optional[datetime] = None, duration_goal: Optional[Union[timedelta, int, float]] = None)[source]#

Run the learner until the goal is reached.

Requests a single point from the learner, evaluates the function to be learned, and adds the point to the learner, until the goal is reached, blocking the current thread.

This function is useful for extracting error messages, as the learner’s function is evaluated in the same thread, meaning that exceptions can simple be caught an inspected.

Parameters:
  • learner (BaseLearner` instance) –

  • goal (callable, optional) – The end condition for the calculation. This function must take the learner as its sole argument, and return True when we should stop requesting more points.

  • loss_goal (float, optional) – Convenience argument, use instead of goal. The end condition for the calculation. Stop when the loss is smaller than this value.

  • npoints_goal (int, optional) – Convenience argument, use instead of goal. The end condition for the calculation. Stop when the number of points is larger or equal than this value.

  • end_time_goal (datetime, optional) – Convenience argument, use instead of goal. The end condition for the calculation. Stop when the current time is larger or equal than this value.

  • duration_goal (timedelta or number, optional) – Convenience argument, use instead of goal. The end condition for the calculation. Stop when the current time is larger or equal than start_time + duration_goal. duration_goal can be a number indicating the number of seconds.

Sequential excecutor#

class adaptive.runner.SequentialExecutor[source]#

A trivial executor that runs functions synchronously.

This executor is mainly for testing.

Replay log#

adaptive.runner.replay_log(learner: LearnerType, log: list[tuple[Literal['tell'], Any, Any] | tuple[Literal['ask'], int]]) None[source]#

Apply a sequence of method calls to a learner.

This is useful for debugging runners.

Parameters:
  • learner (BaseLearner instance) – New learner where the log will be applied.

  • log (list) – contains tuples: (method_name, *args).