
    BVh4                         d 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
       G d de	j                               Zy)zFtrl-proximal for TensorFlow.    )dtypes)ops)	array_ops)gen_training_ops)math_ops)	optimizer)	tf_exportztrain.FtrlOptimizer)v1c                   \     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d	 fd	Zd Zd Zd Zd Zd Z	d Z
 xZS )
FtrlOptimizera  Optimizer that implements the FTRL algorithm.

  This version has support for both online L2 (McMahan et al., 2013) and
  shrinkage-type L2, which is the addition of an L2 penalty
  to the loss function.

  References:
    Ad-click prediction:
      [McMahan et al., 2013](https://dl.acm.org/citation.cfm?id=2488200)
      ([pdf](https://dl.acm.org/ft_gateway.cfm?id=2488200&ftid=1388399&dwn=1&CFID=32233078&CFTOKEN=d60fe57a294c056a-CB75C374-F915-E7A6-1573FBBC7BF7D526))
  c                    t         t        |   ||       |dk  rt        d|z        |dkD  rt        d|z        |dk  rt        d|z        |dk  rt        d|z        |
dk  rt        d|
z        || _        || _        || _        || _        || _        |dn|| _	        |
| _
        d| _        d| _        d| _        d| _        d| _        || _        |	| _        y)a9	  Construct a new FTRL optimizer.

    Args:
      learning_rate: A float value or a constant float `Tensor`.
      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. See section 3.1 in (McMahan et al., 2013).
      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.
      l2_regularization_strength: A float value, must be greater than or
        equal to zero.
      use_locking: If `True` use locks for update operations.
      name: Optional name prefix for the operations created when applying
        gradients.  Defaults to "Ftrl".
      accum_name: The suffix for the variable that keeps the gradient squared
        accumulator.  If not present, defaults to name.
      linear_name: The suffix for the variable that keeps the linear gradient
        accumulator.  If not present, defaults to name + "_1".
      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.
        The FTRL formulation can be written as:
        w_{t+1} = argmin_w(\hat{g}_{1:t}w + L1*||w||_1 + L2*||w||_2^2), where
        \hat{g} = g + (2*L2_shrinkage*w), and g is the gradient of the loss
        function w.r.t. the weights w.
        Specifically, in the absence of L1 regularization, it is equivalent to
        the following update rule:
        w_{t+1} = w_t - lr_t / (beta + 2*L2*lr_t) * g_t -
                  2*L2_shrinkage*lr_t / (beta + 2*L2*lr_t) * w_t
        where lr_t is the learning rate at t.
        When input is sparse shrinkage will only happen on the active weights.
      beta: A float value; corresponds to the beta parameter in the paper.

    Raises:
      ValueError: If one of the arguments is invalid.

    References:
      Ad-click prediction:
        [McMahan et al., 2013](https://dl.acm.org/citation.cfm?id=2488200)
        ([pdf](https://dl.acm.org/ft_gateway.cfm?id=2488200&ftid=1388399&dwn=1&CFID=32233078&CFTOKEN=d60fe57a294c056a-CB75C374-F915-E7A6-1573FBBC7BF7D526))
            z9initial_accumulator_value %f needs to be positive or zeroz3learning_rate_power %f needs to be negative or zeroz:l1_regularization_strength %f needs to be positive or zeroz:l2_regularization_strength %f needs to be positive or zerozDl2_shrinkage_regularization_strength %f needs to be positive or zeroN)superr   __init__
ValueError_learning_rate_learning_rate_power_initial_accumulator_value_l1_regularization_strength_l2_regularization_strength_beta%_l2_shrinkage_regularization_strength_learning_rate_tensor_learning_rate_power_tensor"_l1_regularization_strength_tensor+_adjusted_l2_regularization_strength_tensor,_l2_shrinkage_regularization_strength_tensor_accum_name_linear_name)selflearning_ratelearning_rate_powerinitial_accumulator_valuel1_regularization_strengthl2_regularization_strengthuse_lockingname
accum_namelinear_name$l2_shrinkage_regularization_strengthbeta	__class__s               O/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/training/ftrl.pyr   zFtrlOptimizer.__init__'   s9   n 
-'T: 3&
E
#$% % S L*+ , ,!C'
F
$%& & "C'
F
$%& & ,c1;<= = (D 3D&?D#'AD$'AD$#4DJ, 	.!%D'+D$.2D+7;D48<D5!D#D    c           
          t         j                  d f fd	}|D ]o  } j                  |||j                  |j                  d j
                  xs  j                          j                  |d j                  xs  j                         q y )Nc                 N    ~t        j                  | |      j                  z  S )N)shapedtype)r   onesr   )r1   r2   partition_infor    s      r-   _accum_initializerz7FtrlOptimizer._create_slots.<locals>._accum_initializer   s,    
^^U$&*&E&EF Fr.   accumlinear)	r   float32"_get_or_make_slot_with_initializerr1   r2   r   _name_zeros_slotr   )r    var_listr5   vs   `   r-   _create_slotszFtrlOptimizer._create_slots   s|    (.t F  E
--
!''7



