Skip to content

fix(models): default random_state to None in build_model and TabularDataModule - #641

Open
Alvaropar wants to merge 1 commit into
OpenTabular:mainfrom
Alvaropar:fix/random-state-default
Open

Alvaropar wants to merge 1 commit into
OpenTabular:mainfrom
Alvaropar:fix/random-state-default

Conversation

@Alvaropar

Copy link
Copy Markdown

Problem

The constructors and fit() default random_state to None and resolve it as "an explicit
argument wins over the constructor's; with neither, nothing is reseeded" (#452, #553).
build_model() (classifiers, regressors, LSS) and TabularDataModule.__init__ /
preprocess_data still default to 101. So build_model() ignores the estimator's own seed:
MLPLSS(random_state=42).build_model(...) (docs/core_concepts/sklearn_api.md) splits,
preprocesses and orders the loader with 101. An unseeded build_model() is silently seeded
with 101, while an unseeded fit() is not.

Reproduction

import numpy as np, pandas as pd
from deeptab.models import MLPRegressor

X = pd.DataFrame(np.random.default_rng(0).normal(size=(200, 4)), columns=list("abcd"))
y = X["a"].to_numpy()
rows = lambda m: np.sort(m._data_module.X_val.index.to_numpy())
a = MLPRegressor(random_state=42).build_model(X, y)
b = MLPRegressor().build_model(X, y, random_state=42)
print(np.array_equal(rows(a), rows(b)))   # main: False; this branch: True

Fix

All of them default to None, resolved as fit() does: the call's random_state, else the
estimator's (or, in preprocess_data, the module's, as val_size=None already falls back to
the module's val_size); with neither, unseeded. build_model() still does not call
set_seed(); its docstring now says so.

Behaviour change

  • A fit that rebuilds (rebuild=True, the default) passes the resolved seed explicitly and
    draws what it drew before.
  • build_model() without a seed now uses the constructor's random_state, or none, instead
    of 101. The two docs examples that call it without a seed now follow the constructor's
    random_state=42: config_system.md (build_model(), then inspecting widths) and
    sklearn_api.md (build_model(), then fit(rebuild=False)).
  • build_model() then fit(rebuild=False) keeps the preprocessor that build_model()
    fitted, now on the constructor seed's training partition rather than 101's.
  • A directly built TabularDataModule without a seed is unseeded. Pass random_state=101 to
    keep the old behaviour.

Tests

tests/core/test_random_state.py: the defaults; build_model() uses the constructor's seed,
and an explicit one wins; unseeded builds pass random_state=None to train_test_split;
preprocess_data falls back to the module's seed; a rebuilding fit draws the same validation
rows as build_model(random_state=s); build_model() then fit(rebuild=False) keeps a
preprocessor fitted on the constructor seed's partition. 14 of the 23 cases fail on main;
all pass here.

Docs

The build_model(), TabularDataModule and preprocess_data docstrings;
training_and_evaluation.md ("The random_state parameter") and config_system.md (the
split's seed).

Checks

  • On main @ 3f2d149: tests/core and tests/data: 480 passed, 3 skipped. The new tests
    fail on main (14 of 23) and pass here. just check: all hooks passed.
  • just test before the rebase (on eb15b6b): 1 failed, 3387 passed, 55 skipped, 28 xfailed;
    coverage 91.30 %. The failure,
    test_retrieval_predictions_in_fresh_process[True-normal-modernnca], also fails on main
    here (Windows, CPU: relative difference 1.698e-06 against rtol=1e-6). just docs: build
    succeeded.

…ataModule

build_model() on classifiers, regressors and LSS estimators, and
TabularDataModule.__init__ / preprocess_data, defaulted to 101 while the
constructors and fit() default to None. build_model() therefore ignored
the estimator's own random_state, and an unseeded build_model() was
silently seeded with 101.

All of them now default to None and resolve it as fit() does: an
explicit argument wins, then the estimator's (or the data module's)
random_state; with neither, nothing is seeded. A fit that rebuilds (the
default) passes the resolved seed explicitly and is unchanged. A
fit(rebuild=False) after build_model() keeps the preprocessor that
build_model() fitted, now on the constructor seed's partition rather
than 101's. build_model() still does not call set_seed(); its docstring
now says so.

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.

1 participant