o
    bi                     @   s\   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 deZdS )	    N)backend)keras_export)MonitorCallback)io_utilsz!keras.callbacks.ReduceLROnPlateauc                       sV   e Zd ZdZ								d fd	d
	Zdd ZdddZdddZdd Z  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.
    val_loss皙?
   r   auto-C6?        c	           
         sV   t  j|||d |dkrtd| || _|| _|| _|| _|| _d| _d| _	d S )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__ \/home/ubuntu/.local/lib/python3.10/site-packages/keras/src/callbacks/reduce_lr_on_plateau.pyr   0   s   
zReduceLROnPlateau.__init__c                 C   s   d| _ d| _dS )z)Resets wait counter and cooldown counter.r   N)r   r   r   r   r   r   _resetK   s   
zReduceLROnPlateau._resetNc                 C   s   |    d S N)r!   )r   logsr   r   r   on_train_beginP   s   z ReduceLROnPlateau.on_train_beginc              	   C   sZ  | j d u r	|   |pi }tt| jjj|d< || j	}|d u r;t
jd| j	 ddt|  ddd d S |  rI|  jd8  _d	| _| || jrX|| _d	| _d S |  s|  jd7  _| j| jkrtt| jjj}|t| jkr|| j }t|| j}|| jj_| jd	krtd
|d  d| d | j| _d	| _d S d S d S d S )Nlearning_ratez2Learning rate reduction is conditioned on metric `z1` which is not available. Available metrics are: ,.   )
stacklevel   r   z
Epoch z.: ReduceLROnPlateau reducing learning rate to )
monitor_op_set_monitor_opfloatr   convert_to_numpymodel	optimizerr%   getr   warningswarnjoinlistkeysin_cooldownr   r   _is_improvementbestr   npfloat32r   r   maxr   r   	print_msgr   )r   epochr#   currentold_lrnew_lrr   r   r   on_epoch_endS   sZ   






zReduceLROnPlateau.on_epoch_endc                 C   s
   | j dkS )Nr   )r   r    r   r   r   r7      s   
zReduceLROnPlateau.in_cooldown)r   r   r	   r   r
   r   r   r   r"   )
__name__
__module____qualname____doc__r   r!   r$   rB   r7   __classcell__r   r   r   r   r      s    %

.r   )r2   numpyr:   	keras.srcr   keras.src.api_exportr   $keras.src.callbacks.monitor_callbackr   keras.src.utilsr   r   r   r   r   r   <module>   s    