a
    SicW                     @   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g dG dd	 d	ejZej d
eje_ dS )zNadam optimizer implementation.    N)	optimizer)generic_utils)keras_exportz#keras.optimizers.experimental.Nadam)v1c                       sB   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                    sF   t  jf ||||||	|
|d| | || _|| _|| _|| _d S )N)name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   kwargs	__class__ i/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/nadam.pyr   8   s     	zNadam.__init__c                    s   t  | t| ddrdS d| _g | _g | _tjd|d jd| _	d| _
|D ]0}| j| j|d	d
 | j| j|dd
 qNd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*   X   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   n||| 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)r0   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%   )r0   assignr1   )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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   Zu_t_1r7   r<   r9   Zu_product_t_1beta_2_powervar_keyr&   r)   Zm_hatv_hatr    r:   r!   update_stepw   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!   rY      s    

zNadam.get_config)r   r   r	   r
   NNNFr   NTr   )	__name__
__module____qualname____doc__r   r*   rX   rY   __classcell__r    r    r   r!   r      s"                @r   z{{base_optimizer_keyword_args}})r`   tensorflow.compat.v2compatv2r.   'keras.optimizers.optimizer_experimentalr   keras.utilsr    tensorflow.python.util.tf_exportr   register_keras_serializable	Optimizerr   replacebase_optimizer_keyword_argsr    r    r    r!   <module>   s   
 ,