
    6bi9                         S r SSKrSSKJs  Jr  SSKJr  SSK	J
r
  SSKJr  \" SSS/S9 " S	 S
\
R                  5      5       r\rg)z!RMSprop optimizer implementation.    N)backend_config)optimizer_v2)keras_exportzkeras.optimizers.legacy.RMSpropzkeras.optimizers.RMSprop)v1c                   ~   ^  \ rS rSrSrSr      SU 4S jjrS rU 4S jrSS jr	SS jr
U 4S	 jrU 4S
 jrSrU =r$ )RMSprop   a  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: A `Tensor`, floating point value, or a schedule that is a
    `tf.keras.optimizers.schedules.LearningRateSchedule`, or a callable
    that takes no arguments and returns the actual value to use. The
    learning rate. Defaults to `0.001`.
  rho: Discounting factor for the history/coming gradient. Defaults to
    `0.9`.
  momentum: A scalar or a scalar `Tensor`. Defaults to `0.0`.
  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`.
  name: Optional name prefix for the operations created when applying
    gradients. Defaults to `"RMSprop"`.
  **kwargs: keyword arguments. Allowed arguments are `clipvalue`,
    `clipnorm`, `global_clipnorm`.
    If `clipvalue` (float) is set, the gradient of each weight
    is clipped to be no higher than this value.
    If `clipnorm` (float) is set, the gradient of each weight
    is individually clipped so that its norm is no higher than this value.
    If `global_clipnorm` (float) is set the gradient of all weights is
    clipped so that their global norm is no higher than this value.

Note that in the dense implementation of this algorithm, variables and their
corresponding accumulators (momentum, gradient moving average, square
gradient moving average) will be updated even if the gradient is zero
(i.e. accumulators will decay, momentum will be applied). The sparse
implementation (used when the gradient is an `IndexedSlices` object,
typically because of `tf.gather` or an embedding lookup in the forward pass)
will not update variable slices or their accumulators unless those slices
were used in the forward pass (nor is there an "eventual" correction to
account for these omitted updates). This leads to more efficient updates for
large embedding lookup tables (where most of the slices are not accessed in
a particular graph execution), but differs from the published algorithm.

Usage:

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

Reference:
  - [Hinton, 2012](
    http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf)
Tc                 H  > [         TU ]  " U40 UD6  U R                  SUR                  SU5      5        U R                  SU R                  5        U R                  SU5        SU l        [        U[        R                  5      (       d  [        U5      (       d  US:  a  SU l        [        U[        [        45      (       a'  US:  d  US:  a  [        S	U S
[        U5       S35      eU R                  SU5        U=(       d    [        R                  " 5       U l        XPl        g)a  Construct a new RMSprop optimizer.

Args:
  learning_rate: A `Tensor`, floating point value, or a schedule that is
    a `tf.keras.optimizers.schedules.LearningRateSchedule`, or a
    callable that takes no arguments and returns the actual value to
    use. The learning rate. Defaults to `0.001`.
  rho: Discounting factor for the history/coming gradient. Defaults to
    `0.9`.
  momentum: A scalar or a scalar `Tensor`. Defaults to `0.0`.
  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`.
  name: Optional name prefix for the operations created when applying
    gradients. Defaults to "RMSprop".
  **kwargs: keyword arguments. Allowed to be {`clipnorm`, `clipvalue`,
    `lr`, `decay`}. `clipnorm` is clip gradients by norm; `clipvalue` is
    clip gradients by value, `decay` is included for backward
    compatibility to allow time inverse decay of learning rate. `lr` is
    included for backward compatibility, recommended to use
    `learning_rate` instead.

@compatibility(eager)
When eager execution is enabled, `learning_rate`, `decay`, `momentum`,
and `epsilon` can each be a callable that takes no arguments and returns
the actual value to use. This can be useful for changing these values
across different invocations of optimizer functions.
@end_compatibility
learning_ratelrdecayrhoFr   T   z6`momentum` must be between [0, 1]. Received: momentum=z
 (of type z).momentumN)super__init__
