
    ~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
 ddlmZ  edddg	           G d
 dej                              ZdS )zNadam optimizer implementation.    N)backend_config)optimizer_v2)learning_rate_schedule)keras_exportzkeras.optimizers.legacy.Nadamzkeras.optimizers.Nadam)v1c                   b     e Zd ZdZdZ	 	 	 	 	 d fd	Zd Zd	 Z fd
ZddZ	d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 Tensor or a floating point value.  The learning rate.
      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.
      name: Optional name for the operations created when applying gradients.
        Defaults to `"Nadam"`.
      **kwargs: keyword arguments. Allowed arguments are `clipvalue`,
        `clipnorm`, `global_clipnorm`.
        If `clipvalue` (float) is set, the gradient of each weight
        is clipped to be no higher than this value.
        If `clipnorm` (float) is set, the gradient of each weight
        is individually clipped so that its norm is no higher than this value.
        If `global_clipnorm` (float) is set the gradient of all weights is
        clipped so that their global norm is no higher than this value.

    Usage Example:
      >>> opt = tf.keras.optimizers.legacy.Nadam(learning_rate=0.2)
      >>> var1 = tf.Variable(10.0)
      >>> loss = lambda: (var1 ** 2) / 2.0
      >>> step_count = opt.minimize(loss, [var1]).numpy()
      >>> "{:.1f}".format(var1.numpy())
      9.8

    Reference:
      - [Dozat, 2015](http://cs229.stanford.edu/proj2015/054_report.pdf).
    TMbP??+?Hz>c                    |                     dd          |d<   |                    d|          }t          |t          j                  rt          d           t                      j        |fi | |                     d|                    d|                     |                     d| j	                   |                     d|           |                     d|           |pt          j                    | _        d | _        d S )	Nschedule_decaygMbp?decaylrzdThe Nadam optimizer does not support tf.keras.optimizers.LearningRateSchedules as the learning rate.learning_ratebeta_1beta_2)popget
isinstancer   LearningRateSchedule
ValueErrorsuper__init__
_set_hyper_initial_decayr   epsilon_m_cache)selfr   r   r   r   namekwargs	__class__s          c/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/keras/optimizers/legacy/nadam.pyr   zNadam.__init__D   s    !**%5u==w

4771F
 
 	 !   	(((((D-)H)HIII!4555&)))&))):."8":":    c                 @   |d         j         j        }| j        N|                     dg |ddt          j        j                  | _        | j                            | j                   |D ]}| 	                    |d           |D ]}| 	                    |d           d S )Nr   momentum_cacheonesF)shapedtypeinitializer	trainableaggregationmv)
r*   
base_dtyper   
add_weighttfVariableAggregationONLY_FIRST_REPLICA_weightsappendadd_slot)r    var_list	var_dtypevars       r$   _create_slotszNadam._create_slotsa   s    QK%0	=  OO "2E ,  DM M  /// 	$ 	$CMM#s#### 	$ 	$CMM#s####	$ 	$r%   c                    t          j        |                     d|                    }t          j        |                     d|                    }t          j        |                     d|                    }t          j        | j        dz   |          }t          j        | j        dz   |          }t          j        d|          }	|ddt          j        |	| j        |z            z  z
  z  }
|ddt          j        |	| j        |z            z  z
  z  }t          j        | j        |          |
z  }|| j        j	        u rCt          j        t           j
        j                            | j        || j        	                    }||z  }t          || t          j        | j        |          |||
|d|z
  d|z
  d|
z
  d|z
  d|z
  dt          j        ||          z
  
          |||f<   d S )Nr   r   r         gQ?g      ?g      ?use_locking)lr_tneg_lr_tr   beta_1_tbeta_2_tm_tm_t_1one_minus_beta_1_tone_minus_beta_2_tone_minus_m_tone_minus_m_schedule_newone_minus_m_schedule_nextv_t_prime_denominator)r2   identity
_get_hypercast
iterationspowr   _m_cache_readr   r*   compatr   assign_use_lockingdictconvert_to_tensorr   )r    
var_devicer9   apply_staterA   rC   rD   
local_step	next_step
decay_baserE   rF   m_schedule_newm_schedule_nexts                 r$   _prepare_localzNadam._prepare_localu   s   {4???IFFGG;txCCDD;txCCDDWT_q0)<<
GDOa/;;	WT9--
#
D,?*,LMMNN
 #
D,?),KLLMM
 !3Y??#E+++[	##M>t?P $   N
 )50/3U(yAA 8| 8|)%(>%9&)O&;"%x(D(D"D0
 0
 0
