Use a context manager for callback context init and clean-up in _fit_context #25
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Following what was discussed in the callback API meeting, this PR uses the
_fit_contextprivate decorator to initialize and clean-up the CallbackContext objects, using a context manager. A public decorator that only does the callback context management is also added for 3rd party devs who want callbacks without the param validation in_fit_context. And the callback context attribute is renamed.Since the callback context is initialised outside of the fit function, its
max_iterattribute must be set outside of the init function. Right now it is done simply by assigning a value to the attribute, it could be done through a dedicated setter method, or ineval_on_fit_begin.Some tests are refactored to use
_from_estimatorto instantiate the callback contexts.Some tests are added, one for the new public decorator, and one for estimators that uses their parent's fit. The specific problem in the latter case was that in that case the decorator is called twice, meaning some actions must be wrapped in a try except, like the deletion of the
_callback_fit_ctxattribute, otherwise trying to delete the second time raises an error.