a
    SicS                     @   s`   d Z ddlZddlm  mZ ddlmZ ddl	m
Z
 ddlmZ edG dd de
jZdS )	z"Adadelta optimizer implementation.    N)backend_config)optimizer_v2)keras_exportzkeras.optimizers.Adadeltac                       sf   e Zd ZdZdZd fdd	Zdd	 Z fd
dZ fddZdddZ	dddZ
 fddZ  ZS )Adadeltaa  Optimizer that implements the Adadelta algorithm.

    Adadelta optimization is a stochastic gradient descent method that is based
    on adaptive learning rate per dimension to address two drawbacks:

    - The continual decay of learning rates throughout training.
    - The need for a manually selected global learning rate.

    Adadelta is a more robust extension of Adagrad that adapts learning rates
    based on a moving window of gradient updates, instead of accumulating all
    past gradients. This way, Adadelta continues learning even when many updates
    have been done. Compared to Adagrad, in the original version of Adadelta you
    don't have to set an initial learning rate. In this version, the initial
    learning rate can be set, as in most other Keras optimizers.

    Args:
      learning_rate: Initial value for the learning rate:
        either a floating point value,
        or a `tf.keras.optimizers.schedules.LearningRateSchedule` instance.
        Defaults to 0.001.
        Note that `Adadelta` tends to benefit from higher initial learning rate
        values compared to other optimizers.
        To match the exact form in the original paper, use 1.0.
      rho: A `Tensor` or a floating point value. The decay rate.
      epsilon: Small floating point value used to maintain numerical stability.
      name: Optional name prefix for the operations created when applying
        gradients.  Defaults to `"Adadelta"`.
      **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.

    Reference:
      - [Zeiler, 2012](http://arxiv.org/abs/1212.5701)
    TMbP?ffffff?Hz>c                    sT   t  j|fi | | d|d| | d| j | d| |pLt | _d S )Nlearning_ratelrdecayrho)super__init__
_set_hyperget_initial_decayr   epsilon)selfr	   r   r   namekwargs	__class__ b/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/optimizers/optimizer_v2/adadelta.pyr   G   s
    zAdadelta.__init__c                 C   s0   |D ]}|  |d q|D ]}|  |d qd S )N
accum_grad	accum_var)add_slot)r   var_listvr   r   r   _create_slotsU   s    zAdadelta._create_slotsc              
      sF   t  ||| |||f tt| j|t| d|d d S )Nr   )r   r   )	r   _prepare_localupdatedicttfconvert_to_tensorr   identity
_get_hyper)r   
var_device	var_dtypeapply_stater   r   r   r    \   s    zAdadelta._prepare_localc                    s:   | j }t|t|d kr*tdg| }t | d S )N   r   )weightslennparrayr   set_weights)r   r+   paramsr   r   r   r/   e   s    zAdadelta.set_weightsNc           	   
   C   sv   |j |jj }}|pi ||fp,| ||}| |d}| |d}tjj|j	|j	|j	|d |d |d || j
dS )Nr   r   lr_tr   r   )varaccumaccum_updater
   r   r   graduse_locking)devicedtype
base_dtyper   _fallback_apply_stateget_slotr#   raw_opsResourceApplyAdadeltahandle_use_locking)	r   r5   r2   r)   r'   r(   coefficientsr   r   r   r   r   _resource_apply_densen   s$    
zAdadelta._resource_apply_densec           
      C   sx   |j |jj }}|pi ||fp,| ||}| |d}| |d}	tjj|j	|j	|	j	|d |d |d ||| j
d	S )Nr   r   r1   r   r   )	r2   r3   r4   r
   r   r   r5   indicesr6   )r7   r8   r9   r   r:   r;   r#   r<   ResourceSparseApplyAdadeltar>   r?   )
r   r5   r2   rB   r)   r'   r(   r@   r   r   r   r   r   _resource_apply_sparse   s&    
zAdadelta._resource_apply_sparsec                    s2   t   }|| d| j| d| jd |S )Nr	   r   )r	   r   r   r   )r   
get_configr!   _serialize_hyperparameterr   r   )r   configr   r   r   rE      s    

zAdadelta.get_config)r   r   r   r   )N)N)__name__
__module____qualname____doc___HAS_AGGREGATE_GRADr   r   r    r/   rA   rD   rE   __classcell__r   r   r   r   r      s   (    		

r   )rK   numpyr-   tensorflow.compat.v2compatv2r#   kerasr   keras.optimizers.optimizer_v2r    tensorflow.python.util.tf_exportr   OptimizerV2r   r   r   r   r   <module>   s   