Repository navigation
Description
Support resuming training on loaded models
Target: 0.3
Summary
A model loaded with openmodels should be able to continue training — via partial_fit, or warm_start=True and fit again — and end up with exactly the same model as if the original had continued training in memory. This already works for most estimators. A few can't resume, because state they only use while training isn't saved.
Motivation
- Online learning in production: load a model,
partial_fitit on new data, save it again. The saved file becomes the model's checkpoint; no full retrain each time. - Out-of-core / multi-job training: train on data too large for one process in chunks across separate jobs, saving in between.
- Checkpointing long training: grow a gradient-boosting model in stages with
warm_startand resume after an interruption instead of starting over. - Growing or fine-tuning later: add trees to a deployed ensemble, or fine-tune an MLP on new data.
Pickle users can already do all of this. Users who choose openmodels for a safe, portable format shouldn't silently lose it.
Current state
Measured on every supported estimator that can continue training, in JSON and pickle, by continuing training on the original and on a loaded copy and comparing every inference method afterwards:
- 52 cases continue identically, including the SGD models, naive Bayes, the scalers,
IncrementalPCA,LatentDirichletAllocation,Birch, forests, bagging, linear models, mixtures,MLP*withwarm_start, and meta-estimators wrapping an incremental model. - These can't resume yet — they fail with
AttributeErroron a training-only attribute that isn't saved:
| Estimator | Method | Unsaved training state (at least) |
|---|---|---|
MiniBatchKMeans |
partial_fit |
_counts, _n_since_last_reassign, _ewa_inertia, _ewa_inertia_min, _no_improvement, _batch_size |
MiniBatchNMF |
partial_fit |
_components_numerator, _components_denominator, _ewa_cost, _ewa_cost_min, _no_improvement, _rho, _batch_size |
MiniBatchDictionaryLearning |
partial_fit |
_A, _B, _random_state, _ewa_cost, _ewa_cost_min, _no_improvement, _batch_size |
MLPClassifier, MLPRegressor |
partial_fit |
_optimizer (SGD/Adam state), _no_improvement_count, epts, _random_state` |
GradientBoostingClassifier, GradientBoostingRegressor |
warm_start |
_rng |
HistGradientBoostingClassifier, HistGradientBoostingRegressor |
warm_start |
_random_seed, _featuidation_data, _scorer, ... |
These are documented in the "Resuming Training" section of docs/supported_models.md, and pinned by test/test_resume_training.py, where they're strict xfails in KNOWN_FAILURES.
Proposal
Save the training-only state these estimators need, and restore it on load so that continuing training is bit-for-bit identical to continuing on the original:
- Plain attributes (counters, running sums, arrays,
_batch_size, ...): add them to the saved private attributes. - Random-number generator state (
_rng,_random_state,_feature_subsample_rng): save the full generator state (NumPyRandomState/Generator), not just the seed, so continuation draws the same numbers. - Optimizer objects (
MLP*._optimizer:SGDOptimizerwith velocities,AdamOptimizerwith first/second moments and step count): a dedicated handler that saves and rebuilds them. - Callables and scorers held during training (e.g.
HistGradientBoosting*._scorer): rebuild them from params on load where possible, asfitdoes, rather than saving scorer objects.
Considerations
- File size: training state can be large; e.g. Adam's two moment arrays roughly triple an MLP's weight data. Options: always save it, or make it opt-in (e.g.
serialize(model, include_training_state=True)) so inference-only files stay small. To decide. - Format: the change is additive. Files written by 0.2.x still load in 0.3 for inference; resuming from them needs a refit, or a re-save with 0.3 from a model still in memory. Decide whether this warrants a format version bump, and document the behaviour in the 0.3 notes.
- scikit-learn versions: these attributes are private and can change between releases; each needs checking across all tested scikit-learn versions, as the existing private-attribute handling does.
- Testing: fit the two models separately with the same seed. Never
copy.deepcopya model that will be trained further: deep copies don't keep NumPy views, and some estimators (e.g. Birch) update their state through views, so a deep copy can continue differently from the model itself.
Acceptance criteria
- Every estimator in the table resumes identically after loading, in every supported format.
- Their entries are removed from
KNOWN_FAILURESintest/test_resume_training.py(the strict xfails flip to passing), and the conformance battery'scheck_fit_score_takes_yxfails for the MiniBatch estimators are removed. - The decision on always vs. opt-in training state is implemented and documented.
-
docs/supported_models.md"Resuming Training" and the README are updated; the changelog describes how older files behave.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsReady
Activity