B
    ӻd\                 @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
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!Adagrad optimizer implementation.    N)dtypes)ops)backend_config)optimizer_v2)	array_ops)init_ops)gen_training_ops)keras_exportzkeras.optimizers.Adagradc                   st   e Zd ZdZdZd fdd	Zdd	 Z fd
dZ fddZe	dddZ
dddZdddZ fddZ  ZS )Adagrada  Optimizer that implements the Adagrad algorithm.

  Adagrad is an optimizer with parameter-specific learning rates,
  which are adapted relative to how frequently a parameter gets
  updated during training. The more updates a parameter receives,
  the smaller the updates.

  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 `Adagrad` 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.
    initial_accumulator_value: Floating point value.
      Starting value for the accumulators (per-parameter momentum values).
      Must be non-negative.
    epsilon: Small floating point value used to maintain numerical stability.
    name: Optional name prefix for the operations created when applying
      gradients.  Defaults to `"Adagrad"`.
    **kwargs: Keyword arguments. Allowed to be one of
      `"clipnorm"` or `"clipvalue"`.
      `"clipnorm"` (float) clips gradients by norm and represents
      the maximum L2 norm of each weight variable;
      `"clipvalue"` (float) clips gradient by value and represents the
      maximum absolute value of each weight variable.

  Reference:
    - [Duchi et al., 2011](
      http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf).
  TMbP?皙?Hz>c                sr   |dk rt d| |d kr$t }tt| j|f| | d|d| | d| j || _	|pjt | _d S )Ng        z2initial_accumulator_value must be non-negative: %slearning_ratelrdecay)

ValueErrorr   epsilonsuperr
   __init__Z
_set_hyperget_initial_decay_initial_accumulator_value)selfr   initial_accumulator_valuer   namekwargs)	__class__ ^/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/keras/optimizer_v2/adagrad.pyr   C   s    zAdagrad.__init__c             C   s8   x2|D ]*}|j j}tj| j|d}| |d| qW d S )N)dtypeaccumulator)r   
base_dtyper   Zconstant_initializerr   Zadd_slot)r   Zvar_listvarr   initr   r   r   _create_slotsT   s
    
zAdagrad._create_slotsc          	      sX   t t| ||| |||f tt| j||||f d  tj	dt
jdd d S )Nlr_tr   )r   )r   Zneg_lr_tzero)r   r
   _prepare_localupdatedictr   Z"convert_to_tensor_v2_with_dispatchr   r   Zzerosr   Zint64)r   
var_device	var_dtypeapply_state)r   r   r   r'   [   s    zAdagrad._prepare_localc                s>   | j }t|t|d kr*tdg| }tt| | d S )N   r   )weightslennparrayr   r
   set_weights)r   r.   params)r   r   r   r2   d   s    zAdagrad.set_weightsNc             C   s0   d|krd|d< d|kr&| d|d< | f |S )a  Creates an optimizer from its config.

    This method is the reverse of `get_config`,
    capable of instantiating the same optimizer from the config
    dictionary.

    Args:
        config: A Python dictionary, typically the output of get_config.
        custom_objects: A Python dictionary mapping names to additional Python
          objects used to create this optimizer, such as a function used for a
          hyperparameter.

    Returns:
        An optimizer instance.
    r   g?r   r   )pop)clsconfigZcustom_objectsr   r   r   from_configm   s
    zAdagrad.from_configc             C   s^   |j |jj }}|pi ||fp,| ||}| |d}tj|j|j|d |d || j	dS )Nr    r%   r   )r"   accumr   r   graduse_locking)
devicer   r!   r   _fallback_apply_stateget_slotr   ZResourceApplyAdagradV2handle_use_locking)r   r9   r"   r,   r*   r+   coefficientsaccr   r   r   _resource_apply_dense   s    zAdagrad._resource_apply_densec       	   	   C   s`   |j |jj }}|pi ||fp,| ||}| |d}tj|j|j|d |d ||| j	dS )Nr    r%   r   )r"   r8   r   r   r9   indicesr:   )
r;   r   r!   r   r<   r=   r   ZResourceSparseApplyAdagradV2r>   r?   )	r   r9   r"   rC   r,   r*   r+   r@   rA   r   r   r   _resource_apply_sparse   s    zAdagrad._resource_apply_sparsec                s2   t t|  }|| d| j| j| jd |S )Nr   )r   r   r   r   )r   r
   
get_configr(   Z_serialize_hyperparameterr   r   r   )r   r6   )r   r   r   rE      s    zAdagrad.get_config)r   r   r   r
   )N)N)N)__name__
__module____qualname____doc__Z_HAS_AGGREGATE_GRADr   r$   r'   r2   classmethodr7   rB   rD   rE   __classcell__r   r   )r   r   r
      s   !   		

r
   )rI   numpyr0   Ztensorflow.python.frameworkr   r   Ztensorflow.python.kerasr   Z$tensorflow.python.keras.optimizer_v2r   Ztensorflow.python.opsr   r   Ztensorflow.python.trainingr   Z tensorflow.python.util.tf_exportr	   ZOptimizerV2r
   r   r   r   r   <module>   s   