a
    SicA                     @   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 Adamax optimizer implementation.    N)	optimizer)generic_utils)keras_exportz$keras.optimizers.experimental.Adamax)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 )Adamaxa  Optimizer that implements the Adamax algorithm.

    Adamax, a variant of Adam based on the infinity norm, is a first-order
    gradient-based optimization method. Due to its capability of adjusting the
    learning rate based on data characteristics, it is suited to learn
    time-variant process, e.g., speech data with dynamically changed noise
    conditions. Default parameters follow those provided in the paper (see
    references below).

    Initialization:

    ```python
    m = 0  # Initialize initial 1st moment vector
    u = 0  # Initialize the exponentially weighted infinity norm
    t = 0  # Initialize timestep
    ```

    The update rule for parameter `w` with gradient `g` is described at the end
    of section 7.1 of the paper (see the referenece section):

    ```python
    t += 1
    m = beta1 * m + (1 - beta) * g
    u = max(beta2 * u, abs(g))
    current_lr = learning_rate / (1 - beta1 ** t)
    w = w - current_lr * m / (u + epsilon)
    ```

    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. The exponential decay
        rate for the 1st moment estimates.
      beta_2: A float value or a constant float tensor. The exponential decay
        rate for the exponentially weighted infinity norm.
      epsilon: A small constant for numerical stability.
      {{base_optimizer_keyword_args}}

    Reference:
      - [Kingma et al., 2014](http://arxiv.org/abs/1412.6980)
    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__ j/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/adamax.pyr   I   s     	zAdamax.__init__c                    sl   t  | t| dr | jr dS d| _g | _g | _|D ]0}| j| j|dd | j| j|dd q6dS )a  Initialize optimizer variables.

        Adamax optimizer has 2 types of variables: momentums (denoted as m),
        exponentially weighted infinity norm (denoted as u).

        Args:
          var_list: list of model variables to build Adamax variables on.
        _builtNTm)model_variablevariable_nameu)r   buildhasattrr"   _m_uappendadd_variable_from_reference)r   var_listvarr   r    r!   r'   i   s"    	zAdamax.buildc                 C   sp  t | j|j}t | jd |j}t t | j|j|}| |}| j| j	|  }| j
| j	|  }t|t jr|j}	|| d| j   |t |jd| j  |	 ||| j  t ||	}
t |
t |j|
 }|t ||	 ||| d| || j    nV||| d| j   |t | j| t | ||| d| || j    dS )z=Update step given gradient and the associated model variable.   N)tfcastr   dtype
iterationspowr   _var_keyr)   _index_dictr*   
isinstanceIndexedSlicesindices
assign_addscatter_addvaluesassignr   gathermaximumabs
assign_subr   )r   gradientvariablelr
local_stepbeta_1_powervar_keyr#   r&   r9   Zu_sliceZu_slice_incrementalr    r    r!   update_step   s2    
zAdamax.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!   rI      s    

zAdamax.get_config)r   r   r	   r
   NNNFr   NTr   )	__name__
__module____qualname____doc__r   r'   rH   rI   __classcell__r    r    r   r!   r      s"   .             "r   z{{base_optimizer_keyword_args}})rP   tensorflow.compat.v2compatv2r0   '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   
 