
    -i"                     P    S r SSKr " S S5      r " S S\5      r " S S\5      rg)	z'Stochastic optimization methods for MLP    Nc                   4    \ rS rSrSrS	S jrS rS rS rSr	g)
BaseOptimizer	   a  Base (Stochastic) gradient descent optimizer

Parameters
----------
learning_rate_init : float, default=0.1
    The initial learning rate used. It controls the step-size in updating
    the weights

Attributes
----------
learning_rate : float
    the current learning rate
c                 0    Xl         [        U5      U l        g N)learning_rate_initfloatlearning_rate)selfr   s     `/var/www/html/venv/lib/python3.13/site-packages/sklearn/neural_network/_stochastic_optimizers.py__init__BaseOptimizer.__init__   s    "4"#56    c                 f    U R                  U5      n[        S U 5       U5       H	  u  pEXE-  nM     g)a  Update parameters with given gradients

Parameters
----------
params : list of length = len(coefs_) + len(intercepts_)
    The concatenated list containing coefs_ and intercepts_ in MLP
    model. Used for initializing velocities and updating params

grads : list of length = len(params)
    Containing gradients with respect to coefs_ and intercepts_ in MLP
    model. So length should be aligned with params
c              3   $   #    U  H  ov   M     g 7fr    ).0ps     r   	<genexpr>.BaseOptimizer.update_params.<locals>.<genexpr>*   s     !4V!Vs   N)_get_updateszip)r   paramsgradsupdatesparamupdates         r   update_paramsBaseOptimizer.update_params   s4     ##E* !4V!4g>MEOE ?r   c                     g)zXPerform update to learning rate and potentially other states at the
end of an iteration
Nr   r   	time_steps     r   iteration_endsBaseOptimizer.iteration_ends-   s     	r   c                 .    U(       a  [        US-   5        g)zDecides whether it is time to stop training

Parameters
----------
msg : str
    Message passed in for verbose output

verbose : bool
    Print message to stdin if True

Returns
-------
is_stopping : bool
    True if training needs to stop

 Stopping.T)printr   msgverboses      r   trigger_stoppingBaseOptimizer.trigger_stopping3   s      #$%r   )r
   r   N)皙?)
__name__
__module____qualname____firstlineno____doc__r   r   r#   r+   __static_attributes__r   r   r   r   r   	   s    7"r   r   c                   L   ^  \ rS rSrSr     SU 4S jjrS rS rS rSr	U =r
$ )	SGDOptimizerH   aC  Stochastic gradient descent optimizer with momentum

Parameters
----------
params : list, length = len(coefs_) + len(intercepts_)
    The concatenated list containing coefs_ and intercepts_ in MLP model.
    Used for initializing velocities and updating params

learning_rate_init : float, default=0.1
    The initial learning rate used. It controls the step-size in updating
    the weights

lr_schedule : {'constant', 'adaptive', 'invscaling'}, default='constant'
    Learning rate schedule for weight updates.

    -'constant', is a constant learning rate given by
     'learning_rate_init'.

    -'invscaling' gradually decreases the learning rate 'learning_rate_' at
      each time step 't' using an inverse scaling exponent of 'power_t'.
      learning_rate_ = learning_rate_init / pow(t, power_t)

    -'adaptive', keeps the learning rate constant to
     'learning_rate_init' as long as the training keeps decreasing.
     Each time 2 consecutive epochs fail to decrease the training loss by
     tol, or fail to increase validation score by tol if 'early_stopping'
     is on, the current learning rate is divided by 5.

momentum : float, default=0.9
    Value of momentum used, must be larger than or equal to 0

nesterov : bool, default=True
    Whether to use nesterov's momentum or not. Use nesterov's if True

power_t : float, default=0.5
    Power of time step 't' in inverse scaling. See `lr_schedule` for
    more details.

Attributes
----------
learning_rate : float
    the current learning rate

velocities : list, length = len(params)
    velocities that are used to update params
c                    > [         TU ]  U5        X0l        X@l        XPl        X`l        U Vs/ s H  n[        R                  " U5      PM     snU l        g s  snf r   )	superr   lr_schedulemomentumnesterovpower_tnp
zeros_like
velocities)	r   r   r   r9   r:   r;   r<   r   	__class__s	           r   r   SGDOptimizer.__init__x   sJ     	+,&  =CDVE2==/VDDs    Ac                     U R                   S:X  a.  [        U R                  5      US-   U R                  -  -  U l        gg)zPerform updates to learning rate and potential other states at the
end of an iteration

Parameters
----------
time_step : int
    number of training samples trained on so far, used to update
    learning rate for 'invscaling'

