pygx.algo.early_stopping¶
Early stopping policies for tuning loops.
early_stopping
¶
Early stopping policies for tuning loops.
Early stopping policies are evaluated against measurements reported during a
trial and decide whether the trial should be abandoned before completion.
They are typically passed to pg.sample via the
early_stopping_policy argument.
This module exposes:
StepWise— abandon a trial when the reported metric falls outside step-keyed thresholds.early_stop_by_value/early_stop_by_rank— convenience constructors for the two most common StepWise variants.And/Or/Not— Boolean combinators for composing policies.
And
¶
And(*children: EarlyStoppingPolicy, **kwargs)
Bases: Composite
Logical AND as a composite early stopping policy.
Source code in pygx/algo/early_stopping/_base.py
EarlyStopingPolicyBase
¶
EarlyStopingPolicyBase(
*,
allow_partial: bool = False,
sealed: bool | None = None,
root_path: KeyPath | None = None,
explicit_init: bool = False,
**kwargs: Any
)
Bases: EarlyStoppingPolicy
An early stopping policy base class that supports composition.
Source code in pygx/symbolic/_object.py
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 | |
Not
¶
Not(*children: EarlyStoppingPolicy, **kwargs)
Bases: Composite
Logical OR as a composite early stopping policy.
Source code in pygx/algo/early_stopping/_base.py
Or
¶
Or(*children: EarlyStoppingPolicy, **kwargs)
Bases: Composite
Logical OR as a composite early stopping policy.
Source code in pygx/algo/early_stopping/_base.py
StepWise
¶
Bases: EarlyStopingPolicyBase
Step-wise early stopping policy.
Source code in pygx/algo/early_stopping/_step_wise.py
should_stop_early
¶
should_stop_early(trial: Trial) -> bool
Returns True if a trial should be stopped early.
Source code in pygx/algo/early_stopping/_step_wise.py
recover
¶
recover(history: Iterable[Trial])
Recovers the policy state based on history.
Source code in pygx/algo/early_stopping/_step_wise.py
early_stop_by_rank
¶
early_stop_by_rank(
step_ranks: list[tuple[int, float | int, int]],
metric: str | Callable[[Measurement], float] = "reward",
maximize: bool = True,
) -> StepWise
Step-wise early stopping policy based on the rank of reward/metric.
Example::
policy = early_stop_by_rank([ # Stop at step 1 if accuracy is less than top 80% previous trials at # this step, enabled when there are at least 5 previous trials reported # at this step. (1, 0.8, 5),
# Stop at step 2 if accuracy is less than top 20% previous trials at
# this step, enabled when there are at least 10 previous trials reported
# at this step.
(2, 0.2, 10),
# Stop at step 3 if accuracy is less than the 3rd best trial at this step,
# enabled when there are at least 3 previous trials reported at this step.
(3, 3, 3)
], metric='accuracy')()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_ranks
|
list[tuple[int, float | int, int]]
|
A list of tuple (gating step, rank threshold, trigger histogram size). gating step - At which step this rule will be triggered. rank threshold - A float number in range (0, 1) indicating the rank percentage or an integer (> 0) indicating the absolute rank as the threshold for early stopping. trigger historgram size - The minimal number of historical trials repoted at current step for this rule to trigger. |
required |
metric
|
str | Callable[[Measurement], float]
|
Based on which metric the rank will be computed. Use str for metric name or a callable object that takes a measurement object at a given step as input and returns a float value. |
'reward'
|
maximize
|
bool
|
If True, reward or metric value below the threshold will be stopped, otherwise trials with values above the threshold will be stopped. |
True
|
Returns:
| Type | Description |
|---|---|
StepWise
|
A |
Source code in pygx/algo/early_stopping/_step_wise.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 | |
early_stop_by_value
¶
early_stop_by_value(
step_values: list[tuple[int, float]],
metric: str | Callable[[Measurement], float] = "reward",
maximize: bool = True,
) -> StepWise
Step-wise early stopping policy based on the value of reward/metric.
Example::
policy = early_stop_by_value([ # Stop at step 1 if trial reward is less than 0.2. (1, 0.2),
# Stop at step 2 if trial reward is less than 0.8.
(2, 0.8),
])()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_values
|
list[tuple[int, float]]
|
A list of tuple (gating step, value threshold). gating step - At which step this rule will be triggered. value threshold - A float number indicating the threshold value for early stopping. |
required |
metric
|
str | Callable[[Measurement], float]
|
Based on which metric the value should be compared against. Use str for metric name or a callable object that takes a measurement object at a given step as input and returns a float value. |
'reward'
|
maximize
|
bool
|
If True, reward or metric value below the threshold will be stopped, otherwise trials with values above the threshold will be stopped. |
True
|
Returns:
| Type | Description |
|---|---|
StepWise
|
A |
Source code in pygx/algo/early_stopping/_step_wise.py
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2