Skip to content

xgboost/reused-early-stopping-callback

Rule reused-early-stopping-callback
Pack XGBoost
Severity warning
Fix none, edit by hand
Library versions every version

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])

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.

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

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

XGBoost EarlyStopping retains state from the previous fit when this loop reuses it. Create a fresh callback inside the loop before each train call.

Browse the other rules of the XGBoost pack.