
    2Vh_"                         d dl mZ d dl mZ d dlmZ d dlmZ  edg       G d dej                               Zej                  j                  dej                        e_	        y	)
    )initializers)ops)keras_export)	optimizerzkeras.optimizers.Ftrlc                   `     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z fdZd Z fdZ xZS )Ftrla
  Optimizer that implements the FTRL algorithm.

    "Follow The Regularized Leader" (FTRL) is an optimization algorithm
    developed at Google for click-through rate prediction in the early 2010s. It
    is most suitable for shallow models with large and sparse feature spaces.
    The algorithm is described by
    [McMahan et al., 2013](https://research.google.com/pubs/archive/41159.pdf).
    The Keras version has support for both online L2 regularization
    (the L2 regularization described in the paper
    above) and shrinkage-type L2 regularization
    (which is the addition of an L2 penalty to the loss function).

    Initialization:

    ```python
    n = 0
    sigma = 0
    z = 0
    ```

    Update rule for one variable `w`:

    ```python
    prev_n = n
    n = n + g ** 2
    sigma = (n ** -lr_power - prev_n ** -lr_power) / lr
    z = z + g - sigma * w
    if abs(z) < lambda_1:
      w = 0
    else:
      w = (sgn(z) * lambda_1 - z) / ((beta + sqrt(n)) / alpha + lambda_2)
    ```

    Notation:

    - `lr` is the learning rate
    - `g` is the gradient for the variable
    - `lambda_1` is the L1 regularization strength
    - `lambda_2` is the L2 regularization strength
    - `lr_power` is the power to scale n.

    Check the documentation for the `l2_shrinkage_regularization_strength`
    parameter for more details when shrinkage is enabled, in which case gradient
    is replaced with a gradient with shrinkage.

    Args:
        learning_rate: A float, a
            `keras.optimizers.schedules.LearningRateSchedule` instance, or
            a callable that takes no arguments and returns the actual value to
            use. The learning rate. Defaults to `0.001`.
        learning_rate_power: A float value, must be less or equal to zero.
            Controls how the learning rate decreases during training. Use zero
            for a fixed learning rate.
        initial_accumulator_value: The starting value for accumulators. Only
            zero or positive values are allowed.
        l1_regularization_strength: A float value, must be greater than or equal
            to zero. Defaults to `0.0`.
        l2_regularization_strength: A float value, must be greater than or equal
            to zero. Defaults to `0.0`.
        l2_shrinkage_regularization_strength: A float value, must be greater
            than or equal to zero. This differs from L2 above in that the L2
            above is a stabilization penalty, whereas this L2 shrinkage is a
            magnitude penalty. When input is sparse shrinkage will only happen
            on the active weights.
        beta: A float value, representing the beta value from the paper.
            Defaults to `0.0`.
        {{base_optimizer_keyword_args}}
    c                 X   t        |   d	||||	|
||||||d| |dk  rt        d| d      |dkD  rt        d| d      |dk  rt        d| d      |dk  rt        d| d      |dk  rt        d| d      || _        || _        || _        || _        || _        || _        y )
N)learning_ratenameweight_decayclipnorm	clipvalueglobal_clipnormuse_emaema_momentumema_overwrite_frequencyloss_scale_factorgradient_accumulation_steps        z^`initial_accumulator_value` needs to be positive or zero. Received: initial_accumulator_value=.zR`learning_rate_power` needs to be negative or zero. Received: learning_rate_power=z``l1_regularization_strength` needs to be positive or zero. Received: l1_regularization_strength=z``l2_regularization_strength` needs to be positive or zero. Received: l2_regularization_strength=zt`l2_shrinkage_regularization_strength` needs to be positive or zero. Received: l2_shrinkage_regularization_strength= )	super__init__
ValueErrorlearning_rate_powerinitial_accumulator_valuel1_regularization_strengthl2_regularization_strength$l2_shrinkage_regularization_strengthbeta)selfr
   r   r   r   r   r   r    r   r   r   r   r   r   r   r   r   r   kwargs	__class__s                      I/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/optimizers/ftrl.pyr   zFtrl.__init__N   sE   * 	 	
'%+%$;/(C	
 	
 %s*7,-Q0 
 $'':&;1>  &+8-.a1 
 &+8-.a1 
 0#589<  $7 )B&*D'*D'0 	1 	    c                     | j                   ryt        | 	  |       t        j                  | j
                        }| j                  |ddg|dg      \  | _        | _        y)zInitialize optimizer variables.

        Args:
            var_list: list of model variables to build Ftrl variables on.
        Naccumulatorlinearzeros)initializer)	builtr   buildr   Constantr   add_optimizer_variables_accumulators_linears)r!   var_listaccumulator_initializerr#   s      r$   r,   z
Ftrl.build   sj     ::h"."7"7**#
 -1,H,HH%0': -I -
)DMr%   c                    t        j                  ||j                        }t        j                  ||j                        }| j                  | j	                  |         }| j
                  | j	                  |         }| j                  }| j                  }|| j                  d|z  z  z   }t        j                  |t        j                  d| j                  z  |            }	t        j                  |t        j                  |            }
| j                  |t        j                  |	t        j                  t        j                  t        j                  t        j                   |
|       t        j                   ||             |      |                   t        j                  t        j                  t        j                   |
|       |      d|z        }t        j"                  || j$                   | j$                        }| j'                  |t        j                  t        j                  ||      |             | j'                  ||
       y)z=Update step given gradient and the associated model variable.g       @   N)r   castdtyper/   _get_variable_indexr0   r   r   r    addmultiplyr   square
assign_addsubtractdividepowerclipr   assign)r!   gradientvariabler
   lraccumr(   lr_powerl2_reggrad_to_use	new_accum	quadraticlinear_clippeds                r$   update_stepzFtrl.update_step   s    XXmX^^488Hhnn5""4#;#;H#EFt77AB++00$))sRx00ggLLD===x
 GGE3::h#78	LLJJIIi(;IIehY7  		
  GGJJsyyhY8"=q6z
	 ,,,++

 	JJs||NF;YG	
 	E9%r%   c           	          t         |          }|j                  | j                  | j                  | j
                  | j                  | j                  | j                  d       |S )N)r   r   r   r   r   r    )	r   
get_configupdater   r   r   r   r   r    )r!   configr#   s     r$   rM   zFtrl.get_config   s\    #%'+'?'?-1-K-K.2.M.M.2.M.M8<8a8a				
 r%   )gMbP?g      g?r   r   r   r   NNNNFgGz?NNNftrl)	__name__
__module____qualname____doc__r   r,   rK   rM   __classcell__)r#   s   @r$   r   r      s[    CN  "%#&#&-0 $$(%IV
$0&d r%   r   z{{base_optimizer_keyword_args}}N)	keras.srcr   r   keras.src.api_exportr   keras.src.optimizersr   	Optimizerr   rT   replacebase_optimizer_keyword_argsr   r%   r$   <module>r\      sb    "  - * &'(b9 b )bJ ||##%y'L'Lr%   