o
    i e                     @   sv   d Z ddlm  mZ ddlmZ ddlmZ ddl	m
Z
 e e
ddg dG d	d
 d
ejZej deje_ dS )zNadam optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportz#keras.optimizers.experimental.Nadamzkeras.optimizers.Nadam)v1c                       s\   e Zd ZdZ														 d fd
d	Z fddZdd Z fddZ  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).

    MbP??+?Hz>NFGz?Tc                    sH   t  jd||||||	|
||d	| | || _|| _|| _|| _d S )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__r   I/var/www/myenv/lib/python3.10/site-packages/keras/src/optimizers/nadam.pyr   <   s"   

zNadam.__init__c                    s   t  | t| ddrdS d| _g | _g | _tjd|d jd| _	d| _
|D ]}| j| j|d	d
 | j| j|dd
 q'dS )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    r   r"   r+   ^   s(   zNadam.buildc                    sP  |j }t j|}t jd |}t jd |}td|}t j|}t j|}	|ddt||   |ddt||   }
 fdd} fdd	}tj j	 jd k||d
}||
 }t|	|} 
|} j j|  } j j|  }t|tjr|| d|   |t|jd|  |j || d|	   |tt|jd|	  |j |
| d|  d | d|   }|d|  }||| t| j   dS ||| d|   |t|| d|	   |
| d|  d | d|   }|d|  }||| t| j   dS )z=Update step given gradient and the associated model variable.r&      gQ?r$   g      ?c                      s    j S )N)r1   r   )r   r   r"   get_cached_u_product   s   z/Nadam.update_step.<locals>.get_cached_u_productc                     s(    j  }  j |    jd7  _| S )Nr&   )r1   assignr2   )u_product_tr   u_tr   r"   compute_new_u_product   s   
z0Nadam.update_step.<locals>.compute_new_u_product)true_fnfalse_fnN)r%   r/   castr   
iterationsr   r   powcondr2   _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_1r8   r=   r:   u_product_t_1beta_2_powervar_keyr'   r*   m_hatv_hatr   r;   r"   update_step}   s\   
""zNadam.update_stepc                    s0   t   }|| | j| j| j| jd |S )N)r   r   r   r   )r   
get_configupdate_serialize_hyperparameterr   r   r   r   )r   configr    r   r"   r]      s   

zNadam.get_config)r   r   r	   r
   NNNNFr   NTr   )	__name__
__module____qualname____doc__r   r+   r\   r]   __classcell__r   r   r    r"   r      s&    "@r   z{{base_optimizer_keyword_args}})rd   tensorflow.compat.v2compatv2r/   keras.src.optimizersr   $keras.src.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r   r   r"   <module>   s    0
