o
    zi                     @   sl   d Z ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
mZ ddlmZ ddlmZ G d	d
 d
eZdS )zD
LearningRateFinder
==================

Finds optimal learning rate
    )Optional)overrideN)Callback)_lr_find	_LRFinder)_TunerExitException)isolate_rngc                   @   sj   e Zd ZdZdZ								dd
ededededee de	deddfddZ
dddZedddZdS )LearningRateFindera}  The ``LearningRateFinder`` callback enables the user to do a range test of good initial learning rates, to
    reduce the amount of guesswork in picking a good starting learning rate.

    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.

    Args:
        min_lr: Minimum learning rate to investigate
        max_lr: Maximum learning rate to investigate
        num_training_steps: Number of learning rates to test
        mode: Search strategy to update learning rate after each batch:

            - ``'exponential'`` (default): Increases the learning rate exponentially.
            - ``'linear'``: Increases the learning rate linearly.

        early_stop_threshold: Threshold for stopping the search. If the
            loss at any point is larger than early_stop_threshold*best_loss
            then the search is stopped. To disable, set to None.
        update_attr: Whether to update the learning rate attribute or not.
        attr_name: Name of the attribute which stores the learning rate. The names 'learning_rate' or 'lr' get
            automatically detected. Otherwise, set the name here.

    Example::

        # Customize LearningRateFinder callback to run at different epochs.
        # This feature is useful while fine-tuning models.
        from pytorch_lightning.callbacks import LearningRateFinder


        class FineTuneLearningRateFinder(LearningRateFinder):
            def __init__(self, milestones, *args, **kwargs):
                super().__init__(*args, **kwargs)
                self.milestones = milestones

            def on_fit_start(self, *args, **kwargs):
                return

            def on_train_epoch_start(self, trainer, pl_module):
                if trainer.current_epoch in self.milestones or trainer.current_epoch == 0:
                    self.lr_find(trainer, pl_module)


        trainer = Trainer(callbacks=[FineTuneLearningRateFinder(milestones=(5, 10))])
        trainer.fit(...)

    Raises:
        MisconfigurationException:
            If learning rate/lr in ``model`` or ``model.hparams`` isn't overridden, or if you are using more than
            one optimizer.

    )linearexponential:0yE>   d   r         @T min_lrmax_lrnum_training_stepsmodeearly_stop_thresholdupdate_attr	attr_namereturnNc                 C   s\   |  }|| jvrtd| j || _|| _|| _|| _|| _|| _|| _	d| _
d | _d S )Nz`mode` should be either of F)lowerSUPPORTED_MODES
ValueError_min_lr_max_lr_num_training_steps_mode_early_stop_threshold_update_attr
_attr_name_early_exit	lr_finder)selfr   r   r   r   r   r   r    r&   Y/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/callbacks/lr_finder.py__init__V   s   


zLearningRateFinder.__init__trainer
pl.Trainer	pl_modulepl.LightningModulec                 C   s`   t   t||| j| j| j| j| j| j| jd	| _	W d    n1 s#w   Y  | j
r.t d S )N)r   r   num_trainingr   r   r   r   )r   r   r   r   r   r   r    r!   r"   
optimal_lrr#   r   r%   r)   r+   r&   r&   r'   lr_findo   s    
zLearningRateFinder.lr_findc                 C   s   |  || d S )N)r0   r/   r&   r&   r'   on_fit_start   s   zLearningRateFinder.on_fit_start)r   r   r   r   r   Tr   )r)   r*   r+   r,   r   N)__name__
__module____qualname____doc__r   floatintstrr   boolr(   r0   r   r1   r&   r&   r&   r'   r	       s<    3	

r	   )r5   typingr   typing_extensionsr   pytorch_lightningpl$pytorch_lightning.callbacks.callbackr   !pytorch_lightning.tuner.lr_finderr   r   &pytorch_lightning.utilities.exceptionsr    pytorch_lightning.utilities.seedr   r	   r&   r&   r&   r'   <module>   s   