
    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Adam optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportzkeras.optimizers.Adamz"keras.optimizers.experimental.Adamz*keras.dtensor.experimental.optimizers.Adam)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
$ )	Adam   a
  Optimizer that implements the Adam algorithm.

Adam optimization is a stochastic gradient descent method that is based on
adaptive estimation of first-order and second-order moments.

According to
[Kingma et al., 2014](http://arxiv.org/abs/1412.6980),
the 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:
    - [Kingma et al., 2014](http://arxiv.org/abs/1412.6980)
    - [Reddi et al., 2018](
        https://openreview.net/pdf?id=ryQu7f-RZ) for `amsgrad`.

Notes:

The default value of 1e-7 for epsilon might not be a good default in
general. For example, when training an Inception network on ImageNet a
current good choice is 1.0 or 0.1. Note that since Adam uses the
formulation just before Section 2.1 of the Kingma and Ba paper rather than
the formulation in Algorithm 1, the "epsilon" referred to here is "epsilon
hat" in the paper.

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US.	UD6  U R                  U5      U l        X l        X0l        X@l        XPl        g )N)	nameweight_decayclipnorm	clipvalueglobal_clipnormuse_emaema_momentumema_overwrite_frequencyjit_compile )super__init___build_learning_rate_learning_ratebeta_1beta_2epsilonamsgrad)selflearning_rater   r   r   r   r   r   r   r   r   r   r   r   r   kwargs	__class__s                   V/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/optimizers/adam.pyr   Adam.__init__\   se    $ 	 	
%+%$;#	
 	
 #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.

Adam 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 Adam 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+   
Adam.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   r1   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Adam.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                  S.5        U$ )N)r   r   r   r   r   )	r   
get_configupdate_serialize_hyperparameterr   r   r   r   r   )r   configr    s     r!   rU   Adam.get_config   s]    #%!%!?!?''" ++++<<<<
	
 r#   )	r%   r   r-   r.   r1   r   r   r   r   )gMbP?g?g+?gHz>FNNNNFgGz?NTr   )__name__
__module____qualname____firstlineno____doc__r   r+   rR   rU   __static_attributes____classcell__)r    s   @r!   r   r      sR    8x  $"H!F)KV r#   r   z{{base_optimizer_keyword_args}})r^   tensorflow.compat.v2compatv2r7   tf_keras.src.optimizersr   'tf_keras.src.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r#   r!   <module>rj      s    % ! ! - O : (0		{9 { {| ||##%y'L'Lr#   