o
    ·2úh±  ã                   @   sV   d dl mZ d dlmZ d dlmZ edgƒG dd„ dejƒƒZej 	dej
¡e_dS )	é    )Úops)Úkeras_export)Ú	optimizerzkeras.optimizers.Adamaxc                       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 reference 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 float, a
            `keras.optimizers.schedules.LearningRateSchedule` instance, 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?çÍÌÌÌÌÌì?ç+‡ÙÎ÷ï?çH¯¼šò×z>NFç®Gáz®ï?Úadamaxc                    s@   t ƒ jd|||||||	|
|||dœ|¤Ž || _|| _|| _d S )N)Úlearning_rateÚnameÚweight_decayÚclipnormÚ	clipvalueÚglobal_clipnormÚuse_emaÚema_momentumÚema_overwrite_frequencyÚloss_scale_factorÚgradient_accumulation_steps© )ÚsuperÚ__init__Úbeta_1Úbeta_2Úepsilon)Úselfr   r   r   r   r   r   r   r   r   r   r   r   r   r   Úkwargs©Ú	__class__r   úV/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/optimizers/adamax.pyr   5   s$   õô
zAdamax.__init__c                    s2   | j rdS tƒ  |¡ |  |ddg¡\| _| _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.
        NÚmomentumÚnorm)Úbuiltr   ÚbuildÚadd_optimizer_variablesÚ_mÚ_u)r   Úvar_listr   r   r!   r%   Y   s   	ÿzAdamax.buildc           	      C   sä   t  ||j¡}t  ||j¡}t  | jd |j¡}t  t  | j|j¡|¡}| j|  |¡ }| j|  |¡ }|  	|t  
t  ||¡d| j ¡¡ |  |t  t  
| j|¡t  |¡¡¡ |  |t  t  
||¡t  
d| t  || j¡¡¡¡ dS )z=Update step given gradient and the associated model variable.é   N)r   ÚcastÚdtypeÚ
iterationsÚpowerr   r'   Ú_get_variable_indexr(   Ú
assign_addÚmultiplyÚsubtractÚassignÚmaximumr   ÚabsÚ
assign_subÚdivideÚaddr   )	r   ÚgradientÚvariabler   ÚlrÚ
local_stepÚbeta_1_powerÚmÚur   r   r!   Úupdate_stepi   s*   ÿÿÿ
þþzAdamax.update_stepc                    s&   t ƒ  ¡ }| | j| j| jdœ¡ |S )N)r   r   r   )r   Ú
get_configÚupdater   r   r   )r   Úconfigr   r   r!   rA   ƒ   s   
ýÿzAdamax.get_config)r   r   r   r	   NNNNFr
   NNNr   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r@   rA   Ú__classcell__r   r   r   r!   r      s(    /ñ$r   z{{base_optimizer_keyword_args}}N)Ú	keras.srcr   Úkeras.src.api_exportr   Úkeras.src.optimizersr   Ú	Optimizerr   rG   ÚreplaceÚbase_optimizer_keyword_argsr   r   r   r!   Ú<module>   s     

ÿ