
    ij&                     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)MonitorCallback)io_utilsz!keras.callbacks.ReduceLROnPlateauc                   X     e Zd ZdZ	 	 	 	 	 	 	 	 d fd	Zd Zd	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| _	        y )N)	min_deltag      ?zDReduceLROnPlateau does not support a factor >= 1.0. Received factor=r   )
super__init__
ValueErrorfactormin_lrpatienceverbosecooldowncooldown_counterwait)selfmonitorr   r   r   moder
   r   r   kwargs	__class__s             }/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/callbacks/reduce_lr_on_plateau.pyr   zReduceLROnPlateau.__init__0   sp     	$)<S=##)(, 
    !	    c                 .    d| _         d| _        d| _        y)z6Resets wait counter, cooldown counter, and best value.Nr   )bestr   r   r   s    r   _resetzReduceLROnPlateau._resetK   s    	 !	r   c           	      f   t        t        j                  |j                              }|t	        j
                  | j                        kD  rg|| j                  z  }t        || j                        }||_        | j                  dkD  r+|rd| nd}t        j                  d|dz    d| d| d       y	y
)Nr     z
Epoch    z: ReduceLROnPlateau reducingz learning rate to .TF)floatr   convert_to_numpylearning_ratenpfloat32r   r   maxr   r   	print_msg)r   	optimizerepochnameold_lrnew_lrname_strs          r   _reduce_optimizer_lrz&ReduceLROnPlateau._reduce_optimizer_lrQ   s    w//	0G0GHIBJJt{{++dkk)F-F&,I#||a)-Qtf:2""uqyk)Ej 26(!= r   c                 $    | j                          y N)r   )r   logss     r   on_train_beginz ReduceLROnPlateau.on_train_begin`   s    r   c                 P   | j                   | j                          |xs i }t        | j                  j                  d      rjt        | j                  j                  j                        D ]=  \  }}t        t        j                  |j                              |d|j                   <   ? n?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*                  | j0                  k\  rd}t        | j                  j                  d      rSt        | j                  j                  j                        D ]&  \  }}| j3                  |||j                         s%d}( n(| j3                  | j                  j                  |      rd}|r| j4                  | _        d| _        y y y y )N
optimizerslearning_rate_r'   z2Learning rate reduction is conditioned on metric `z1` which is not available. Available metrics are: ,r$      )
stacklevelr#   r   FT)
monitor_op_set_monitor_ophasattrmodelr,   	enumerater8   r%   r   r&   r'   r.   getr   warningswarnjoinlistkeysin_cooldownr   r   _is_improvementr   r   r2   r   )r   r-   r5   idxoptcurrentreduceds          r   on_epoch_endzReduceLROnPlateau.on_epoch_endc   s<   ??"  "zr4::''6%djj&:&:&E&EF S49,,S->->?5~chhZ01
 %*(()=)=)K)KL%D! ((4<<(?MMLL> "diik!234A7 	 !%%*%	##GTYY7#		%%'		Q	99-#Gtzz33\B(1 JJ00;;) /HC  $88 #Usxxj  +//  44 JJ00% '+G04-$%	 ! . (r   c                      | j                   dkD  S )Nr   )r   r   s    r   rH   zReduceLROnPlateau.in_cooldown   s    $$q((r   )val_lossg?
   r   autog-C6?r   g        )r"   r4   )__name__
__module____qualname____doc__r   r   r2   r6   rN   rH   __classcell__)r   s   @r   r   r      sB    !J 63&j)r   r   )rC   numpyr(   	keras.srcr   keras.src.api_exportr   $keras.src.callbacks.monitor_callbackr   keras.src.utilsr   r    r   r   <module>r^      s;       - @ $ 12M) M) 3M)r   