Repository navigation
Preserve model specifications when refitting metalearners - #1084
Conversation
a45a09a to
26c8c28
Compare
|
Merged main to pick up #1082’s Python 3.9 runner fix. The original product code and tests are unchanged. The 25 workflow tests and six metalearner tests pass locally, including four subtests. |
kbattocchi
left a comment
There was a problem hiding this comment.
Thanks for the contribution - this is a good catch and definitely worth fixing.
However, as to the strategy of the fix, I think it would be better to use the sklearn approach of never modifying initializer arguments in fit - instead we should write the updated models into a separate _models or _cate_models field and leave models alone (likewise, we should actually be doing the cloning of the models in fit as part of this logic, not in the initializer for each class).
|
Thanks, that makes sense. I’ve moved cloning into |
kbattocchi
left a comment
There was a problem hiding this comment.
Thanks for revising, this looks much better, just a few smaller comments this time around.
| else: | ||
| force_all_finite = False | ||
| X, = check_input_arrays(X, force_all_finite=force_all_finite, ensure_2d=True) | ||
| models = getattr(self, 'models_', self.models) # fitted estimators from older versions |
There was a problem hiding this comment.
Is it correct to fall back to self.models as a default? It seems like we should never do this, since that implies that the estimator hasn't been fit, right?
There was a problem hiding this comment.
That fallback was for older pickles, where models could contain the already fitted models. I’ve removed it. T-learner now reads models_ directly, and I’ve updated the PR description to call out the compatibility change.
| Xs, Ts = broadcast_unit_treatments(X, self._d_t[0] + 1) | ||
| feat_arr = np.concatenate((Xs, Ts), axis=1) | ||
| prediction = self.overall_model.predict(feat_arr).reshape((-1, self._d_t[0] + 1,) + self._d_y) | ||
| model = getattr(self, 'overall_model_', self.overall_model) # fitted estimators from older versions |
There was a problem hiding this comment.
Same concern about fallback here.
There was a problem hiding this comment.
Same change here. S-learner prediction now reads overall_model_ directly.
| models, | ||
| cate_models=None, | ||
| propensity_model=LogisticRegression(), | ||
| propensity_model=_DEFAULT_PROPENSITY_MODEL, |
There was a problem hiding this comment.
I think this change is now unnecessary since we fit only on clones, so this can be reverted to LogisticRegression and the sentinel logic can be removed.
There was a problem hiding this comment.
I restored the LogisticRegression() default. I kept a small identity check so each learner gets its own instance. Otherwise set_params on one learner’s default would affect the others. Fitting still clones the model per arm.
| models, | ||
| final_models, | ||
| propensity_model=LogisticRegression(), | ||
| propensity_model=_DEFAULT_PROPENSITY_MODEL, |
There was a problem hiding this comment.
Same comment about reverting the sentinel applies here.
There was a problem hiding this comment.
I applied the same change here. Each learner gets its own default, and an explicitly supplied model is kept as the constructor parameter and cloned during fit.
| X, = check_input_arrays(X, force_all_finite=force_all_finite, ensure_2d=True) | ||
| taus = [] | ||
| for model in self.final_models: | ||
| for model in getattr(self, 'final_models_', self.final_models): |
There was a problem hiding this comment.
Same concern about fallback here.
There was a problem hiding this comment.
Domain Adaptation now uses final_models_ directly for both prediction and SHAP.
kbattocchi
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
Signed-off-by: Aryan Rajesh Ganjoo <[email protected]>
Signed-off-by: Aryan Rajesh Ganjoo <[email protected]>
Signed-off-by: Aryan Rajesh Ganjoo <[email protected]>
1a428da to
eae1885
Compare
TLearner,XLearnerandDomainAdaptationLearnerreplace theirmodels(andcate_models/final_models) arguments duringfitwith the per-arm list they train. A secondfitvalidates that list instead of the original argument. Refitting the same estimator on data with a different number of treatment categories therefore fails:The metalearners now keep the constructor model specifications and clone them in
fit. Fitted outcome and treatment effect models live in dedicated attributes. Each refit starts with fresh clones of the current specifications. Prediction uses the fitted attributes directly. Older pickles of fitted T-, S-, and Domain Adaptation learners may need to be refit before predicting with this version. This also applies to SHAP for Domain Adaptation.The guarded metalearner tests and pandas integration case passed on the PR branch and on its integration with current
main(6 tests and 4 subtests each). I also checked fitted-model prediction and SHAP, and verified that editing a default propensity model leaves other learners’ defaults intact. Ruff passed on the changed file.Prepared with AI assistance.