Create a new trial from which hyperparameters can be suggested. This method is part of an alternative to :func:`~optuna.study.Study.optimize` that allows controlling the lifetime of a trial outside the scope of ``func``. Each call to this method should be followed by a call
(self, fixed_distributions: dict[str, BaseDistribution] | None = None)
| 526 | ) |
| 527 | |
| 528 | def ask(self, fixed_distributions: dict[str, BaseDistribution] | None = None) -> Trial: |
| 529 | """Create a new trial from which hyperparameters can be suggested. |
| 530 | |
| 531 | This method is part of an alternative to :func:`~optuna.study.Study.optimize` that allows |
| 532 | controlling the lifetime of a trial outside the scope of ``func``. Each call to this |
| 533 | method should be followed by a call to :func:`~optuna.study.Study.tell` to finish the |
| 534 | created trial. |
| 535 | |
| 536 | .. seealso:: |
| 537 | |
| 538 | The :ref:`ask_and_tell` tutorial provides use-cases with examples. |
| 539 | |
| 540 | Example: |
| 541 | |
| 542 | Getting the trial object with the :func:`~optuna.study.Study.ask` method. |
| 543 | |
| 544 | .. testcode:: |
| 545 | |
| 546 | import optuna |
| 547 | |
| 548 | |
| 549 | study = optuna.create_study() |
| 550 | |
| 551 | trial = study.ask() |
| 552 | |
| 553 | x = trial.suggest_float("x", -1, 1) |
| 554 | |
| 555 | study.tell(trial, x**2) |
| 556 | |
| 557 | Example: |
| 558 | |
| 559 | Passing previously defined distributions to the :func:`~optuna.study.Study.ask` |
| 560 | method. |
| 561 | |
| 562 | .. testcode:: |
| 563 | |
| 564 | import optuna |
| 565 | |
| 566 | |
| 567 | study = optuna.create_study() |
| 568 | |
| 569 | distributions = { |
| 570 | "optimizer": optuna.distributions.CategoricalDistribution(["adam", "sgd"]), |
| 571 | "lr": optuna.distributions.FloatDistribution(0.0001, 0.1, log=True), |
| 572 | } |
| 573 | |
| 574 | # You can pass the distributions previously defined. |
| 575 | trial = study.ask(fixed_distributions=distributions) |
| 576 | |
| 577 | # `optimizer` and `lr` are already suggested and accessible with `trial.params`. |
| 578 | assert "optimizer" in trial.params |
| 579 | assert "lr" in trial.params |
| 580 | |
| 581 | Args: |
| 582 | fixed_distributions: |
| 583 | A dictionary containing the parameter names and parameter's distributions. Each |
| 584 | parameter in this dictionary is automatically suggested for the returned trial, |
| 585 | even when the suggest method is not explicitly invoked by the user. If this |