
    hh                         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g        G d	 d
ej                                      Zej                   j                  dej                        e_         y)zNadam optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportzkeras.optimizers.Nadamz#keras.optimizers.experimental.Nadam)v1c                   X     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z fdZd Z fdZ xZS )Nadama4  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        |   d||||||	|
||d	| | j                  |      | _        || _        || _        || _        y )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                  [/var/www/html/dev/engine/venv/lib/python3.12/site-packages/tf_keras/src/optimizers/nadam.pyr   zNadam.__init__<   sf    " 	 	
%+%$;#	
 	
 #77F    c                    t         |   |       t        | dd      ryd| _        g | _        g | _        t        j                  d|d   j                        | _	        d| _
        |D ]Z  }| j                  j                  | j                  |d	
             | j
                  j                  | j                  |d
             \ y)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*   zNadam.build^   s     	h45)++c!1B1BC #$ 
	COO""00#&c 1 
 ##00#&c 1 
	r    c                     |j                   }t        j                   j                  |      }t        j                   j                  dz   |      }t        j                   j                  dz   |      }t        j                  d|      }t        j                   j
                  |      }t        j                   j                  |      }	|ddt        j                  ||      z  z
  z  |ddt        j                  ||      z  z
  z  }
 fd} fd}t        j                   j                   j                  dz   k(  ||      }||
z  }t        j                  |	|      } j                  |      } j                   j                  |      } j                   j                  |      }t        |t        j                        r|j!                  | d|z
  z         |j#                  t        j                  |j$                  d|z
  z  |j&                               |j!                  | d|	z
  z         |j#                  t        j                  t        j(                  |j$                        d|	z
  z  |j&                               |
|z  d|z
  z  dz
  |z  d|z
  z  z   }|d|z
  z  }|j+                  ||z  t        j,                  |       j.                  z   z         y	|j!                  ||z
  d|z
  z         |j!                  t        j(                  |      |z
  d|	z
  z         |
|z  d|z
  z  dz
  |z  d|z
  z  z   }|d|z
  z  }|j+                  ||z  t        j,                  |       j.                  z   z         y	)
z=Update step given gradient and the associated model variable.r%      gQ?r#   g      ?c                       j                   S )N)r0   )r   s   r   get_cached_u_productz/Nadam.update_step.<locals>.get_cached_u_product   s    ??"r    c                      j                   z  } j                   j                  |        xj                  dz  c_        | S )Nr%   )r0   assignr1   )u_product_tr   u_ts    r   compute_new_u_productz0Nadam.update_step.<locals>.compute_new_u_product   s:    //C/KOO"";/##q(#r    )true_fnfalse_fnN)r$   r.   castr   
iterationsr   r   powcondr1   _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_1r9   r>   r<   u_product_t_1beta_2_powervar_keyr&   r)   m_hatv_hatr=   s   `                    @r   update_stepzNadam.update_step}   s   NN	WWT''3WWT__q0)<
GGDOOa/;	i(i0i0cRVVE:%>??@#rvveY'? @@A	#	 gg##!(;<(*

 $e+vvfj1--)OOD,,W56T--g67h 0 0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         |          }|j                  | j                  | j                        | j
                  | j                  | j                  d       |S )N)r   r   r   r   )r   
get_configupdate_serialize_hyperparameterr   r   r   r   )r   configr   s     r   r_   zNadam.get_config   sV    #%!%!?!?''" ++++<<		
 r    )gMbP?g?g+?gHz>NNNNFgGz?NTr   )	__name__
__module____qualname____doc__r   r*   r]   r_   __classcell__)r   s   @r   r   r      sM    
>  $ D>>P@ r    r   z{{base_optimizer_keyword_args}})rf   tensorflow.compat.v2compatv2r.   tf_keras.src.optimizersr   'tf_keras.src.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r    r   <module>rq      s{    & ! ! - O : ClI l l^ %%%y'L'Lr    