
    BVh+                         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  G d
 de	j                        Zy)z!Adagrad optimizer implementation.    N)dtypes)tensor_conversion)backend_config)optimizer_v2)	array_ops)gen_training_ops)init_opsc                   v     e Zd ZdZdZ	 	 	 	 d fd	Zd Z fdZ fdZe	dd       Z
ddZdd	Z fd
Z x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).
  Tc                 D   |dk  rt        d|z        |t        j                         }t        t        |   |fi | | j                  d|j                  d|             | j                  d| j                         || _	        |xs t        j                         | _        y )Ng        z2initial_accumulator_value must be non-negative: %slearning_ratelrdecay)

ValueErrorr   epsilonsuperr   __init__
_set_hyperget_initial_decay_initial_accumulator_value)selfr   initial_accumulator_valuer   namekwargs	__class__s         \/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/keras/optimizer_v2/adagrad.pyr   zAdagrad.__init__A   s     !3&K01 2 2&&(g	'4!$1&1OOOVZZm%DEOOGT001&?D#6n446DL    c                     |D ]L  }|j                   j                  }t        j                  | j                  |      }| j                  |d|       N y )Ndtypeaccumulator)r!   
base_dtyper	   constant_initializerr   add_slot)r   var_listvarr!   inits        r   _create_slotszAdagrad._create_slotsR   sK     .ii""e**

)
)8d
mmC-	.r   c           
         t         t        |   |||       |||f   j                  t	        t        j                  | j                  |      |||f   d    t        j                  dt        j                                     y )Nlr_t r    )r   neg_lr_tzero)r   r   _prepare_localupdatedictr   "convert_to_tensor_v2_with_dispatchr   r   zerosr   int64)r   
var_device	var_dtypeapply_stater   s       r   r/   zAdagrad._prepare_localY   sv    	'4'
I{KY'(//%HHi ":y"9:6BB6<<8	
r   c                     | j                   }t        |      t        |      dz   k(  rt        j                  d      g|z   }t        t
        |   |       y )N   r   )weightslennparrayr   r   set_weights)r   r:   paramsr   s      r   r>   zAdagrad.set_weightse   sF    \\F 6{c'lQ&&!'g	'4$W-r   c                 T    d|vrd|d<   d|v r|j                  d      |d<    | di |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   皙?r   r   r,   )pop)clsconfigcustom_objectss      r   from_configzAdagrad.from_confign   s=    " #&0,/f()v~ &

4 0f_==r   c                 >   |j                   |j                  j                  }}|xs i j                  ||f      xs | j	                  ||      }| j                  |d      }t        j                  |j                  |j                  |d   |d   || j                        S )Nr"   r+   r   )r'   accumr   r   graduse_locking)
devicer!   r#   r   _fallback_apply_stateget_slotr   ResourceApplyAdagradV2handle_use_locking)r   rI   r'   r7   r5   r6   coefficientsaccs           r   _resource_apply_densezAdagrad._resource_apply_dense   s    JJ		(<(<	J &B++Z,CD I11*iH  --]
+C22JJjjY'%%' 'r   c           	      @   |j                   |j                  j                  }}|xs i j                  ||f      xs | j	                  ||      }| j                  |d      }t        j                  |j                  |j                  |d   |d   ||| j                        S )Nr"   r+   r   )r'   rH   r   r   rI   indicesrJ   )
rK   r!   r#   r   rL   rM   r   ResourceSparseApplyAdagradV2rO   rP   )	r   rI   r'   rU   r7   r5   r6   rQ   rR   s	            r   _resource_apply_sparsezAdagrad._resource_apply_sparse   s    JJ		(<(<	J &B++Z,CD I11*iH  --]
+C88JJjjY'%%' 'r   c                     t         t        |          }|j                  | j	                  d      | j
                  | j                  | j                  d       |S )Nr   )r   r   r   r   )r   r   
get_configr0   _serialize_hyperparameterr   r   r   )r   rD   r   s     r   rY   zAdagrad.get_config   sQ    7D,.F
MM77H$$%)%D%D<<	  Mr   )gMbP?rA   gHz>r   )N)__name__
__module____qualname____doc___HAS_AGGREGATE_GRADr   r)   r/   r>   classmethodrF   rS   rW   rY   __classcell__)r   s   @r   r   r      s[    B  #),	7".
.  ,'' r   r   )r^   numpyr<   tensorflow.python.frameworkr   r   tensorflow.python.kerasr   $tensorflow.python.keras.optimizer_v2r   tensorflow.python.opsr   r   r	   OptimizerV2r   r,   r   r   <module>rh      s8    (  . 9 2 = + 2 *Ml&& Mr   