U |Ph@sddlmZddlZddlmZddlmZddlmZddl m Z ddl m Z m Z erjddlmZmZn(e d e je d \ZZe d e je d \ZZGd d d ZdS)) annotationsN bisect_right)Callable) TYPE_CHECKING) IgniteInfo) min_versionoptional_import)EngineEventsz ignite.enginer r c@seZdZdZd$ddddddd d d Zd d ZdddddZdddddZedddddddddZ edddddddZ eddddddddZ eddd ddd!d"d#Z dS)%ParamSchedulerHandlera^ General purpose scheduler for parameters values. By default it can schedule in a linear, exponential, step or multistep function. One can also pass Callables to have customized scheduling logic. Args: parameter_setter (Callable): Function that sets the required parameter value_calculator (Union[str,Callable]): Either a string ('linear', 'exponential', 'step' or 'multistep') or Callable for custom logic. vc_kwargs (Dict): Dictionary that stores the required parameters for the value_calculator. epoch_level (bool): Whether the step is based on epoch or iteration. Defaults to False. name (Optional[str]): Identifier of logging.logger to use, if None, defaulting to ``engine.logger``. event (Optional[str]): Event to which the handler attaches. Defaults to Events.ITERATION_COMPLETED. FNrzstr | Callabledictboolz str | None)parameter_settervalue_calculator vc_kwargs epoch_levelnameeventcCsb||_|dk r|ntj|_|j|j|j|jd|_||_ ||_ |j |d|_ t ||_||_dS)N)linear exponentialstepZ multistep)r)rr ITERATION_COMPLETEDr_linear _exponential_step _multistep _calculators_parameter_setter _vc_kwargs_get_value_calculator_value_calculatorlogging getLoggerlogger_name)selfrrrrrrr'W/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/handlers/parameter_scheduler.py__init__,s  zParamSchedulerHandler.__init__cCs>t|tr|j|St|r |Stdt|jddS)Nz.value_calculator must be either a string from z or a Callable.) isinstancestrrcallable ValueErrorlistkeys)r&rr'r'r(r Fs  z+ParamSchedulerHandler._get_value_calculatorr None)enginereturncCs@|jr|jj|jd<n|jj|jd<|jf|j}||dS)N current_step)rstateepochr iterationr!r)r&r1 new_valuer'r'r(__call__Os zParamSchedulerHandler.__call__cCs$|jdkr|j|_||j|dS)zT Args: engine: Ignite Engine that is used for training. N)r%r$add_event_handlerr)r&r1r'r'r(attachXs zParamSchedulerHandler.attachfloatint) initial_value step_constantstep_max_value max_valuer3r2cCs@||krd}n*||kr ||}n||||||}||S)a| Keeps the parameter value to zero until step_zero steps passed and then linearly increases it to 1 until an additional step_one steps passed. Continues the trend until it reaches max_value. Args: initial_value (float): Starting value of the parameter. step_constant (int): Step index until parameter's value is kept constant. step_max_value (int): Step index at which parameter's value becomes max_value. max_value (float): Max parameter value. current_step (int): Current step index. Returns: float: new parameter value gr')r=r>r?r@r3deltar'r'r(ras  zParamSchedulerHandler._linear)r=gammar3r2cCs |||S)a Decays the parameter value by gamma every step. Based on the closed form of ExponentialLR from Pytorch: https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.ExponentialLR.html. Args: initial_value (float): Starting value of the parameter. gamma (float): Multiplicative factor of parameter value decay. current_step (int): Current step index. Returns: float: new parameter value r')r=rBr3r'r'r(r|sz"ParamSchedulerHandler._exponential)r=rB step_sizer3r2cCs||||S)a  Decays the parameter value by gamma every step_size. Based on StepLR from Pytorch: https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.StepLR.html. Args: initial_value (float): Starting value of the parameter. gamma (float): Multiplicative factor of parameter value decay. step_size (int): Period of parameter value decay. current_step (int): Current step index. Returns float: new parameter value r')r=rBrCr3r'r'r(rszParamSchedulerHandler._stepz list[int])r=rB milestonesr3r2cCs||t||S)aO Decays the parameter value by gamma once the number of steps reaches one of the milestones. Based on MultiStepLR from Pytorch. https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.MultiStepLR.html. Args: initial_value (float): Starting value of the parameter. gamma (float): Multiplicative factor of parameter value decay. milestones (List[int]): List of step indices. Must be increasing. current_step (int): Current step index. Returns: float: new parameter value r)r=rBrDr3r'r'r(rsz ParamSchedulerHandler._multistep)FNN) __name__ __module__ __qualname____doc__r)r r8r: staticmethodrrrrr'r'r'r(r s    r ) __future__rr"bisectrcollections.abcrtypingr monai.configr monai.utilsrr ignite.enginer r OPT_IMPORT_VERSION_r r'r'r'r( s