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 schedulerepoch lr if epoch 10 return lr else return lr ops exp0 1 model keras models sequentialkeras layers dense10 model compilekeras optimizers sgd loss mse roundmodel optimizer learningrate 5 0 01 callback keras callbacks learningrateschedulerscheduler history model fitnp arange100 reshape5 20 np zeros5 epochs15 callbackscallback verbose0 roundmodel optimizer learningrate 5 0 00607 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 new api support for old api for backward compatibility
import numpy as np

from keras import backend
from keras.api_export import keras_export
from keras.callbacks.callback import Callback
from keras.utils import io_utils


@keras_export("keras.callbacks.LearningRateScheduler")
class LearningRateScheduler(Callback):
    

    def __init__(self, schedule, verbose=0):
        super().__init__()
        self.schedule = schedule
        self.verbose = verbose

    def on_epoch_begin(self, epoch, logs=None):
        if not hasattr(self.model.optimizer, "learning_rate"):
            raise ValueError('Optimizer must have a "learning_rate" attribute.')

        try:  
            learning_rate = float(
                backend.convert_to_numpy(self.model.optimizer.learning_rate)
            )
            learning_rate = self.schedule(epoch, learning_rate)
        except TypeError:  
            learning_rate = self.schedule(epoch)

        if not isinstance(learning_rate, (float, np.float32, np.float64)):
            raise ValueError(
                "The output of the `schedule` function should be a float. "
                f"Got: {learning_rate}"
            )

        self.model.optimizer.learning_rate = learning_rate
        if self.verbose > 0:
            io_utils.print_msg(
                f"\nEpoch {epoch + 1}: LearningRateScheduler setting learning "
                f"rate to {learning_rate}."
            )

    def on_epoch_end(self, epoch, logs=None):
        logs = logs or {}
        logs["learning_rate"] = float(
            backend.convert_to_numpy(self.model.optimizer.learning_rate)
        )