early_stopping
orchard.optimization.early_stopping
¶
Early Stopping Callback for Optuna Studies.
Provides StudyEarlyStoppingCallback and its factory
get_early_stopping_callback, which terminate an Optuna study when a
user-defined (or metric-specific default) performance threshold is
sustained for a configurable number of consecutive trials.
Key Functions
get_early_stopping_callback: Factory that resolves sensible
default thresholds for common metrics (AUC, accuracy, F1,
loss, MAE, MSE) and returns a configured callback.
Key Components
StudyEarlyStoppingCallback: Optuna callback that tracks
consecutive threshold hits and calls study.stop() once
patience is reached. Direction-aware (maximize/minimize).
Example
callback = get_early_stopping_callback("auc", "maximize") study.optimize(objective, callbacks=[callback])
StudyEarlyStoppingCallback(threshold, direction='maximize', patience=2, enabled=True)
¶
Callback to stop Optuna study when target metric is achieved.
Prevents wasteful computation when near-perfect performance is reached (e.g., AUC > 0.9999).
Example
callback = StudyEarlyStoppingCallback( threshold=0.9999, direction="maximize", patience=3 ) study.optimize(objective, callbacks=[callback])
Attributes:
| Name | Type | Description |
|---|---|---|
threshold |
float
|
Metric value that triggers early stopping. |
direction |
str
|
"maximize" or "minimize". |
patience |
int
|
Number of trials meeting threshold before stopping. |
_count |
int
|
Internal counter for consecutive threshold hits. |
Initialize early stopping callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Target metric value (e.g., 0.9999 for AUC) |
required |
direction
|
str
|
"maximize" or "minimize" (should match study direction) |
'maximize'
|
patience
|
int
|
Number of consecutive trials meeting threshold before stop |
2
|
enabled
|
bool
|
Whether callback is active (allows runtime disable) |
True
|
Source code in orchard/optimization/early_stopping.py
__call__(study, trial)
¶
Callback invoked after each trial completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
study
|
Study
|
Optuna study instance |
required |
trial
|
FrozenTrial
|
Completed trial |
required |
Side Effects
Calls study.stop() when early stopping criteria are met.
Source code in orchard/optimization/early_stopping.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
get_early_stopping_callback(metric_name, direction, threshold=None, patience=2, enabled=True, task_thresholds=None)
¶
Factory function to create appropriate early stopping callback.
Lookup order: explicit threshold → task_thresholds from the task
registry → None (disabled with warning).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
Name of metric being optimized (e.g., "auc", "accuracy") |
required |
direction
|
str
|
"maximize" or "minimize" |
required |
threshold
|
float | None
|
Custom threshold (if None, uses task default) |
None
|
patience
|
int
|
Trials meeting threshold before stopping |
2
|
enabled
|
bool
|
Whether callback is active |
True
|
task_thresholds
|
Mapping[str, float] | None
|
Task-specific metric→threshold mapping from the task registry. |
None
|
Returns:
| Type | Description |
|---|---|
StudyEarlyStoppingCallback | None
|
Configured callback or None if disabled |