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 Adamax optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportz$keras.optimizers.experimental.Adamaxzkeras.optimizers.Adamax)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 )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                    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/adamax.pyr   K   s"   

zAdamax.__init__c                    sl   t  | t| dr| jrdS d| _g | _g | _|D ]}| j| j|dd | j| j|dd qd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)Zmodel_variableZvariable_nameu)r   buildhasattrr"   _m_uappendZadd_variable_from_reference)r   Zvar_listvarr   r   r!   r%   m   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    dS ||| d| j   |t | j| t | ||| d| || j    dS )z=Update step given gradient and the associated model variable.   N)tfcastr   ZdtypeZ
iterationspowr   Z_var_keyr'   Z_index_dictr(   
isinstanceZIndexedSlicesindicesZ
assign_addZscatter_addvaluesZassignr   gathermaximumabsZ
assign_subr   )r   ZgradientvariablelrZ
local_stepZbeta_1_powerZvar_keyr#   r$   r0   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updateZ_serialize_hyperparameterr   r   r   r   )r   configr   r   r!   r8      s   

zAdamax.get_config)r   r   r	   r
   NNNNFr   NTr   )	__name__
__module____qualname____doc__r   r%   r7   r8   __classcell__r   r   r   r!   r      s&    .""r   z{{base_optimizer_keyword_args}})r>   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    
