
    |h                         d Z ddlmc mZ ddlmZ ddlmZ ddl	m
Z
  e        e
dddg 	       G d
 dej                                      Zej                   j                  dej                        e_         y)z!RMSprop optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportzkeras.optimizers.RMSpropz%keras.optimizers.experimental.RMSpropz-keras.dtensor.experimental.optimizers.RMSprop)v1c                   Z     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z fdZd Z fdZ xZS )RMSpropaS  Optimizer that implements the RMSprop algorithm.

    The gist of RMSprop is to:

    - Maintain a moving (discounted) average of the square of gradients
    - Divide the gradient by the root of this average

    This implementation of RMSprop uses plain momentum, not Nesterov momentum.

    The centered version additionally maintains a moving average of the
    gradients, and uses that average to estimate the variance.

    Args:
        learning_rate: Initial value for the learning rate:
            either a floating point value,
            or a `tf.keras.optimizers.schedules.LearningRateSchedule` instance.
            Defaults to 0.001.
        rho: float, defaults to 0.9. Discounting factor for the old gradients.
        momentum: float, defaults to 0.0. If not 0.0., the optimizer tracks the
            momentum value, with a decay rate equals to `1 - momentum`.
        epsilon: A small constant for numerical stability. This epsilon is
            "epsilon hat" in the Kingma and Ba paper (in the formula just before
            Section 2.1), not the epsilon in Algorithm 1 of the paper.
            Defaults to `1e-7`.
        centered: Boolean. If `True`, gradients are normalized by the estimated
            variance of the gradient; if False, by the uncentered second moment.
            Setting this to `True` may help with training, but is slightly more
            expensive in terms of computation and memory. Defaults to `False`.
        {{base_optimizer_keyword_args}}

    Usage:

    >>> opt = tf.keras.optimizers.RMSprop(learning_rate=0.1)
    >>> var1 = tf.Variable(10.0)
    >>> loss = lambda: (var1 ** 2) / 2.0  # d(loss) / d(var1) = var1
    >>> opt.minimize(loss, [var1])
    >>> var1.numpy()
    9.683772

    Reference:
        - [Hinton, 2012](http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf) # noqa: E501
    c                     t        |   d||||	|
||||d	| | j                  |      | _        || _        || _        || _        || _        y )N)	weight_decayclipnorm	clipvalueglobal_clipnormuse_emaema_momentumema_overwrite_frequencyjit_compilename )super__init___build_learning_rate_learning_raterhomomentumepsiloncentered)selflearning_rater   r   r   r   r
   r   r   r   r   r   r   r   r   kwargs	__class__s                   ^/var/www/html/test/engine/venv/lib/python3.12/site-packages/tf_keras/src/optimizers/rmsprop.pyr   zRMSprop.__init__M   sm    $ 	 	
%+%$;#	
 	
 #77F      c                    t         |   |       t        | d      r| j                  ry d| _        g | _        |D ]-  }| j                  j                  | j                  |d             / g | _        | j                  dkD  r2|D ]-  }| j                  j                  | j                  |d             / g | _	        | j                  r3|D ]-  }| j                  j                  | j                  |d             / y y )N_builtTvelocityr   r   average_gradient)r   buildhasattrr#   _velocitiesappendadd_variable_from_reference
_momentumsr   _average_gradientsr   )r   var_listvarr   s      r    r&   zRMSprop.buildq   s    h4"t{{ 	C##00jA	
 ==1 &&44S*E
 #%== ''..44S:LM r!   c                 T   t        j                  | j                  |j                        }| j	                  |      }| j
                  | j                  |      }d}| j                  dkD  r| j                  | j                  |      }d}| j                  r| j                  | j                  |      }| j                  }t        |t         j                        r|j                  ||z         |j                  t        j                  t        j                   |j"                        d|z
  z  |j$                               | j                  ry|j                  ||z         |j                  t        j                  |j"                  d|z
  z  |j$                               |t        j                   |      z
  | j&                  z   }	n|| j&                  z   }	t        j(                  |	|j$                        }
t        j                  ||j"                  z  t         j*                  j-                  |
      z  |j$                        }| j                  dkD  rB|j                  | j                  |z         |j                  |       |j/                  |        y|j                  |        y|j                  ||z  d|z
  t        j                   |      z  z          | j                  rC|j                  ||z  d|z
  |z  z          |t        j                   |      z
  | j&                  z   }	n|| j&                  z   }	||z  t         j*                  j-                  |	      z  }| j                  dkD  r4|j                  | j                  |z  |z          |j/                  |        y|j/                  |        y)z=Update step given gradient and the associated model variable.Nr      )tfcastr   dtype_var_keyr(   _index_dictr   r+   r   r,   r   
isinstanceIndexedSlicesassignscatter_addsquarevaluesindicesr   gathermathrsqrt
assign_add)r   gradientvariablelrvar_keyr$   r   average_gradr   denominatordenominator_slices	increments               r    update_stepzRMSprop.update_step   s   WWT''8--)##D$4$4W$=>==1t'7'7'@AH==2243C3CG3LMLhhh 0 01OOC(N+    IIhoo.!c':H<L<L
 }}##C,$67(($$ 1s73X5E5E
 '<)@@4<<O&5!#;8H8H!I((X__$rww}}5G'HH  I
 }}q  89$$Y/##XI.$$iZ0 OOC(Na#g89L-LLM}}##C,$6!c'X9M$MN&<)@@4<<O&5Xk(BBI}}q  89 DE##XI.##YJ/r!   c                     t         |          }|j                  | j                  | j                        | j
                  | j                  | j                  | j                  d       |S )N)r   r   r   r   r   )	r   
get_configupdate_serialize_hyperparameterr   r   r   r   r   )r   configr   s     r    rK   zRMSprop.get_config   s]    #%!%!?!?''" xx MM<< MM
	
 r!   )gMbP?g?g        gHz>FNNNNFgGz?d   Tr   )	__name__
__module____qualname____doc__r   r&   rI   rK   __classcell__)r   s   @r    r   r      sP    )Z  #"!H4:0x r!   r   z{{base_optimizer_keyword_args}})rS   tensorflow.compat.v2compatv2r1   tf_keras.src.optimizersr   'tf_keras.src.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r!   r    <module>r^      s    ( ! ! - O : +3		ti!! t tn //))%y'L'Lr!   