Issue 37 custom cv scorer - #69
Open
hanyangii wants to merge 4 commits into
Open
Conversation
hanyangii
requested review from
SommerKai and
thomasATbayer
and
a lite review from Copilot
August 5, 2026 13:39
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors MotherML’s Optuna tuning layer to make hyperparameter optimization extensible: it introduces a new AbstractMotherTuner base class and an ObjectiveContext dataclass so users can implement custom optimization workflows while reusing the common optimize() orchestration.
Changes:
- Added
ObjectiveContextto bundleoptimize()inputs for custom objective/optimize implementations. - Introduced
AbstractMotherTunerand refactoredMotherTunerto implement the abstractobjective()andcall_optimize()hooks. - Added an advanced notebook demonstrating custom tuners and a custom PyTorch-wrapped model workflow.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
uv.lock |
Updates lock metadata/markers and reflects the project version used for dependency resolution. |
src/mother/optimization/core.py |
Adds ObjectiveContext + AbstractMotherTuner and refactors MotherTuner onto the new hook-based architecture. |
src/mother/optimization/__init__.py |
Exposes the new tuner API (AbstractMotherTuner, ObjectiveContext) at the package level. |
examples/notebooks/05_advanced/05_custom_model_tuner.ipynb |
Provides user-facing examples of building custom tuners and a torch-backed model for tuning. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| self.sampler = sampler | ||
|
|
||
| self.study: typing.Optional[Study] = None | ||
| self.study: typing.Optional[Study] | None = None |
Comment on lines
+325
to
+328
| sampler (optuna.samplers.BaseSampler): Sampler for Optuna trials. | ||
| study (typing.Optional[Study]): Optuna study object. | ||
| **kwargs (Any): additional arguments for the scorer | ||
|
|
Comment on lines
+403
to
+404
| " return self.__init__(**self._init_params)\n", | ||
| "\n", |
Comment on lines
+529
to
+536
| " estimator = skl_base.clone(context.estimator)\n", | ||
| " suggested_params_to_train: dict = context.get_hyper_space(trial=trial, X=context.X, y=context.y)\n", | ||
| " estimator.set_params(**suggested_params_to_train)\n", | ||
| " estimator.fit(X_train, y_train)\n", | ||
| "\n", | ||
| " # calculate valid loss\n", | ||
| " valid_loss, valid_acc = context.estimator.validation(X_valid, y_valid)\n", | ||
| "\n", |
Comment on lines
+555
to
+561
| "model_tuned = tuner.optimize(\n", | ||
| " model,\n", | ||
| " X,\n", | ||
| " y,\n", | ||
| " cross_validation=None,\n", | ||
| " hyperparameter_space_function=model.get_hyperparameter_space,\n", | ||
| ")" |
Comment on lines
+123
to
127
| class AbstractMotherTuner(ABC): | ||
| def __init__( | ||
| self, | ||
| scorer: typing.Union[typing.Callable, str], | ||
| sampler: typing.Optional[optuna.samplers.BaseSampler] = None, | ||
| early_stopping_optuna: bool = False, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
AbstractMotherTunerabstract base class incore.py, defining the structure for tuner classes and requiring implementation ofobjectiveandcall_optimizemethods. This enables users to create custom tuner subclasses for specialized optimization workflows.ObjectiveContextdataclass to encapsulate all arguments and state needed for optimization, making it easier to pass information between methods and customize the optimization process.MotherTunerclass to inherit fromAbstractMotherTunerand moved the core optimization logic into the new context-based structure. TheMotherTunerclass now implements the abstract methods and uses theObjectiveContextfor its operations.