L i)ddlmZddlZddlZddlmZddlmZddlm Z GddeZ d dZ dd Z dd Z dd Z dd Zy)) annotationsN) BasePruner)StudyDirection) TrialStatec<eZdZdZ d ddZddZy)SuccessiveHalvingPrunera|Pruner using Asynchronous Successive Halving Algorithm. `Successive Halving `__ is a bandit-based algorithm to identify the best one among multiple configurations. This class implements an asynchronous version of Successive Halving. Please refer to the paper of `Asynchronous Successive Halving `__ for detailed descriptions. Note that, this class does not take care of the parameter for the maximum resource, referred to as :math:`R` in the paper. The maximum resource allocated to a trial is typically limited inside the objective function (e.g., ``step`` number in `simple_pruning.py `__, ``EPOCH`` number in `chainer_integration.py `__). .. seealso:: Please refer to :meth:`~optuna.trial.Trial.report`. Example: We minimize an objective function with ``SuccessiveHalvingPruner``. .. testcode:: import numpy as np from sklearn.datasets import load_iris from sklearn.linear_model import SGDClassifier from sklearn.model_selection import train_test_split import optuna X, y = load_iris(return_X_y=True) X_train, X_valid, y_train, y_valid = train_test_split(X, y) classes = np.unique(y) def objective(trial): alpha = trial.suggest_float("alpha", 0.0, 1.0) clf = SGDClassifier(alpha=alpha) n_train_iter = 100 for step in range(n_train_iter): clf.partial_fit(X_train, y_train, classes=classes) intermediate_value = clf.score(X_valid, y_valid) trial.report(intermediate_value, step) if trial.should_prune(): raise optuna.TrialPruned() return clf.score(X_valid, y_valid) study = optuna.create_study( direction="maximize", pruner=optuna.pruners.SuccessiveHalvingPruner() ) study.optimize(objective, n_trials=20) Args: min_resource: A parameter for specifying the minimum resource allocated to a trial (in the `paper `__ this parameter is referred to as :math:`r`). This parameter defaults to 'auto' where the value is determined based on a heuristic that looks at the number of required steps for the first trial to complete. A trial is never pruned until it executes :math:`\mathsf{min}\_\mathsf{resource} \times \mathsf{reduction}\_\mathsf{factor}^{ \mathsf{min}\_\mathsf{early}\_\mathsf{stopping}\_\mathsf{rate}}` steps (i.e., the completion point of the first rung). When the trial completes the first rung, it will be promoted to the next rung only if the value of the trial is placed in the top :math:`{1 \over \mathsf{reduction}\_\mathsf{factor}}` fraction of the all trials that already have reached the point (otherwise it will be pruned there). If the trial won the competition, it runs until the next completion point (i.e., :math:`\mathsf{min}\_\mathsf{resource} \times \mathsf{reduction}\_\mathsf{factor}^{ (\mathsf{min}\_\mathsf{early}\_\mathsf{stopping}\_\mathsf{rate} + \mathsf{rung})}` steps) and repeats the same procedure. .. note:: If the step of the last intermediate value may change with each trial, please manually specify the minimum possible step to ``min_resource``. reduction_factor: A parameter for specifying reduction factor of promotable trials (in the `paper `__ this parameter is referred to as :math:`\eta`). At the completion point of each rung, about :math:`{1 \over \mathsf{reduction}\_\mathsf{factor}}` trials will be promoted. min_early_stopping_rate: A parameter for specifying the minimum early-stopping rate (in the `paper `__ this parameter is referred to as :math:`s`). bootstrap_count: Minimum number of trials that need to complete a rung before any trial is considered for promotion into the next rung. c(t|tr|dk7rtdj|t|tr|dkrtdj||dkrtdj||dkrtdj||dkrtd j||dkDr|dk(rtd j|d|_t|tr||_||_||_||_y) NautozSThe value of `min_resource` is {}, but must be either `min_resource` >= 1 or 'auto'zSThe value of `min_resource` is {}, but must be either `min_resource >= 1` or 'auto'zJThe value of `reduction_factor` is {}, but must be `reduction_factor >= 2`rzXThe value of `min_early_stopping_rate` is {}, but must be `min_early_stopping_rate >= 0`zHThe value of `bootstrap_count` is {}, but must be `bootstrap_count >= 0`z_bootstrap_count > 0 and min_resource == 'auto' are mutually incompatible, bootstrap_count is {}) isinstancestr ValueErrorformatint _min_resource_reduction_factor_min_early_stopping_rate_bootstrap_count)self min_resourcereduction_factormin_early_stopping_ratebootstrap_counts h/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/pruners/_successive_halving.py__init__z SuccessiveHalvingPruner.__init__ss8 lC (\V-CCCI6,CW  lC (\A-=CCI6,CW  a 66!!)>"--u-=F%;F%C"%%- %%1 11"&"4"4&&4+H+H4+OP# ))zz% ~))5)9*40H NN 0 0(E R-feXFI9~!6!664&&   AIDQrN)r rr) rz str | intrrrrrrreturnNone)r/z'optuna.study.Study'r0'optuna.trial.FrozenTrial'r:bool)__name__ __module__ __qualname____doc__rr8rrrr sReR#) !'( 000000"% 00  00  00d1rrc|Dcgc]9}|jtjk(s!|j.|j;}}|syt |}t |dzdScc}w)Ndr )staterCOMPLETEr max)r4tn_stepsr s rr$r$sd#qww*2E2E'E!++Ja G G I yC ##s"A" A"A"crd}t||jvr|dz }t||jvr|S)Nrr )r' system_attrs)r0r2s rr!r!s= D d #u'9'9 9   d #u'9'9 9 Krc$dj|S)Nzcompleted_rung_{})r)r2s rr'r's  % %d ++rc|Dcgc] }||jvs|j|"}}|j||Scc}w)N)rKappend)r4r3r6rHcompeting_valuess rr+r+sJ;A_QHPQP^P^D^x0__E" `s??ct||zdz }|dk(rd}|j|tjk(r |||dz k\S|||kS)Nr r)r,sortrMAXIMIZE)r3rOrstudy_directionpromotable_idxs rr-r-sm *+/??1DN.111(>A+=)>??? $^4 44r)r4 list['optuna.trial.FrozenTrial']r:z int | None)r0r<r:r)r2rr:r)r4rVr3floatr6rr: list[float]) r3rWrOrXrrrTrr:r=) __future__rr%optunaoptuna.pruners._baseroptuna.study._study_directionroptuna.trial._staterrr$r!r'r+r-rBrrr^s" +8*KjK\ $, ,5:FI5 5!55$ 5  5r