
    2Vh                     h    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.LearningRateSchedulerc                   4     e Zd ZdZd fd	ZddZddZ xZS )LearningRateSchedulera#  Learning rate scheduler.

    At the beginning of every epoch, this callback gets the updated learning
    rate value from `schedule` function provided at `__init__`, with the current
    epoch and current learning rate, and applies the updated learning rate on
    the optimizer.

    Args:
        schedule: A function that takes an epoch index (integer, indexed from 0)
            and current learning rate (float) as inputs and returns a new
            learning rate as output (float).
        verbose: Integer. 0: quiet, 1: log update messages.

    Example:

    >>> # This function keeps the initial learning rate for the first ten epochs
    >>> # and decreases it exponentially after that.
    >>> def scheduler(epoch, lr):
    ...     if epoch < 10:
    ...         return lr
    ...     else:
    ...         return lr * ops.exp(-0.1)
    >>>
    >>> model = keras.models.Sequential([keras.layers.Dense(10)])
    >>> model.compile(keras.optimizers.SGD(), loss='mse')
    >>> round(model.optimizer.learning_rate, 5)
    0.01

    >>> callback = keras.callbacks.LearningRateScheduler(scheduler)
    >>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5),
    ...                     epochs=15, callbacks=[callback], verbose=0)
    >>> round(model.optimizer.learning_rate, 5)
    0.00607

    c                 >    t         |           || _        || _        y N)super__init__scheduleverbose)selfr   r   	__class__s      [/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/callbacks/learning_rate_scheduler.pyr   zLearningRateScheduler.__init__/   s         c                 F   t        | j                  j                  d      st        d      	 t	        t        j                  | j                  j                  j                              }| j                  ||      }t        |t        t        j                  t        j                  f      st        d|       || j                  j                  _        | j                  dkD  r t        j                   d|dz    d| d       y y # t        $ r | j                  |      }Y w xY w)	Nlearning_ratez0Optimizer must have a "learning_rate" attribute.z>The output of the `schedule` function should be a float. Got: r   z
Epoch    z1: LearningRateScheduler setting learning rate to .)hasattrmodel	optimizer
ValueErrorfloatr   convert_to_numpyr   r   	TypeError
isinstancenpfloat32float64r   r   	print_msg)r   epochlogsr   s       r   on_epoch_beginz$LearningRateScheduler.on_epoch_begin4   s   tzz++_=OPP	1!(()=)=)K)KLM !MM%?M -%RZZ)HI%( 
 .;

*<<!519+ &(/,   	1 MM%0M	1s   AD D D c                     |xs i }t        t        j                  | j                  j                  j
                              |d<   y )Nr   )r   r   r   r   r   r   )r   r#   r$   s      r   on_epoch_endz"LearningRateScheduler.on_epoch_endM   s8    zr %$$TZZ%9%9%G%GH!
_r   )r   r
   )__name__
__module____qualname____doc__r   r%   r'   __classcell__)r   s   @r   r   r   	   s    "H
2
r   r   )numpyr   	keras.srcr   keras.src.api_exportr   keras.src.callbacks.callbackr   keras.src.utilsr   r    r   r   <module>r3      s8      - 1 $ 56G
H G
 7G
r   