invscaling   N)r9   r	   r   r<   r
   r!   s     r   r#   SGDOptimizer.iteration_ends   s?     |+d--.)a-DLL1PP  ,r   c                 
   U R                   S:w  a  U(       a  [        US-   5        gU R                  S::  a  U(       a  [        US-   5        gU =R                  S-  sl        U(       a  [        USU R                  -  -   5        g)	Nadaptiver&   Tgư>z# Learning rate too small. Stopping.g      @z Setting learning rate to %fF)r9   r'   r
   r(   s      r   r+   SGDOptimizer.trigger_stopping   su    z)cL()%cAABc!#69K9KKKLr   c                 h   [        U R                  U5       VVs/ s H$  u  p#U R                  U-  U R                  U-  -
  PM&     nnnX@l        U R                  (       aG  [        U R                  U5       VVs/ s H$  u  p#U R                  U-  U R                  U-  -
  PM&     nnnU$ s  snnf s  snnf )V  Get the values used to update params with given gradients

Parameters
----------
grads : list, length = len(coefs_) + len(intercepts_)
    Containing gradients with respect to coefs_ and intercepts_ in MLP
    model. So length should be aligned with params

Returns
-------
updates : list, length = len(grads)
    The values to add to params
)r   r?   r:   r
   r;   )r   r   velocitygradr   s        r   r   SGDOptimizer._get_updates   s      #&doou"=
"= MMH$t'9'9D'@@"= 	 
 "== '*$//5&A&ANH (4+=+=+DD&A  
 
s   +B(8+B.)r
   r9   r:   r;   r<   r?   )r-   constant?Tg      ?)r.   r/   r0   r1   r2   r   r#   r+   r   r3   __classcell__r@   s   @r   r5   r5   H   s4    -d E"  r   r5   c                   8   ^  \ rS rSrSr SU 4S jjrS rSrU =r$ )AdamOptimizer   aJ  Stochastic gradient descent optimizer with Adam

Note: All default values are from the original Adam paper

Parameters
----------
params : list, length = len(coefs_) + len(intercepts_)
    The concatenated list containing coefs_ and intercepts_ in MLP model.
    Used for initializing velocities and updating params

learning_rate_init : float, default=0.001
    The initial learning rate used. It controls the step-size in updating
    the weights

beta_1 : float, default=0.9
    Exponential decay rate for estimates of first moment vector, should be
    in [0, 1)

beta_2 : float, default=0.999
    Exponential decay rate for estimates of second moment vector, should be
    in [0, 1)

epsilon : float, default=1e-8
    Value for numerical stability

Attributes
----------
learning_rate : float
    The current learning rate

t : int
    Timestep

ms : list, length = len(params)
    First moment vectors

vs : list, length = len(params)
    Second moment vectors

References
----------
:arxiv:`Kingma, Diederik, and Jimmy Ba (2014) "Adam: A method for
    stochastic optimization." <1412.6980>
c                   > [         TU ]  U5        X0l        X@l        XPl        SU l        U Vs/ s H  n[        R                  " U5      PM     snU l        U Vs/ s H  n[        R                  " U5      PM     snU l	        g s  snf s  snf )Nr   )
r8   r   beta_1beta_2epsilontr=   r>   msvs)r   r   r   rV   rW   rX   r   r@   s          r   r   AdamOptimizer.__init__   so     	+,5;<VE2=='V<5;<VE2=='V< =<s    B Bc                 $   U =R                   S-  sl         [        U R                  U5       VVs/ s H'  u  p#U R                  U-  SU R                  -
  U-  -   PM)     snnU l        [        U R                  U5       VVs/ s H*  u  pCU R
                  U-  SU R
                  -
  US-  -  -   PM,     snnU l        U R                  [        R                  " SU R
                  U R                   -  -
  5      -  SU R                  U R                   -  -
  -  U l	        [        U R                  U R                  5       VVs/ s H9  u  p$U R                  * U-  [        R                  " U5      U R                  -   -  PM;     nnnU$ s  snnf s  snnf s  snnf )rJ   rD      )rY   r   rZ   rV   r[   rW   r   r=   sqrtr
   rX   )r   r   mrL   vr   s         r   r   AdamOptimizer._get_updates   sb    	! tww.
. KK!Oq4;;$66.
 tww.
. KK!Oq4;;47;;.

 ##gga$++tvv--./4;;&&( 	 DGGTWW-
- !#rwwqzDLL'@A- 	 
 #


s   .F ?1F;A F)rV   rW   rX   r
   rZ   rY   r[   )gMbP?rO   g+?g:0yE>)	r.   r/   r0   r1   r2   r   r   r3   rP   rQ   s   @r   rS   rS      s    +\ SW
=   r   rS   )r2   numpyr=   r   r5   rS   r   r   r   <module>rd      s5    -
 < <~z= zzZM Zr   