
    2Vh                     p    d dl Z d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
  ed       G d de             Zy)	    N)backend)keras_export)Callback)io_utilsz!keras.callbacks.ReduceLROnPlateauc                   P     e Zd ZdZ	 	 	 	 	 	 	 	 d fd	Zd ZddZddZd Z xZ	S )	ReduceLROnPlateaua4  Reduce learning rate when a metric has stopped improving.

    Models often benefit from reducing the learning rate by a factor
    of 2-10 once learning stagnates. This callback monitors a
    quantity and if no improvement is seen for a 'patience' number
    of epochs, the learning rate is reduced.

    Example:

    ```python
    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
                                  patience=5, min_lr=0.001)
    model.fit(x_train, y_train, callbacks=[reduce_lr])
    ```

    Args:
        monitor: String. Quantity to be monitored.
        factor: Float. Factor by which the learning rate will be reduced.
            `new_lr = lr * factor`.
        patience: Integer. Number of epochs with no improvement after which
            learning rate will be reduced.
        verbose: Integer. 0: quiet, 1: update messages.
        mode: String. One of `{'auto', 'min', 'max'}`. In `'min'` mode,
            the learning rate will be reduced when the
            quantity monitored has stopped decreasing; in `'max'` mode it will
            be reduced when the quantity monitored has stopped increasing; in
            `'auto'` mode, the direction is automatically inferred from the name
            of the monitored quantity.
        min_delta: Float. Threshold for measuring the new optimum, to only focus
            on significant changes.
        cooldown: Integer. Number of epochs to wait before resuming normal
            operation after the learning rate has been reduced.
        min_lr: Float. Lower bound on the learning rate.
    c	                    t         
|           || _        |dk\  rt        d|       || _        || _        || _        || _        || _        || _	        d| _
        d| _        d| _        || _        d | _        | j                          y )Ng      ?zDReduceLROnPlateau does not support a factor >= 1.0. Received factor=r   )super__init__monitor
ValueErrorfactormin_lr	min_deltapatienceverbosecooldowncooldown_counterwaitbestmode
monitor_op_reset)selfr   r   r   r   r   r   r   r   kwargs	__class__s             X/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/callbacks/reduce_lr_on_plateau.pyr   zReduceLROnPlateau.__init__0   s     	S=##)(, 
 "   !			    c                 n     j                   dvr,t        j                  d j                    dd       d _          j                   dk(  s j                   dk(  r.d j                  vr  fd	 _        t
        j                   _        n  fd
 _        t
        j                    _        d _        d _	        y)z)Resets wait counter and cooldown counter.>   maxminautozLearning rate reduction mode z# is unknown, fallback to auto mode.   
stacklevelr"   r!   accc                 J    t        j                  | |j                  z
        S N)nplessr   abr   s     r   <lambda>z*ReduceLROnPlateau._reset.<locals>.<lambda>^   s    2771a$..6H+I r   c                 J    t        j                  | |j                  z         S r(   )r)   greaterr   r+   s     r   r.   z*ReduceLROnPlateau._reset.<locals>.<lambda>a   s    2::aT^^9K+L r   r   N)
r   warningswarnr   r   r)   infr   r   r   r   s   `r   r   zReduceLROnPlateau._resetR   s    9922MM/		{ ;) )
 DI99IIE$=IDODILDODI !	r   c                 $    | j                          y r(   )r   )r   logss     r   on_train_beginz ReduceLROnPlateau.on_train_beginf   s    r   c                 `   |xs i }t        t        j                  | j                  j                  j
                              |d<   |j                  | j                        }|Ot        j                  d| j                   ddj                  t        |j                                      dd       y | j                         r| xj                  dz  c_        d	| _        | j!                  || j"                        r|| _        d	| _        y | j                         s| xj                  dz  c_        | j                  | j$                  k\  rt        t        j                  | j                  j                  j
                              }|t'        j(                  | j*                        kD  r|| j,                  z  }t/        || j*                        }|| j                  j                  _        | j0                  d	kD  rt3        j4                  d
|dz    d| d       | j6                  | _        d	| _        y y y y )Nlearning_ratez2Learning rate reduction is conditioned on metric `z1` which is not available. Available metrics are: ,.r#   r$      r   z
Epoch z.: ReduceLROnPlateau reducing learning rate to )floatr   convert_to_numpymodel	optimizerr9   getr   r1   r2   joinlistkeysin_cooldownr   r   r   r   r   r)   float32r   r   r    r   r   	print_msgr   )r   epochr6   currentold_lrnew_lrs         r   on_epoch_endzReduceLROnPlateau.on_epoch_endi   s   zr %$$TZZ%9%9%G%GH!
_ ((4<<(?MMLL> "diik!234A7 	 !%%*%	w		2#		%%'		Q	99-"00 JJ00>>F
 

4;; 77!'$++!5!$VT[[!9=C

,,:<<!+$.."*519+ 644:81!>
 15-$%	 8 . (r   c                      | j                   dkD  S )Nr   )r   r4   s    r   rE   zReduceLROnPlateau.in_cooldown   s    $$q((r   )val_lossg?
   r   r"   g-C6?r   g        r(   )
__name__
__module____qualname____doc__r   r   r7   rL   rE   __classcell__)r   s   @r   r   r      s>    !J  D()&V)r   r   )r1   numpyr)   	keras.srcr   keras.src.api_exportr   keras.src.callbacks.callbackr   keras.src.utilsr   r    r   r   <module>r[      s;       - 1 $ 12I) I) 3I)r   