
    6bi                         S r SSKJs  Jr  SSKJr  SSKJr  SSK	J
r
  \" 5       \
" SS/ S9 " S	 S
\R                  5      5       5       r\R                   R                  S\R                  5      \l         g)z Adamax optimizer implementation.    N)	optimizer)register_keras_serializable)keras_exportz$keras.optimizers.experimental.Adamaxzkeras.optimizers.Adamax)v1c                   h   ^  \ rS rSrSr             SU 4S jjrU 4S jrS rU 4S jrSr	U =r
$ )	Adamax   a1  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)
c                    > [         TU ]  " SUUUUUU	U
UUS.	UD6  U R                  U5      U l        X l        X0l        X@l        g )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                  X/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/optimizers/adamax.pyr   Adamax.__init__K   s`    " 	 	
%+%$;#	
 	
 #77F    c                 J  > [         TU ]  U5        [        U S5      (       a  U R                  (       a  gSU l        / U l        / U l        U HU  nU R                  R                  U R                  USS95        U R
                  R                  U R                  USS95        MW     g)zInitialize 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)   Adamax.buildm   s     	h4""t{{CGGNN00#&c 1 
 GGNN00#&c 1  r"   c                 z   [         R                  " U R                  UR                  5      n[         R                  " U R                  S-   UR                  5      n[         R
                  " [         R                  " U R                  UR                  5      U5      nU R                  U5      nU R                  U R                  U      nU R                  U R                  U      n[        U[         R                  5      (       Ga'  UR                  n	UR                  U* SU R                  -
  -  5        UR                  [         R                  " UR                   SU R                  -
  -  U	5      5        UR#                  XR$                  -  5        [         R&                  " X5      n
[         R(                  " U
[         R*                  " UR                   5      5      U
-
  nUR                  [         R                  " X5      5        UR-                  X7-  SU-
  XR.                  -   -  -  5        gUR                  X-
  SU R                  -
  -  5        UR#                  [         R(                  " U R$                  U-  [         R*                  " U5      5      5        UR-                  X7-  SU-
  XR.                  -   -  -  5        g)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_stepAdamax.update_step   s   WWT''8WWT__q0(..A
vvbggdkk8>>BJO--)GGD$$W-.GGD$$W-.h 0 011&&GLL!q4;;/0MM  AO!DgN HHQ_%ii+G

7BFF8??$;<wF   MM"**+>HIQ-!ll2BCD
 LL(,1t{{?;<HHRZZa1ABCQ-!ll2BCDr"   c                    > [         TU ]  5       nUR                  U R                  U R                  5      U R
                  U R                  U R                  S.5        U$ )N)r   r   r   r   )r   
get_configupdate_serialize_hyperparameterr   r   r   r   )r   configr   s     r    rQ   Adamax.get_config   sV    #%!%!?!?''" ++++<<		
 r"   )r$   r   r+   r,   r   r   r   )gMbP?g?g+?gHz>NNNNFgGz?NTr   )__name__
__module____qualname____firstlineno____doc__r   r)   rN   rQ   __static_attributes____classcell__)r   s   @r    r   r      sM    
*\  $ D6 D r"   r   z{{base_optimizer_keyword_args}})rZ   tensorflow.compat.v2compatv2r4   tf_keras.src.optimizersr   'tf_keras.src.saving.object_registrationr    tensorflow.python.util.tf_exportr   	Optimizerr   replacebase_optimizer_keyword_argsr   r"   r    <module>rf      s{    ' ! ! - O : *,E"YY   Y Yx ''%y'L'Lr"   