
    &Vf&                         d dl mZ d dl mZ d dlmZ d dlmZ  edg           G d dej                              Zej	        
                    dej                  e_	        d	S )
    )backend)ops)keras_export)	optimizerzkeras.optimizers.Nadamc                   d     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd
	Z fdZ fdZd Z fdZ xZ	S )Nadama  Optimizer that implements the Nadam algorithm.

    Much like Adam is essentially RMSprop with momentum, Nadam is Adam with
    Nesterov momentum.

    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, or a callable
            that takes no arguments and returns the actual value to use. The
            exponential decay rate for the 1st moment estimates.
            Defaults to `0.9`.
        beta_2: A float value or a constant float tensor, or a callable
            that takes no arguments and returns the actual value to use. The
            exponential decay rate for the 2nd moment estimates. Defaults to
            `0.999`.
        epsilon: A small constant for numerical stability. This epsilon is
            "epsilon hat" in the Kingma and Ba paper (in the formula just before
            Section 2.1), not the epsilon in Algorithm 1 of the paper.
            Defaults to `1e-7`.
        {{base_optimizer_keyword_args}}

    Reference:

    - [Dozat, 2015](http://cs229.stanford.edu/proj2015/054_report.pdf).

    MbP??+?Hz>NFGz?nadamc                 |     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__s                   W/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/optimizers/nadam.pyr   zNadam.__init__'   sp    $ 	 	
'%+%$;/(C	
 	
 	
 	
 	
     c                    | j         rdS |r|d         j        }nt          j                    }t	                                          |           g | _        g | _        t          j        d|          | _	        |D ]`}| j        
                    |                     |d                     | j        
                    |                     |d                     adS )zInitialize optimizer variables.

        Nadam optimizer has 2 types of variables: momentums and velocities.

        Args:
            var_list: list of model variables to build Nadam variables on.
        Nr         ?)dtypemomentum)reference_variabler   velocity)builtr(   r   floatxr   build
_momentums_velocitiesVariable
_u_productappendadd_variable_from_reference)r!   var_listr(   varr#   s       r$   r.   zNadam.buildK   s    : 	F 	%QK%EEN$$Eh!*3e<<< 
	 
	CO""00'* 1    
 ##00'* 1     
	 
	r%   c                 $   | j         j        }|                     | j         | j         | j        z  ddt	          j        dt	          j        | j        dz   |                    z  z
  z             t                      	                    |||           d S )Nr'         ?Q?   )
r2   r(   assignr   r   powercast
iterationsr   _backend_update_step)r!   gradstrainable_variablesr   r(   r#   s        r$   r?   zNadam._backend_update_stepj   s    %OOk 	$11De(L(LMMMN	
 	
 	
 	$$U,?OOOOOr%   c                    |j         }t          j        ||          }t          j        ||          }t          j        | j        dz   |          }t          j        | j        dz   |          }t          j        d|          }t          j        | j        |          }	t          j        | j        |          }
|	ddt          j        ||          z  z
  z  }|	ddt          j        ||          z  z
  z  }t          j        | j        |          }||z  }t          j        |
|          }| j        | 	                    |                   }| j
        | 	                    |                   }|                     |t          j        t          j        ||          d|	z
                       |                     |t          j        t          j        t          j        |          |          d|
z
                       t          j        t          j        t          j        ||          d|z
            t          j        t          j        d|z
  |          d|z
                      }t          j        |d|z
            }|                     |t          j        t          j        ||          t          j        t          j        |          | j                                       dS )z=Update step given gradient and the associated model variable.r:      r9   r'   r8   N)r(   r   r=   r>   r   r   r<   r2   r/   _get_variable_indexr0   
assign_addmultiplysubtractsquareadddivide
assign_subsqrtr    )r!   gradientvariabler   	var_dtypelr
local_step	next_stepdecayr   r   u_tu_t_1u_product_tu_product_t_1beta_2_powermvm_hatv_hats                       r$   update_stepzNadam.update_stepw   sv   N	XmY//8Hi00Xdo19==
HT_q0)<<	y))$+y11$+y11cSYuj%A%ABBC#sy	'B'B CCDht	::#e+y44OD44X>>?T55h??@s|CL155F
DD	
 	
 	
 	s|CLH)=)=qAAAJPP	
 	
 	
 Js|E1--q=/@AAJs|AGX66KHH
 
 
1q</11JUB''%$,)O)O 	
 	
 	
 	
 	
r%   c                     t                                                      }|                    | j        | j        | j        d           |S )N)r   r   r    )r   
get_configupdater   r   r    )r!   configr#   s     r$   r_   zNadam.get_config   sN    ##%%++< 	
 	
 	
 r%   )r	   r
   r   r   NNNNFr   NNNr   )
__name__
__module____qualname____doc__r   r.   r?   r]   r_   __classcell__)r#   s   @r$   r   r      s         @  $$(" " " " " "H    >P P P P P&
 &
 &
P
 
 
 
 
 
 
 
 
r%   r   z{{base_optimizer_keyword_args}}N)	keras.srcr   r   keras.src.api_exportr   keras.src.optimizersr   	Optimizerr   re   replacebase_optimizer_keyword_argsr   r%   r$   <module>rm      s                - - - - - - * * * * * * '())a a a a aI a a *)aH %%%y'L r%   