
    ~Wh                         d Z ddlmc mZ ddlmZ ddlmZ ddl	m
Z
  e             e
ddg            G 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                   X     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 d fd
	Z fdZd Z fdZ x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                      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___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__s                  ]/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/keras/optimizers/adamax.pyr   zAdamax.__init__K   s    " 	 	
%+%$;#	
 	
 	
 	
 	
 #77FF    c                 l   t                                          |           t          | d          r	| j        rdS d| _        g | _        g | _        |D ]`}| j                            |                     |d                     | j                            |                     |d                     ad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#   s      r$   r,   zAdamax.buildm   s     	h4"" 	t{ 	F 
	 
	CGNN00#&c 1    
 GNN00#&c 1     
	 
	r%   c                    t          j        | j        |j                  }t          j        | j        dz   |j                  }t          j        t          j        | j        |j                  |          }|                     |          }| j        | j	        |                  }| j
        | j	        |                  }t          |t           j                  r|j        }	|                    | d| j        z
  z             |                    t          j        |j        d| j        z
  z  |	                     |                    || j        z             t          j        ||	          }
t          j        |
t          j        |j                            |
z
  }|                    t          j        ||	                     |                    ||z  d|z
  || j        z   z  z             dS |                    ||z
  d| j        z
  z             |                    t          j        | j        |z  t          j        |                               |                    ||z  d|z
  || j        z   z  z             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+   r?   u_sliceu_slice_incrementals               r$   update_stepzAdamax.update_step   s<   WT'88WT_q0(.AA
vbgdk8>BBJOO--))GD$W-.GD$W-.h 011 	&GLL!q4;/000MM AO!DgNN   HHQ_%%%i7++G
7BF8?$;$;<<wF   MM"*+>HHIIIaQ-!dl2BCD    
 LL(Q,1t{?;<<<HHRZa1A1ABBCCCaQ-!dl2BCD    r%   c                     t                                                      }|                    |                     | j                  | j        | j        | j        d           |S )N)r!   r   r   r   )r   
get_configupdate_serialize_hyperparameterr   r   r   r   )r    configr#   s     r$   rR   zAdamax.get_config   si    ##%%!%!?!?'" " ++< 		
 		
 		
 r%   )r	   r
   r   r   NNNNFr   NTr   )	__name__
__module____qualname____doc__r   r,   rP   rR   __classcell__)r#   s   @r$   r   r      s        
* *\  $           D    6     D        r%   r   z{{base_optimizer_keyword_args}})rY   tensorflow.compat.v2compatv2r6   keras.optimizersr    keras.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r%   r$   <module>rd      s    ' & ! ! ! ! ! ! ! ! ! & & & & & & H H H H H H : 9 9 9 9 9 *,E"  Y Y Y Y YY  Y Y  Yx ''%y'L r%   