o
    i e'                     @   sl   d Z ddlm  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d	G d
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                       sl   e Zd ZdZdZ					 d fdd	Zd	d
 Zdd Z fddZdddZ	dddZ
 fddZ  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                    s   | dd|d< |d|}t|tjrtdt j|fi | | d|d| | d| j	 | d| | d| |pDt
 | _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__ P/var/www/myenv/lib/python3.10/site-packages/keras/src/optimizers/legacy/nadam.pyr   D   s   

zNadam.__init__c                 C   sp   |d j j}| jd u r | jdg |ddtjjd| _| j| j |D ]}| 	|d q"|D ]}| 	|d q-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varr"   r"   r#   _create_slotsa   s    
zNadam._create_slotsc                 C   s@  t | d|}t | d|}t | d|}t | jd |}t | jd |}t d|}	|ddt |	| j|    }
|ddt |	| j|    }t | j||
 }|| jj	u rot t j
jj| j|| jd	}|| }t|| t | j||||
|d| d| d|
 d| d| dt || d
|||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)r/   identity
_get_hypercast
iterationspowr   _m_cache_readr   r'   compatr   assign_use_lockingdictconvert_to_tensorr   )r   
var_devicer6   apply_stater=   r?   r@   
local_step	next_step
decay_baserA   rB   m_schedule_newm_schedule_nextr"   r"   r#   _prepare_localu   sF   
zNadam._prepare_localc                    s   t | j| _t |S N)r/   rI   r   rN   r   _prepare)r   r5   r    r"   r#   r]      s   zNadam._prepareNc                 C   s  |j |jj}}|pi ||fp| ||}| |d}| |d}||d  }	|d | |d |  }
tjjj	||
| j
d}
|
|d  }|d | |d	 t|  }tjjj	||| j
d}||d
  }|d |	 |d |  }||d | t||d    }tjjj	||| j
djS )Nr+   r,   rF   r?   rC   r;   rG   r@   rD   rH   rE   rB   r=   r   )devicer'   r-   r   _fallback_apply_stateget_slotr/   rO   r   rP   rQ   squaresqrtop)r   gradr7   rU   rT   r6   coefficientsr+   r,   g_primerA   	m_t_primev_t	v_t_primem_t_barvar_tr"   r"   r#   _resource_apply_dense   s<   




zNadam._resource_apply_densec                 C   s  |j |jj}}|pi ||fp| ||}| |d}| |d}	||d  }
||d  }tjjj	|||d  | j
d}t|g | |||}t||}W d    n1 s\w   Y  ||d  }|d |
 |d	 |  }|| |d
  }tjjj	|	|	|d  | j
d}t|g | |	||}t||}W d    n1 sw   Y  ||d  }t||d  }| |||d | | }tj|||g S )Nr+   r,   rF   rC   r?   r;   rG   rE   rB   rD   r@   rH   r   r>   )r^   r'   r-   r   r_   r`   r/   rO   r   rP   rQ   control_dependencies_resource_scatter_addgatherrb   group)r   rd   r7   indicesrU   rT   r6   re   r+   r,   rf   m_scaled_g_valuesrA   	m_t_slicerg   rj   v_scaled_g_valuesrh   	v_t_sliceri   v_prime_sqrt_plus_eps
var_updater"   r"   r#   _resource_apply_sparse   sL   


zNadam._resource_apply_sparsec                    s:   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    r"   r#   ry      s   
zNadam.get_config)r   r	   r
   r   r   r\   )__name__
__module____qualname____doc___HAS_AGGREGATE_GRADr   r8   r[   r]   rl   rx   ry   __classcell__r"   r"   r    r#   r      s    ")

/r   )r   tensorflow.compat.v2rO   v2r/   	keras.srcr   keras.src.optimizers.legacyr   keras.src.optimizers.schedulesr    tensorflow.python.util.tf_exportr   OptimizerV2r   r"   r"   r"   r#   <module>   s   