
    6bi                         S r SSKrSSKJs  Jr  SSKJr  SSK	J
r
  SSKJr  \" SSS/S9 " S	 S
\
R                  5      5       rg)z!Adagrad optimizer implementation.    N)backend_config)optimizer_v2)keras_exportzkeras.optimizers.legacy.Adagradzkeras.optimizers.Adagrad)v1c                      ^  \ rS rSrSrSr    SU 4S jjrS rU 4S jrU 4S jr	\
SS j5       rSS	 jrSS
 jrU 4S jrSrU =r$ )Adagrad   a  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.
    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.
    Defaults to `0.001`.
  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 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:
  - [Duchi et al., 2011](
    http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf).
Tc                 N  > US:  a  [        SU-  5      eUc  [        R                  " 5       n[        TU ]  " U40 UD6  U R                  SUR                  SU5      5        U R                  SU R                  5        X l        U=(       d    [        R                  " 5       U l        g )Ng        z2initial_accumulator_value must be non-negative: %slearning_ratelrdecay)	
ValueErrorr   epsilonsuper__init__
_set_hyperget_initial_decay_initial_accumulator_value)selfr   initial_accumulator_valuer   namekwargs	__class__s         `/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/optimizers/legacy/adagrad.pyr   Adagrad.__init__E   s     %s*D+,  ?$,,.G((D-)HI!4!45*C':."8"8":    c                     U H^  nUR                   R                  n[        R                  R                  R                  U R                  US9nU R                  USU5        M`     g )Ndtypeaccumulator)r    
base_dtypetfcompatr   constant_initializerr   add_slot)r   var_listvarr    inits        r   _create_slotsAdagrad._create_slotsZ   sU    CII((E99<<44//u 5 D MM#}d3 r   c                    > [         TU ]  XU5        X1U4   R                  [        [        R
                  " U R                  U5      X1U4   S   * [        R                  " S[        R                  S9S95        g )Nlr_t r   )r   neg_lr_tzero)	r   _prepare_localupdatedictr#   convert_to_tensorr   zerosint64)r   
var_device	var_dtypeapply_stater   s       r   r1   Adagrad._prepare_localb   si    zkB+,33,,T\\9E%9&=>vFFXXb1	
r   c                    > U R                   n[        U5      [        U5      S-   :X  a  [        R                  " S5      /U-   n[        TU ]  U5        g )N   r   )weightslennparrayr   set_weights)r   r=   paramsr   s      r   rA   Adagrad.set_weightsl   sD     v;#g,**xx{mg-GG$r   c                 \    SU;  a  SUS'   SU;   a  UR                  S5      US'   U " S0 UD6$ )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_configAdagrad.from_configu   s=    " 'f425F./6>&,jj&6F?#}V}r   c           	      Z   UR                   UR                  R                  pTU=(       d    0 R                  XE45      =(       d    U R	                  XE5      nU R                  US5      n[        R                  R                  UR                  UR                  US   US   UU R                  S9$ )Nr!   r-   r   )r(   accumr   r   graduse_locking)devicer    r"   r   _fallback_apply_stateget_slotr#   raw_opsResourceApplyAdagradV2handle_use_locking)r   rN   r(   r9   r7   r8   coefficientsaccs           r   _resource_apply_denseAdagrad._resource_apply_dense   s     #

CII,@,@I#)r..#
 ?''
> 	 mmC/zz00

**F# +)) 1 
 	
r   c           
      \   UR                   UR                  R                  peU=(       d    0 R                  XV45      =(       d    U R	                  XV5      nU R                  US5      n[        R                  R                  UR                  UR                  US   US   UUU R                  S9$ )Nr!   r-   r   )r(   rM   r   r   rN   indicesrO   )rP   r    r"   r   rQ   rR   r#   rS   ResourceSparseApplyAdagradV2rU   rV   )	r   rN   r(   r\   r9   r7   r8   rW   rX   s	            r   _resource_apply_sparseAdagrad._resource_apply_sparse   s     #

CII,@,@I#)r..#
 ?''
> 	 mmC/zz66

**F# +)) 7 
 	
r   c                    > [         TU ]  5       nUR                  U R                  S5      U R                  U R
                  U R                  S.5        U$ )Nr   )r   r   r   r   )r   
get_configr2   _serialize_hyperparameterr   r   r   )r   rH   r   s     r   ra   Adagrad.get_config   sT    #%!%!?!?#" ,,-1-L-L<<		
 r   )r   r   )gMbP?rE   gHz>r   )N)__name__
__module____qualname____firstlineno____doc___HAS_AGGREGATE_GRADr   r*   r1   rA   classmethodrJ   rY   r^   ra   __static_attributes____classcell__)r   s   @r   r   r      s[    
!F  "%;*4
%  ,
 
" r   r   )rh   numpyr?   tensorflow.compat.v2r$   v2r#   tf_keras.srcr   tf_keras.src.optimizers.legacyr    tensorflow.python.util.tf_exportr   OptimizerV2r   r.   r   r   <module>rt      sS    (  ! ! ' 7 : %"$EFZl&& Z	Zr   