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 based on step-keyed predicates over its reported measurements.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
Composite
¶
Composite(*children: EarlyStoppingPolicy, **kwargs)
Bases: EarlyStoppingPolicyBase
Base class for composite early stopping policies.
Accept children as positional varargs: And(p1, p2, p3).
Source code in pygx/algo/early_stopping/_base.py
EarlyStoppingPolicyBase
¶
EarlyStoppingPolicyBase(
*,
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
3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 | |
Not
¶
Not(*children: EarlyStoppingPolicy, **kwargs)
Bases: Composite
Logical NOT 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: EarlyStoppingPolicyBase
Early stopping policy driven by a step-keyed plan.
The plan is a list of (gating step, predicate) tuples. When a trial
reports a measurement at or beyond a gating step, that gate's predicate
is called with the trial's latest measurement and the list of
measurements that earlier trials reported at the same gate; returning
True abandons the trial. Each trial is decided at most once per gate.
See early_stop_by_value
and early_stop_by_rank
for ready-made plans.
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.0, 1.0] indicating the rank percentage or an integer (> 0) indicating the absolute rank as the threshold for early stopping. trigger histogram size - The minimal number of historical trials reported 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, trials whose value ranks below the threshold are stopped; otherwise trials whose value ranks above the threshold are stopped. |
True
|
Returns:
| Type | Description |
|---|---|
StepWise
|
A |
Source code in pygx/algo/early_stopping/_step_wise.py
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
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, trials whose value is below the threshold are stopped; otherwise trials whose value is above the threshold are stopped. |
True
|
Returns:
| Type | Description |
|---|---|
StepWise
|
A |
Source code in pygx/algo/early_stopping/_step_wise.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2