_set_hyperget_initial_decay	_momentum
isinstancetfTensorcallableintfloat
ValueErrortyper   epsiloncentered)	selfr   r   r   r   r    namekwargs	__class__s	           `/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/optimizers/legacy/rmsprop.pyr   RMSprop.__init__c   s    Z 	((D-)HI!4!45s#x++!!!|!DNhe--qLHqL$:ZX/?rC  	
H-:."8"8":     c                     U H  nU R                  US5        M     U R                  (       a  U H  nU R                  US5        M     U R                  (       a  U H  nU R                  US5        M     g g )Nrmsr   mg)add_slotr   r    )r!   var_listvars      r%   _create_slotsRMSprop._create_slots   s]    CMM#u% >>c:.  ==c4(   r'   c                 N  > [         TU ]  XU5        [        R                  " U R	                  SU5      5      nX1U4   R                  [        X1U4   S   * [        R                  " U R                  U5      U[        R                  " U R	                  SU5      5      SU-
  S95        g )Nr   lr_tr   g      ?)neg_lr_tr   r   r   one_minus_rho)	r   _prepare_localr   identity
_get_hyperupdatedictconvert_to_tensorr   )r!   
var_device	var_dtypeapply_stater   r$   s        r%   r4   RMSprop._prepare_local   s    zkBkk$//%;<+,33%9&=>vFF,,T\\9ET__Z%KL!Ci	
r'   c                 J   UR                   UR                  R                  pTU=(       d    0 R                  XE45      =(       d    U R	                  XE5      nU R                  US5      nU R                  (       a  U R                  US5      nU R                  (       av  U R                  US5      n	[        R                  R                  UR                  U	R                  UR                  UR                  US   US   US   US   UU R                  S9
$ [        R                  R                  UR                  UR                  UR                  US   US   US   US   UU R                  S9	$ US   U-  US	   [        R                  " U5      -  -   n
[        R                  R                   R#                  XzU R                  S
9n
U
nU R                  (       an  U R                  US5      n	US   U	-  US	   U-  -   n[        R                  R                   R#                  XU R                  S
9nU
[        R                  " U5      -
  nX&S   U-  [        R$                  " U5      US   -   -  -
  n[        R                  R                   R#                  X-U R                  S
9R&                  $ )Nr)   r   r*   r1   r   r   )
r-   r*   msmomr   r   r   r   graduse_locking)	r-   r?   r@   r   r   r   r   rA   rB   r3   rB   )devicedtype
base_dtyper   _fallback_apply_stateget_slotr   r    r   raw_opsResourceApplyCenteredRMSProphandle_use_lockingResourceApplyRMSPropsquarecompatr   assignsqrtop)r!   rA   r-   r<   r:   r;   coefficientsr)   r@   r*   rms_tdenom_tmg_tvar_ts                 r%   _resource_apply_denseRMSprop._resource_apply_dense   s    #

CII,@,@I#)r..#
 ?''
> 	 mmC'>>--Z0C}}]]3-zz>>

yyzz

#F+$U+)*5(3 $ 1 1 ?   zz66

zz

#F+$U+)*5(3 $ 1 1 7 
 
 !'#-1		$1   E IILL''(9(9 ( E G}}]]3- '","?3d:;  yy||**$*;*; +   "))D/1v.5 <	#:: E 99<<&&(9(9 ' br'   c                    UR                   UR                  R                  peU=(       d    0 R                  XV45      =(       d    U R	                  XV5      nU R                  US5      nU R                  (       a  U R                  US5      n	U R                  (       aw  U R                  US5      n
[        R                  R                  UR                  U
R                  UR                  U	R                  US   US   US   US   UUU R                  S9$ [        R                  R                  UR                  UR                  U	R                  US   US   US   US   UUU R                  S9
$ X-  US	   -  n[        R                  R                  R!                  XUS   -  U R                  S
9n[        R"                  " U/5         U R%                  XU5      n[        R&                  " X5      nS S S 5        WnU R                  (       a  U R                  US5      n
XS	   -  n[        R                  R                  R!                  XUS   -  U R                  S
9n[        R"                  " U/5         U R%                  XU5      n[        R&                  " UU5      nU[        R(                  " U5      -
  nS S S 5        U R%                  UUUS   U-  [        R*                  " U5      US   -   -  5      nU R                  (       a  [        R,                  " UUW/6 $ [        R,                  " UU/6 $ ! , (       d  f       GNG= f! , (       d  f       N= f)Nr)   r   r*   r1   r   r   )r-   r*   r?   r@   r   r   r   r   rA   indicesrB   )
r-   r?   r@   r   r   r   r   rA   r[   rB   r3   rC   r2   )rD   rE   rF   r   rG   rH   r   r    r   rI   "ResourceSparseApplyCenteredRMSProprK   rL   ResourceSparseApplyRMSProprO   r   rP   control_dependencies_resource_scatter_addgatherrN   rQ   group)r!   rA   r-   r[   r<   r:   r;   rS   r)   r@   r*   rms_scaled_g_valuesrT   	rms_slicedenom_slicemg_scaled_g_valuesrV   mg_slice
var_updates                      r%   _resource_apply_sparseRMSprop._resource_apply_sparse   s    #

CII,@,@I#)r..#
 ?''
> 	 mmC'>>--Z0C}}]]3-zzDD

yyzz

#F+$U+)*5(3# $ 1 1 E   zz<<

zz

#F+$U+)*5(3# $ 1 1 =   $(;,2O"OIILL''<..D<M<M ( E ((%122"5 IIe5		 2
 $K}}]]3-%),I%I"yy||**\%00d>O>O +  ,,dV455%7D  "yyw7H"+bii.A"AK 5 33Z(77;',y*AACJ }}xx*eT!:;;88j%0117 21 54s   )L.1AM .
L= 
Mc                    > U R                   n[        U5      [        U5      S-   :X  a  [        R                  " S5      /U-   n[        TU ]  U5        g )Nr   r   )weightslennparrayr   set_weights)r!   rk   paramsr$   s      r%   ro   RMSprop.set_weightsC  sD     v;#g,**xx{mg-GG$r'   c           	         > [         TU ]  5       nUR                  U R                  S5      U R                  U R                  S5      U R                  S5      U R
                  U R                  S.5        U$ )Nr   r   r   )r   r   r   r   r   r    )r   
get_configr7   _serialize_hyperparameterr   r   r    )r!   configr$   s     r%   rs   RMSprop.get_configL  sn    #%!%!?!?#" ,,55e< :::F<< MM		
 r'   )r   r    r   )gMbP?g?g        gHz>Fr   )N)__name__
__module____qualname____firstlineno____doc___HAS_AGGREGATE_GRADr   r.   r4   rX   rh   ro   rs   __static_attributes____classcell__)r$   s   @r%   r   r      sR    
?B  C!J)
:xE2N% r'   r   )r{   numpyrm   tensorflow.compat.v2rO   v2r   tf_keras.srcr   tf_keras.src.optimizers.legacyr    tensorflow.python.util.tf_exportr   OptimizerV2r   RMSProp r'   r%   <module>r      s[    (  ! ! ' 7 : %"$EF{l&& {	{|	 r'   