Skip to content

fix(models): update fit()'s state when set_params() assigns a config - #514

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/set-params-config-sync
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/set-params-config-sync

Conversation

@ChrisW09

@ChrisW09 ChrisW09 commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #513

Problem

__init__ derives the state fit() reads from the three config parameters: the architecture config from model_config (self.config), the preprocessor from preprocessing_config (self._preprocessor, self._preprocessor_kwargs) and the optimizer from trainer_config (self._optimizer_type). SklearnBase.set_params() re-derived this state only when a config object replaced the config of an estimator that already held one. As a result:

  • On a default-constructed estimator, config objects passed through set_params() were stored but never trained. GridSearchCV clones such an estimator for every candidate, so searches over model_config objects trained the default architecture.
  • set_params(model_config=None), preprocessing_config=None or trainer_config=None kept the previous architecture, preprocessing and optimizer, and with all three set to None, clone() raised ValueError.

Fix

deeptab/models/base.py: a new SklearnBase._set_config_param(name, value) assigns one of the three config parameters and re-derives the state fit() reads from it, as __init__ does:

  • a config object sets config/_config_kwargs, rebuilds the preprocessor from the PreprocessingConfig, or takes the TrainerConfig's optimizer;
  • None (replacing a config) restores the defaults of an estimator constructed without it: the default model config, the default preprocessor with empty _preprocessor_kwargs, and Adam.

Both branches of set_params() use it. The legacy branch, which default-constructed estimators take, therefore keeps accepting flat keys such as layer_sizes next to a config object in the same call. Objects that aren't DeepTab configs are stored unchanged, as before. The set_params docstring says that assigning a config updates what fit() trains with.

Tests

tests/models/test_estimator_configuration.py::TestSetParamsConfigsReachFit:

  • test_model_config_on_default_estimator_is_trained
  • test_trainer_and_preprocessing_configs_on_default_estimator
  • test_grid_search_over_model_config_objects_trains_selected_config
  • test_setting_configs_to_none_restores_defaults (including clone() afterwards)
  • test_flat_keys_still_apply_next_to_a_config_object: MLPRegressor().set_params(trainer_config=..., layer_sizes=[4]) keeps working.

The first four fail on main and pass with this change. The last one passes on main too and guards that behaviour.

A related defect with its own cause, model_config__* keys being dropped when the estimator has no model_config, is #613 (fixed in #614); the two PRs merge cleanly.

Checks

  • just test: 3241 passed, 46 skipped, 28 xfailed; coverage 91.32%
  • just docs: build succeeded (warnings treated as errors)
  • just check: all hooks passed (ruff, prettier, pyright)

__init__ derives config, _preprocessor and _optimizer_type from the
three config parameters, but set_params() only re-derived them when a
config object replaced another one. On a default-constructed estimator
(as cloned by GridSearchCV) a config object set through set_params() was
ignored by fit(), and setting a config back to None kept the previous
configuration. Both code paths now share one helper.

Fixes #513
@ChrisW09
ChrisW09 force-pushed the fix/set-params-config-sync branch from de29b2f to a448034 Compare October 21, 2026 22:44
@ChrisW09 ChrisW09 changed the title fix(models): keep fit() in sync with configs set through set_params fix(models): update fit()'s state when set_params() assigns a config Oct 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Config objects (or None) set via set_params() are not what fit() trains with, so searches over model_config tune nothing

1 participant