optimizer that implements the adadelta algorithm adadelta optimization is a stochastic gradient descent method that is based on adaptive learning rate per dimension to address two drawbacks the continual decay of learning rates throughout training the need for a manually selected global learning rate adadelta is a more robust extension of adagrad that adapts learning rates based on a moving window of gradient updates instead of accumulating all past gradients this way adadelta continues learning even when many updates have been done compared to adagrad in the original version of adadelta you don t have to set an initial learning rate in this version the initial learning rate can be set as in most other keras optimizers args learningrate a float a keras optimizers schedules learningrateschedule instance or a callable that takes no arguments and returns the actual value to use the learning rate defaults to 0 001 note that adadelta tends to benefit from higher initial learning rate values compared to other optimizers to match the exact form in the original paper use 1 0 rho a floating point value the decay rate defaults to 0 95 epsilon small floating point value for maintaining numerical stability baseoptimizerkeywordargs reference zeiler 2012http arxiv orgabs1212 5701 update step given gradient and the associated model variable lr ops castlearningrate variable dtype grad ops castgrad variable dtype rho self rho accumulatedgrad self accumulatedgrads self getvariableindexvariable accumulateddeltavar self accumulateddeltavars self getvariableindexvariable def rmsx return ops sqrtops addx self epsilon self assign accumulatedgrad ops add rho accumulatedgrad ops multiply1 rho ops squaregrad deltavar ops negative ops divide ops multiplyrmsaccumulateddeltavar grad rmsaccumulatedgrad self assign accumulateddeltavar ops add ops multiplyrho accumulateddeltavar ops multiply1 rho ops squaredeltavar self assignaddvariable ops multiplylr deltavar def getconfigself config super getconfig config update rho self rho epsilon self epsilon return config adadelta doc adadelta doc replace baseoptimizerkeywordargs optimizer baseoptimizerkeywordargs optimizer that implements the adadelta algorithm adadelta optimization is a stochastic gradient descent method that is based on adaptive learning rate per dimension to address two drawbacks the continual decay of learning rates throughout training the need for a manually selected global learning rate adadelta is a more robust extension of adagrad that adapts learning rates based on a moving window of gradient updates instead of accumulating all past gradients this way adadelta continues learning even when many updates have been done compared to adagrad in the original version of adadelta you don t have to set an initial learning rate in this version the initial learning rate can be set as in most other keras optimizers args learning_rate a float a keras optimizers schedules learningrateschedule instance or a callable that takes no arguments and returns the actual value to use the learning rate defaults to 0 001 note that adadelta tends to benefit from higher initial learning rate values compared to other optimizers to match the exact form in the original paper use 1 0 rho a floating point value the decay rate defaults to 0 95 epsilon small floating point value for maintaining numerical stability base_optimizer_keyword_args reference zeiler 2012 http arxiv org abs 1212 5701 update step given gradient and the associated model variable
from keras import ops
from keras.api_export import keras_export
from keras.optimizers import optimizer


@keras_export(["keras.optimizers.Adadelta"])
class Adadelta(optimizer.Optimizer):
    

    def __init__(
        self,
        learning_rate=0.001,
        rho=0.95,
        epsilon=1e-7,
        weight_decay=None,
        clipnorm=None,
        clipvalue=None,
        global_clipnorm=None,
        use_ema=False,
        ema_momentum=0.99,
        ema_overwrite_frequency=None,
        name="adadelta",
        **kwargs,
    ):
        super().__init__(
            learning_rate=learning_rate,
            weight_decay=weight_decay,
            clipnorm=clipnorm,
            clipvalue=clipvalue,
            global_clipnorm=global_clipnorm,
            use_ema=use_ema,
            ema_momentum=ema_momentum,
            ema_overwrite_frequency=ema_overwrite_frequency,
            name=name,
            **kwargs,
        )
        self.rho = rho
        self.epsilon = epsilon

    def build(self, var_list):
        if self.built:
            return
        super().build(var_list)
        self._accumulated_grads = []
        self._accumulated_delta_vars = []
        for var in var_list:
            self._accumulated_grads.append(
                self.add_variable_from_reference(var, "accumulated_grad")
            )
            self._accumulated_delta_vars.append(
                self.add_variable_from_reference(var, "accumulated_delta_var")
            )

    def update_step(self, grad, variable, learning_rate):
        
        lr = ops.cast(learning_rate, variable.dtype)
        grad = ops.cast(grad, variable.dtype)

        rho = self.rho
        accumulated_grad = self._accumulated_grads[
            self._get_variable_index(variable)
        ]
        accumulated_delta_var = self._accumulated_delta_vars[
            self._get_variable_index(variable)
        ]

        def rms(x):
            return ops.sqrt(ops.add(x, self.epsilon))

        self.assign(
            accumulated_grad,
            ops.add(
                rho * accumulated_grad, ops.multiply(1 - rho, ops.square(grad))
            ),
        )
        delta_var = ops.negative(
            ops.divide(
                ops.multiply(rms(accumulated_delta_var), grad),
                rms(accumulated_grad),
            )
        )
        self.assign(
            accumulated_delta_var,
            ops.add(
                ops.multiply(rho, accumulated_delta_var),
                ops.multiply(1 - rho, ops.square(delta_var)),
            ),
        )
        self.assign_add(variable, ops.multiply(lr, delta_var))

    def get_config(self):
        config = super().get_config()

        config.update(
            {
                "rho": self.rho,
                "epsilon": self.epsilon,
            }
        )
        return config


Adadelta.__doc__ = Adadelta.__doc__.replace(
    "{{base_optimizer_keyword_args}}", optimizer.base_optimizer_keyword_args
)