Skip to content

Commit eac1015

Browse files
committed
add support for 'early_stopping'
1 parent cbf98a2 commit eac1015

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

src/hyperactive/opt/_adapters/_base_optuna_adapter.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,25 @@ def _solve(self, experiment, param_space, n_trials, **kwargs):
161161

162162
optimizer = self._get_optimizer()
163163

164-
# Create study
165164
study = optuna.create_study(
166-
direction="maximize", # Assuming we want to maximize scores
165+
direction="maximize",
167166
sampler=optimizer,
168167
)
169168

170-
# Setup initial positions
171169
self._setup_initial_positions(study)
172170

173-
# Setup early stopping callback
174171
callbacks = []
175172
if self.early_stopping is not None:
173+
patience = self.early_stopping
176174

177175
def early_stopping_callback(study, trial):
178-
if len(study.trials) >= self.early_stopping:
176+
if trial.number < patience:
177+
return
178+
if trial.number - study.best_trial.number >= patience:
179179
study.stop()
180180

181181
callbacks.append(early_stopping_callback)
182182

183-
# Run optimization
184183
study.optimize(
185184
self._objective,
186185
n_trials=n_trials,

0 commit comments

Comments
 (0)