
    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9 " S	 S
\R                  5      5       5       r\R                   R                  S\R                  5      \l         g)zNadam optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportz#keras.optimizers.experimental.Nadamzkeras.optimizers.Nadam)v1c                   h   ^  \ 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
$ )	Nadam   a  Optimizer that implements the Nadam algorithm.

Much like Adam is essentially RMSprop with momentum, Nadam is Adam with
Nesterov momentum.

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`.
    {{base_optimizer_keyword_args}}

Reference:
    - [Dozat, 2015](http://cs229.stanford.edu/proj2015/054_report.pdf).

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        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)selflearning_rater   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/nadam.pyr   Nadam.__init__<   s`    " 	 	
%+%$;#	
 	
 #77F    c                   > [         TU ]  U5        [        U SS5      (       a  gSU l        / U l        / U l        [        R                  " SUS   R                  S9U l	        S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     g)zInitialize optimizer variables.

Nadam optimizer has 2 types of variables: momentums and velocities.

Args:
    var_list: list of model variables to build Nadam variables on.
_builtFNT      ?r   )dtype   m)model_variablevariable_namev)r   buildgetattrr$   
_momentums_velocitiestfVariabler&   
_u_product_u_product_counterappendadd_variable_from_reference)r   var_listvarr   s      r    r,   Nadam.build^   s     	h45))++c!1B1BC #$COO""00#&c 1 
 ##00#&c 1  r"   c                 "  ^ ^ UR                   n[        R                  " T R                  U5      n[        R                  " T R                  S-   U5      n[        R                  " T R                  S-   U5      n[        R                  " SU5      n[        R                  " T R
                  U5      n[        R                  " T R                  U5      n	USS[        R                  " Xu5      -  -
  -  mUSS[        R                  " Xv5      -  -
  -  n
U 4S jnU U4S jn[        R                  " T R                  T R                  S-   :H  UUS9nX-  n[        R                  " X5      nT R                  U5      nT R                  T R                  U      nT R                  T R                  U      n[        U[        R                  5      (       Ga  UR!                  U* SU-
  -  5        UR#                  [        R                  " UR$                  SU-
  -  UR&                  5      5        UR!                  U* SU	-
  -  5        UR#                  [        R                  " [        R(                  " UR$                  5      SU	-
  -  UR&                  5      5        U
U-  SU-
  -  ST-
  U-  SU-
  -  -   nUSU-
  -  nUR+                  UU-  [        R,                  " U5      T R.                  -   -  5        g	UR!                  UU-
  SU-
  -  5        UR!                  [        R(                  " U5      U-
  SU	-
  -  5        U
U-  SU-
  -  ST-
  U-  SU-
  -  -   nUSU-
  -  nUR+                  UU-  [        R,                  " U5      T R.                  -   -  5        g	)
z=Update step given gradient and the associated model variable.r'      gQ?r%   g      ?c                     > T R                   $ )N)r2   )r   s   r    get_cached_u_product/Nadam.update_step.<locals>.get_cached_u_product   s    ??"r"   c                     > TR                   T-  n TR                   R                  U 5        T=R                  S-  sl        U $ )Nr'   )r2   assignr3   )u_product_tr   u_ts    r    compute_new_u_product0Nadam.update_step.<locals>.compute_new_u_product   s:    //C/KOO"";/##q(#r"   )true_fnfalse_fnN)r&   r0   castr   
iterationsr   r   powcondr3   _var_keyr.   _index_dictr/   
isinstanceIndexedSlices
assign_addscatter_addvaluesindicessquare
assign_subsqrtr   )r   gradientvariable	var_dtypelr
local_step	next_stepdecayr   r   u_t_1r<   rB   r@   u_product_t_1beta_2_powervar_keyr(   r+   m_hatv_hatrA   s   `                    @r    update_stepNadam.update_step}   s   NN	WWT''3WWT__q0)<
GGDOOa/;	i(i0i0cRVVE%>??@#rvve'? @@A	#	 gg##!(;<(*

 $+vvf1--)OOD,,W56T--g67h 0 011LL!q6z*+MM  OOq6z2H4D4D
 LL!q6z*+MM  IIhoo.!f*=x?O?O
 AI]!23q3w(6JK7 E \)*E0M NO LL(Q,1v:67LL"))H-1a&jABAI]!23q3w(6JK7 E \)*E0M NOr"   c                    > [         TU ]  5       nUR                  U R                  U R                  5      U R
                  U R                  U R                  S.5        U$ )N)r   r   r   r   )r   
get_configupdate_serialize_hyperparameterr   r   r   r   )r   configr   s     r    re   Nadam.get_config   sV    #%!%!?!?''" ++++<<		
 r"   )	r$   r   r.   r2   r3   r/   r   r   r   )gMbP?g?g+?gHz>NNNNFgGz?NTr   )__name__
__module____qualname____firstlineno____doc__r   r,   rb   re   __static_attributes____classcell__)r   s   @r    r   r      sM    
>  $ D>>P@ r"   r   z{{base_optimizer_keyword_args}})rn   tensorflow.compat.v2compatv2r0   tf_keras.src.optimizersr   'tf_keras.src.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r"   r    <module>rz      s{    & ! ! - O : )+ClI l l^ %%%y'L'Lr"   