Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass callbacks kwarg to study.optimize() in run_hp_search_optuna() #34732

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

milos7250
Copy link

@milos7250 milos7250 commented Nov 14, 2024

What does this PR do?

This PR allows the user to specify additional callbacks for optuna's study.optimize(). Previously, the callbacks kwarg would be passed to optuna.create_study() and would cause an Exception.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

This PR allows the user to specify additional callbacks for optuna's `study.optimize()`. Previously, the callbacks kwarg would be passed to `optuna.create_study()` and would cause an Exception.
@LysandreJik
Copy link
Member

Is it possible to add an example of callback usage to make it clearer how this should be used?

@milos7250
Copy link
Author

A general example of setting up a callback can be found on the optuna readthedocs. The idea is to allow users to have greater control over results in individual trials (e.g. adding more pruning conditions or further logging).

This PR allows the user to pass a callbacks argument to trainer.hyperparameter_search() containing list of callbacks to be used in the study. For example:

class StopWhenTrialKeepBeingPrunedCallback:
    def __init__(self, threshold: int):
        self.threshold = threshold
        self._consequtive_pruned_count = 0

    def __call__(self, study: optuna.study.Study, trial: [optuna.trial.FrozenTrial](https://docs.python.org/3/library/abc.html#abc.ABC)) -> None:
        if trial.state == optuna.trial.TrialState.PRUNED:
            self._consequtive_pruned_count += 1
        else:
            self._consequtive_pruned_count = 0

        if self._consequtive_pruned_count >= self.threshold:
            study.stop()

trainer.hyperparameter_search(
    n_trials=25,
    direction="minimize",
    backend="optuna",
    hp_space=hyperparameter_search_ranges,
    study_name="my_study",
    storage=f"sqlite:///optuna.db",
    callbacks=[StopWhenTrialKeepBeingPrunedCallback(5)],
)

@milos7250
Copy link
Author

One more thing I've realized, the docstring for hyperparameter_search currently only mentions that **kwargs are passed to optuna.create_study(), this would need to be updated for this PR as callbacks are passed to study.optimize(). I am also not sure how this change would affect other hp search backends

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.

2 participants