Skip to content

Issue 37 custom cv scorer - #69

Open
hanyangii wants to merge 4 commits into
mainfrom
Issue-37-custom-cv-scorer
Open

Issue 37 custom cv scorer#69
hanyangii wants to merge 4 commits into
mainfrom
Issue-37-custom-cv-scorer

Conversation

@hanyangii

Copy link
Copy Markdown
Collaborator
  • Introduced an AbstractMotherTuner abstract base class in core.py, defining the structure for tuner classes and requiring implementation of objective and call_optimize methods. This enables users to create custom tuner subclasses for specialized optimization workflows.
  • Added a new ObjectiveContext dataclass to encapsulate all arguments and state needed for optimization, making it easier to pass information between methods and customize the optimization process.
  • Refactored the original MotherTuner class to inherit from AbstractMotherTuner and moved the core optimization logic into the new context-based structure. The MotherTuner class now implements the abstract methods and uses the ObjectiveContext for its operations.
  • Created a notebook to explain how to use the new class

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ObjectiveContext to bundle optimize() inputs for custom objective/optimize implementations.
  • Introduced AbstractMotherTuner and refactored MotherTuner to implement the abstract objective() and call_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,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable Users to provide their own cv function and/or tuning function to optuna

3 participants