Z+,,,r%   c                     t          j        | j                  | _        t	                                          |          S N)r2   rM   r   rR   r   _prepare)r    r8   r#   s     r$   rb   zNadam._prepare   s1      [77ww)))r%   Nc                 8   |j         |j        j        }}|pi                     ||f          p|                     ||          }|                     |d          }|                     |d          }||d         z  }	|d         |z  |d         |z  z   }
t          j        j        	                    ||
| j
                  }
|
|d         z  }|d         |z  |d	         t          j        |          z  z   }t          j        j        	                    ||| j
                  }||d
         z  }|d         |	z  |d         |z  z   }||d         |z  t          j        |          |d         z   z  z
  }t          j        j        	                    ||| j
                  j        S )Nr.   r/   rJ   rC   rG   r?   rK   rD   rH   rL   rI   rF   rA   r   )devicer*   r0   r   _fallback_apply_stateget_slotr2   rS   r   rT   rU   squaresqrtop)r    gradr:   rY   rX   r9   coefficientsr.   r/   g_primerE   	m_t_primev_t	v_t_primem_t_barvar_ts                   r$   _resource_apply_densezNadam._resource_apply_dense   s    #
CI,@I
#)r..#
 
 ?''
I>> 	 MM#s##MM#s##&@AA$q(/0478 	 il!!!Sd6G!HH,'BCC	:&*\ .
IdOO.  il!!!Sd6G!HH,'>??	)G37#i/0 	 l6*W4GIi!88
 
 y|""34;L"MMPPr%   c                 Z   |j         |j        j        }}|pi                     ||f          p|                     ||          }|                     |d          }|                     |d          }	||d         z  }
||d         z  }t          j        j        	                    |||d         z  | j
                  }t          j        |g          5  |                     |||          }t          j        ||          }d d d            n# 1 swxY w Y   ||d         z  }|d         |
z  |d	         |z  z   }||z  |d
         z  }t          j        j        	                    |	|	|d         z  | j
                  }t          j        |g          5  |                     |	||          }t          j        ||          }d d d            n# 1 swxY w Y   ||d         z  }t          j        |          |d         z   }|                     |||d         |z  |z            }t          j        |||g S )Nr.   r/   rJ   rG   rC   r?   rK   rI   rF   rH   rD   rL   r   rB   )rd   r*   r0   r   re   rf   r2   rS   r   rT   rU   control_dependencies_resource_scatter_addgatherrh   group)r    rj   r:   indicesrY   rX   r9   rk   r.   r/   rl   m_scaled_g_valuesrE   	m_t_slicerm   rp   v_scaled_g_valuesrn   	v_t_slicero   v_prime_sqrt_plus_eps
var_updates                         r$   _resource_apply_sparsezNadam._resource_apply_sparse   s    #
CI,@I
#)r..#
 
 ?''
I>> 	 MM#s##MM#s##&@AA !<0D#EEil!!q<
++9J " 
 
 $cU++ 	0 	0,,Q9JKKC	#w//I	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 -H II	)G37#i/0 	 "D[L9M,NNil!!q<
++9J " 
 
 $cU++ 	0 	0,,Q9JKKC	#w//I	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 -D EE	 "	 2 2\)5L L//$w.1FF
 


 x*gs344s$   -DDD-GG
G
c                    t                                                      }|                    |                     d          | j        |                     d          |                     d          | j        d           |S )Nr   r   r   )r   r   r   r   r   )r   
get_configupdate_serialize_hyperparameterr   r   )r    configr#   s     r$   r   zNadam.get_config   s    ##%%!%!?!?#" " ,88BB88BB< 
	
 
	
 
	
 r%   )r
   r   r   r   r	   ra   )__name__
__module____qualname____doc___HAS_AGGREGATE_GRADr   r;   r_   rb   rr   r   r   __classcell__)r#   s   @r$   r	   r	      s        
   D       :$ $ $('
 '
 '
R* * * * *Q Q Q Q<-5 -5 -5 -5^        r%   r	   )r   tensorflow.compat.v2rS   v2r2   kerasr   keras.optimizers.legacyr   keras.optimizers.schedulesr    tensorflow.python.util.tf_exportr   OptimizerV2r	    r%   r$   <module>r      s    & % ! ! ! ! ! ! ! ! !             0 0 0 0 0 0 = = = = = = : 9 9 9 9 9 # "AB  _ _ _ _ _L$ _ _	 _ _ _r%   