(djj* q(D$5$5$CD	Er.   c           	      J   t        j                  | j                  d      | _        t        j                  | j                  d      | _        t        j                  | j                  | j                  dt        j                  | j                  d      z  z  z   d      | _
        | j                  J t        j                  | j                  d      | _        t        j                  | j                  d      | _        t        j                  | j                  d	      | _        y )
Nr!   )r'   r$   g       @gr.-Du8#adjusted_l2_regularization_strengthr+   r*   r"   )r   convert_to_tensorr   r   r   r   r   r   r   maximumr   _beta_tensorr   r   r   r   )r    s    r-   _preparezFtrlOptimizer._prepare   s    !$!6!6/"3D.1.C.C((/K/MD+ 8;7L7L((4::	ht22E:	:,< 	<284D4 ;;GGG--djjvFD8;8M8M22395D5 (+'<'<!!(=(?D$r.   c                    | j                  |d      }| j                  |d      }| j                  dk  rt        j                  ||||t	        j
                  | j                  |j                  j                        t	        j
                  | j                  |j                  j                        t	        j
                  | j                  |j                  j                        t	        j
                  | j                  |j                  j                        | j                  	      S t        j                  ||||t	        j
                  | j                  |j                  j                        t	        j
                  | j                  |j                  j                        t	        j
                  | j                  |j                  j                        t	        j
                  | j                  |j                  j                        t	        j
                  | j                  |j                  j                        | j                  
      S Nr6   r7   r   )r&   )get_slotr   r   
apply_ftrlr   castr   r2   
base_dtyper   r   r   _use_lockingapply_ftrl_v2r   r    gradvarr6   r7   s        r-   _apply_densezFtrlOptimizer._apply_dense   s   MM#w'E]]3)F11S8((




--22CII4H4H
I
--??		,,.
--HH		,,.
--88#)):N:N
O'') ) ++




--22CII4H4H
I
--??		,,.
--HH		,,.
--II		,,.
--88#)):N:N
O'') )r.   c                    | j                  |d      }| j                  |d      }| j                  dk  rt        j                  |j                  |j                  |j                  |t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        | j                  	      S t        j                  |j                  |j                  |j                  |t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        | j                  
      S rF   )rG   r   r   resource_apply_ftrlhandler   rI   r   r2   rJ   r   r   r   rK   resource_apply_ftrl_v2r   rM   s        r-   _resource_apply_densez#FtrlOptimizer._resource_apply_dense   s   MM#w'E]]3)F11S811
**
,,
--

--22CII4H4H
I
--??		,,.
--HH		,,.
--88#)):N:N
O'') ) 44
**
,,
--

--22CII4H4H
I
--??		,,.
--HH		,,.
--II		,,.
--88#)):N:N
O'') )r.   c                    | j                  |d      }| j                  |d      }| j                  dk  rt        j                  ||||j                  |j
                  t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        | j                  
      S t        j                  ||||j                  |j
                  t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                  |j                  j                        t        j                  | j                   |j                  j                        t        j                  | j                  |j                  j                        | j                        S rF   )rG   r   r   sparse_apply_ftrlvaluesindicesr   rI   r   r2   rJ   r   r   r   rK   sparse_apply_ftrl_v2r   rM   s        r-   _apply_sparsezFtrlOptimizer._apply_sparse   s   MM#w'E]]3)F11S8//



++
,,
--22CII4H4H
I
--??		,,.
--HH		,,.
--88#)):N:N
O'') ) 22



++
,,
--22CII4H4H
I
--??		,,.
--HH		,,.
--II

--/
--88#)):N:N
O'') )r.   c                 V   | j                  |d      }| j                  |d      }| j                  dk  rt        j                  |j                  |j                  |j                  ||t        j                  | j                  |j                        t        j                  | j                  |j                        t        j                  | j                  |j                        t        j                  | j                  |j                        | j                  
      S t        j                  |j                  |j                  |j                  ||t        j                  | j                  |j                        t        j                  | j                  |j                        t        j                  | j                  |j                        t        j                  | j                  |j                        t        j                  | j                  |j                        | j                        S rF   )rG   r   r   resource_sparse_apply_ftrlrS   r   rI   r   r2   r   r   r   rK   resource_sparse_apply_ftrl_v2r   )r    rN   rO   rY   r6   r7   s         r-   _resource_apply_sparsez$FtrlOptimizer._resource_apply_sparse  s   MM#w'E]]3)F11S888
**
,,
--


--22DJJ
?
--??
L
--HH

$
--88$**
E'') ) ;;
**
,,
--


--22DJJ
?
--??
L
--HH

$
--II

$
--88$**
E'') )r.   )
g      g?r   r   FFtrlNNr   N)__name__
__module____qualname____doc__r   r>   rD   rP   rU   r[   r_   __classcell__)r,   s   @r-   r   r      sQ    
 $(),*-*- 47[$z
E?&)@)@ )D)r.   r   N)rd   tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   tensorflow.python.trainingr    tensorflow.python.util.tf_exportr	   	Optimizerr    r.   r-   <module>rl      sL    $ . + + 2 * 0 6 $%&I)I'' I) 'I)r.   