o
    N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 )Nadama  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__Z_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   ]D:\Projects\ConvertPro\env\Lib\site-packages\keras/optimizers/optimizer_experimental/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)Zmodel_variableZvariable_namev)r   buildgetattrr"   
_momentums_velocitiestfVariabler$   
_u_product_u_product_counterappendZadd_variable_from_reference)r   Z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)r.   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%   )r.   Zassignr/   )u_product_tr   Zu_tr   r!   compute_new_u_product   s   
z0Nadam.update_step.<locals>.compute_new_u_product)Ztrue_fnZfalse_fnN)r$   r,   castr   Z
iterationsr   r   powZcondr/   Z_var_keyr*   Z_index_dictr+   
isinstanceZIndexedSlicesZ
assign_addZscatter_addvaluesindicesZsquareZ
assign_subsqrtr   )r   ZgradientvariableZ	var_dtypelrZ
local_stepZ	next_stepZdecayr   r   Zu_t_1r3   r6   r4   Zu_product_t_1Zbeta_2_powerZvar_keyr&   r'   Zm_hatZv_hatr   r5   r!   update_step{   s\   
""zNadam.update_stepc                    s0   t   }|| | j| j| j| jd |S )N)r   r   r   r   )r   
get_configupdateZ_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}})rF   Ztensorflow.compat.v2compatv2r,   Z'keras.optimizers.optimizer_experimentalr   Z keras.saving.object_registrationr   Z tensorflow.python.util.tf_exportr   Z	Optimizerr   replaceZbase_optimizer_keyword_argsr   r   r   r!   <module>   s    .
