a
    Sic-!                     @   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Adam optimizer implementation.    N)	optimizer)generic_utils)keras_exportz"keras.optimizers.experimental.Adam)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 )Adama  Optimizer that implements the Adam algorithm.

    Adam optimization is a stochastic gradient descent method that is based on
    adaptive estimation of first-order and second-order moments.

    According to
    [Kingma et al., 2014](http://arxiv.org/abs/1412.6980),
    the method is "*computationally
    efficient, has little memory requirement, invariant to diagonal rescaling of
    gradients, and is well suited for problems that are large in terms of
    data/parameters*".

    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.
      amsgrad: Boolean. Whether to apply AMSGrad variant of this algorithm from
        the paper "On the Convergence of Adam and beyond". Defaults to `False`.
      {{base_optimizer_keyword_args}}

    Reference:
      - [Kingma et al., 2014](http://arxiv.org/abs/1412.6980)
      - [Reddi et al., 2018](
          https://openreview.net/pdf?id=ryQu7f-RZ) for `amsgrad`.

    Notes:

    The default value of 1e-7 for epsilon might not be a good default in
    general. For example, when training an Inception network on ImageNet a
    current good choice is 1.0 or 0.1. Note that since Adam uses the
    formulation just before Section 2.1 of the Kingma and Ba paper rather than
    the formulation in Algorithm 1, the "epsilon" referred to here is "epsilon
    hat" in the paper.

    The sparse implementation of this algorithm (used when the gradient is an
    IndexedSlices object, typically because of `tf.gather` or an embedding
    lookup in the forward pass) does apply momentum to variable slices even if
    they were not used in the forward pass (meaning they have a gradient equal
    to zero). Momentum decay (beta1) is also applied to the entire momentum
    accumulator. This means that the sparse behavior is equivalent to the dense
    behavior (in contrast to some momentum implementations which ignore momentum
    unless a variable slice was actually used).
    MbP??+?Hz>FNGz?Tc                    sL   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amsgrad)selflearning_rater   r   r   r   r   r   r   r   r   r   r   r   kwargs	__class__ h/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/adam.pyr   T   s"    	zAdam.__init__c                    s   t  | t| dr | jr dS d| _g | _g | _|D ]0}| j| j|dd | j| j|dd q6| jrg | _	|D ]}| j	| j|dd qxdS )a  Initialize optimizer variables.

        Adam optimizer has 3 types of variables: momentums, velocities and
        velocity_hat (only set when amsgrad is applied),

        Args:
          var_list: list of model variables to build Adam variables on.
        _builtNTm)model_variablevariable_namevvhat)
r   buildhasattrr#   
_momentums_velocitiesappendadd_variable_from_referencer   _velocity_hats)r   var_listvarr   r!   r"   r)   v   s2    	z
Adam.buildc                 C   s  d}d}t | j|j}t | jd |j}t t | j|j|}t t | j|j|}| |}| j	| j
|  }| j| j
|  }	|t d|  d|  }
t|t jrn|| d| j   |t |jd| j  |j |	|	 d| j   |	t t |jd| j  |j | jrN| j| j
|  }|t ||	 |}	|||
 t |	| j   n||| d| j   |	t ||	 d| j   | jr| j| j
|  }|t ||	 |}	|||
 t |	| j   dS )z=Update step given gradient and the associated model variable.N   )tfcastr   dtype
iterationspowr   r   _var_keyr+   _index_dictr,   sqrt
isinstanceIndexedSlices
assign_addscatter_addvaluesindicessquarer   r/   assignmaximum
assign_subr   )r   gradientvariablebeta_1_powerbeta_2_powerlr
local_stepvar_keyr$   r'   alphav_hatr!   r!   r"   update_step   sH    
 zAdam.update_stepc                    s4   t   }|| | j| j| j| j| jd |S )N)r   r   r   r   r   )	r   
get_configupdate_serialize_hyperparameterr   r   r   r   r   )r   configr   r!   r"   rO      s    
zAdam.get_config)r   r   r	   r
   FNNNFr   NTr   )	__name__
__module____qualname____doc__r   r)   rN   rO   __classcell__r!   r!   r   r"   r      s$   9             "#-r   z{{base_optimizer_keyword_args}})rV   tensorflow.compat.v2compatv2r3   '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   
 <