xgboost/reused-early-stopping-callback
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| XGBoost | warning | none, edit by hand | All versions |
XGBoost's EarlyStopping callback stores training history and a count of
rounds without improvement. Reusing it can carry those values into a new fit
and stop that fit sooner than intended.
stop = xgb.callback.EarlyStopping(rounds=5)for params in parameter_grid: stop = xgb.callback.EarlyStopping(rounds=5) model = xgb.train(params, train, evals=[(valid, "valid")], callbacks=[stop])What the rule reports
Section titled “What the rule reports”The rule checks callback lists and tuples passed to train in the body of a
loop. It reports any EarlyStopping instance created immediately before
the loop that the loop reuses. A name rebound by the loop target can refer
to a different callback on each pass.
How to fix it
Section titled “How to fix it”Construct a fresh callback for each training run. Where a callback must be copied, copy the unused instance before any fit.
Training API and callback reuse
Diagnostic message
Section titled “Diagnostic message”XGBoost EarlyStopping retains state from the previous fit when this loop reuses it. Create a fresh callback inside the loop before each train call.See all rules in the XGBoost pack.