
    6bi"                         S r SSKJs  Jr  SSKJr  SSKJr  SSK	J
r
  \" 5       \
" SSS/ S	9 " S
 S\R                  5      5       5       r\R                   R                  S\R                  5      \l         g)zAdamW optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportzkeras.optimizers.AdamWz#keras.optimizers.experimental.AdamWz+keras.dtensor.experimental.optimizers.AdamW)v1c                   j   ^  \ rS rSrSr              SU 4S jjrU 4S jrS rU 4S jrSr	U =r
$ )	AdamW   aO
  Optimizer that implements the AdamW algorithm.

AdamW optimization is a stochastic gradient descent method that is based on
adaptive estimation of first-order and second-order moments with an added
method to decay weights per the techniques discussed in the paper,
'Decoupled Weight Decay Regularization' by
[Loshchilov, Hutter et al., 2019](https://arxiv.org/abs/1711.05101).

According to
[Kingma et al., 2014](http://arxiv.org/abs/1412.6980),
the underying Adam method is "*computationally
efficient, has little memory requirement, invariant to diagonal rescaling of
gradients, and is well suited for problems that are large in terms of
data/parameters*".

Args:
    learning_rate: A `tf.Tensor`, floating point value, 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.
    beta_1: A float value or a constant float tensor, or a callable
        that takes no arguments and returns the actual value to use. The
        exponential decay rate for the 1st moment estimates.
        Defaults to 0.9.
    beta_2: A float value or a constant float tensor, or a callable
        that takes no arguments and returns the actual value to use. The
        exponential decay rate for the 2nd moment estimates.
        Defaults to 0.999.
    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.
    amsgrad: Boolean. Whether to apply AMSGrad variant of this algorithm
        from the paper "On the Convergence of Adam and beyond".
        Defaults to `False`.
    {{base_optimizer_keyword_args}}

Reference:
  - [Loshchilov et al., 2019](https://arxiv.org/abs/1711.05101)
  - [Kingma et al., 2014](http://arxiv.org/abs/1412.6980) for `adam`
  - [Reddi et al., 2018](
      https://openreview.net/pdf?id=ryQu7f-RZ) for `amsgrad`.

Notes:

The sparse implementation of this algorithm (used when the gradient is an
IndexedSlices object, typically because of `tf.gather` or an embedding
lookup in the forward pass) does apply momentum to variable slices even if
they were not used in the forward pass (meaning they have a gradient equal
to zero). Momentum decay (beta1) is also applied to the entire momentum
accumulator. This means that the sparse behavior is equivalent to the dense
behavior (in contrast to some momentum implementations which ignore momentum
unless a variable slice was actually used).
c                    > [         TU ]  " SUUUU	U
UUUS.UD6  U R                  U5      U l        X l        X0l        X@l        XPl        X`l        U R                  c  [        S5      eg )N)nameclipnorm	clipvalueglobal_clipnormuse_emaema_momentumema_overwrite_frequencyjit_compilezLMissing value of `weight_decay` which is required and must be a float value. )
super__init___build_learning_rate_learning_rateweight_decaybeta_1beta_2epsilonamsgrad
ValueError)selflearning_rater   r   r   r   r   r   r   r   r   r   r   r   r   kwargs	__class__s                   W/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/optimizers/adamw.pyr   AdamW.__init__Z   s    $ 	 
	
+%$;#
	
 
	
 #77F($*  %    c                   > [         TU ]  U5        [        U S5      (       a  U R                  (       a  gSU l        / U l        / U l        U HU  nU R                  R                  U R                  USS95        U R
                  R                  U R                  USS95        MW     U R                  (       a:  / U l	        U H,  nU R                  R                  U R                  USS95        M.     gg)zInitialize optimizer variables.

AdamW optimizer has 3 types of variables: momentums, velocities and
velocity_hat (only set when amsgrad is applied),

Args:
  var_list: list of model variables to build AdamW variables on.
_builtNTm)model_variablevariable_namevvhat)
r   buildhasattrr&   
_momentums_velocitiesappendadd_variable_from_referencer   _velocity_hats)r   var_listvarr!   s      r"   r,   AdamW.build   s     	h4""t{{COO""00#&c 1 
 ##00#&c 1   <<"$D##**44'*& 5    r$   c                    [         R                  " U R                  UR                  5      n[         R                  " U R                  S-   UR                  5      n[         R
                  " [         R                  " U R                  UR                  5      U5      n[         R
                  " [         R                  " U R                  UR                  5      U5      nU R                  U5      nU R                  U R                  U      nU R                  U R                  U      n	U[         R                  " SU-
  5      -  SU-
  -  n
[        U[         R                  5      (       Gax  UR                  U* SU R                  -
  -  5        UR!                  [         R                  " UR"                  SU R                  -
  -  UR$                  5      5        U	R                  U	* SU R                  -
  -  5        U	R!                  [         R                  " [         R&                  " UR"                  5      SU R                  -
  -  UR$                  5      5        U R(                  (       aC  U R*                  U R                  U      nUR-                  [         R.                  " X5      5        Un	UR1                  X-  [         R                  " U	5      U R2                  -   -  5        gUR                  X-
  SU R                  -
  -  5        U	R                  [         R&                  " U5      U	-
  SU R                  -
  -  5        U R(                  (       aC  U R*                  U R                  U      nUR-                  [         R.                  " X5      5        Un	UR1                  X-  [         R                  " U	5      U R2                  -   -  5        g)z=Update step given gradient and the associated model variable.   N)tfcastr   dtype
iterationspowr   r   _var_keyr.   _index_dictr/   sqrt
isinstanceIndexedSlices
assign_addscatter_addvaluesindicessquarer   r2   assignmaximum
assign_subr   )r   gradientvariablelr
local_stepbeta_1_powerbeta_2_powervar_keyr'   r*   alphav_hats               r"   update_stepAdamW.update_step   s   WWT''8WWT__q0(..A
vvbggdkk8>>BJOvvbggdkk8>>BJO--)OOD,,W56T--g67RWWQ-..!l2BCh 0 011LL!q4;;/0MM  OOq4;;79I9I
 LL!q4;;/0MM  IIhoo.!dkk/B$$ ||++D,<,<W,EFRZZ12rwwqzDLL/H IJ LL(,1t{{?;<LL"))H-1a$++oFG||++D,<,<W,EFRZZ12rwwqzDLL/H IJr$   c           	         > [         TU ]  5       nUR                  U R                  U R                  5      U R
                  U R                  U R                  U R                  U R                  S.5        U$ )N)r   r   r   r   r   r   )
r   
get_configupdate_serialize_hyperparameterr   r   r   r   r   r   )r   configr!   s     r"   rV   AdamW.get_config   sf    #%!%!?!?''" !% 1 1++++<<<<		
 r$   )
r&   r   r.   r/   r2   r   r   r   r   r   )gMbP?gMbp?g?g+?gHz>FNNNFgGz?NTr   )__name__
__module____qualname____firstlineno____doc__r   r,   rS   rV   __static_attributes____classcell__)r!   s   @r"   r   r      sR    5r  $(T!F)KV r$   r   z{{base_optimizer_keyword_args}})r_   tensorflow.compat.v2compatv2r8   tf_keras.src.optimizersr   'tf_keras.src.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r$   r"   <module>rk      s    & " ! - O : )1		I  D %%%y'L'Lr$   