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 for classification tasks).
Usage
callback = StudyEarlyStoppingCallback( threshold=0.9999, direction="maximize", patience=3 ) study.optimize(objective, callbacks=[callback])
Attributes:
| Name | Type | Description |
|---|---|---|
threshold |
Metric value that triggers early stopping |
|
direction |
"maximize" or "minimize" |
|
patience |
Number of trials meeting threshold before stopping |
|
_count |
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
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 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 | |
get_early_stopping_callback(metric_name, direction, threshold=None, patience=2, enabled=True)
¶
Factory function to create appropriate early stopping callback.
Provides sensible defaults for common metrics.
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 metric-specific default) |
None
|
patience
|
int
|
Trials meeting threshold before stopping |
2
|
enabled
|
bool
|
Whether callback is active |
True
|
Returns:
| Type | Description |
|---|---|
StudyEarlyStoppingCallback | None
|
Configured callback or None if disabled |