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
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 | |
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