§
    	¼¸j†"  ã                   ód   — d Z ddlZ G d„ d¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZdS )	z'Stochastic optimization methods for MLPé    Nc                   ó,   — e Zd ZdZdd„Zd„ Zd„ Zd„ ZdS )	ÚBaseOptimizera9  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                 ó<   — || _         t          |¦  «        | _        d S ©N)Úlearning_rate_initÚfloatÚlearning_rate)Úselfr   s     ú}/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/sklearn/neural_network/_stochastic_optimizers.pyÚ__init__zBaseOptimizer.__init__   s!   € Ø"4ˆÔÝ"Ð#5Ñ6Ô6ˆÔÐÐó    c                 óz   — |                       |¦  «        }t          d„ |D ¦   «         |¦  «        D ]
\  }}||z  }ŒdS )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   ó   K  — | ]}|V — Œd S r   © )Ú.0Úps     r   ú	<genexpr>z.BaseOptimizer.update_params.<locals>.<genexpr>*   s"   è è € Ð!4Ð!4¨ !Ð!4Ð!4Ð!4Ð!4Ð!4Ð!4r   N)Ú_get_updatesÚzip)r   ÚparamsÚgradsÚupdatesÚparamÚupdates         r   Úupdate_paramszBaseOptimizer.update_params   sX   € ð ×#Ò# EÑ*Ô*ˆÝ Ð!4Ð!4¨VÐ!4Ñ!4Ô!4°gÑ>Ô>ð 	ð 	‰MˆE�6Ø�V‰OˆEˆEð	ð 	r   c                 ó   — dS )zhPerform update to learning rate and potentially other states at the
        end of an iteration
        Nr   ©r   Ú	time_steps     r   Úiteration_endszBaseOptimizer.iteration_ends-   s	   € ð 	ˆr   c                 ó.   — |rt          |dz   ¦  «         dS )aH  Decides 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_stoppingzBaseOptimizer.trigger_stopping3   s$   € ð  ð 	&Ý�#˜Ñ$Ñ%Ô%Ð%Øˆtr   N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r'   r   r   r   r   r   	   s_   € € € € € ðð ð7ð 7ð 7ð 7ðð ð ð"ð ð ðð ð ð ð r   r   c                   ó@   ‡ — e Zd ZdZ	 	 	 	 	 dˆ fd„	Zd„ Zd	„ Zd
„ Zˆ xZS )ÚSGDOptimizeraË  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
    r   ÚconstantçÍÌÌÌÌÌì?Tç      à?c                 ó¤   •— t          ¦   «                              |¦  «         || _        || _        || _        || _        d„ |D ¦   «         | _        d S )Nc                 ó6   — g | ]}t          j        |¦  «        ‘ŒS r   ©ÚnpÚ
zeros_like©r   r   s     r   ú
<listcomp>z)SGDOptimizer.__init__.<locals>.<listcomp>‡   s"   € ÐDÐDÐD°E�2œ=¨Ñ/Ô/ÐDÐDÐDr   )Úsuperr   Úlr_scheduleÚmomentumÚnesterovÚpower_tÚ
velocities)r   r   r   r9   r:   r;   r<   Ú	__class__s          €r   r   zSGDOptimizer.__init__x   sT   ø€ õ 	‰Œ×ÒÐ+Ñ,Ô,Ð,à&ˆÔØ ˆŒØ ˆŒØˆŒØDÐD¸VÐDÑDÔDˆŒˆˆr   c                 ón   — | j         dk    r)t          | j        ¦  «        |dz   | j        z  z  | _        dS dS )a  Perform 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    zSGDOptimizer.iteration_ends‰   sF   € ð Ô˜|Ò+Ð+å�dÔ-Ñ.Ô.°)¸a±-ÀDÄLÑ1PÑPð ÔÐÐð ,Ð+r   c                 óâ   — | j         dk    r|rt          |dz   ¦  «         dS | j        dk    r|rt          |dz   ¦  «         dS | xj        dz  c_        |rt          |d| j        z  z   ¦  «         dS )	NÚadaptiver"   Tg�íµ ÷Æ°>z# Learning rate too small. Stopping.g      @z Setting learning rate to %fF)r9   r#   r
   r$   s      r   r'   zSGDOptimizer.trigger_stopping˜   sž   € ØÔ˜zÒ)Ð)Øð *Ý�c˜LÑ(Ñ)Ô)Ð)Ø�4àÔ Ò%Ð%Øð CÝ�cÐAÑAÑBÔBÐBØ�4àÐÔ˜cÑ!ÐÔØð 	MÝ�#Ð6¸Ô9KÑKÑKÑLÔLÐLØˆur   c                 ó¨   ‡ — ˆ fd„t          ‰ j        |¦  «        D ¦   «         }|‰ _        ‰ j        r!ˆ fd„t          ‰ j        |¦  «        D ¦   «         }|S )á¦  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
        c                 ó@   •— g | ]\  }}‰j         |z  ‰j        |z  z
  ‘ŒS r   ©r:   r
   ©r   ÚvelocityÚgradr   s      €r   r7   z-SGDOptimizer._get_updates.<locals>.<listcomp>¶   sA   ø€ ð 
ð 
ð 
á�˜$ð ŒM˜HÑ$ tÔ'9¸DÑ'@Ñ@ð
ð 
ð 
r   c                 ó@   •— g | ]\  }}‰j         |z  ‰j        |z  z
  ‘ŒS r   rG   rH   s      €r   r7   z-SGDOptimizer._get_updates.<locals>.<listcomp>½   sA   ø€ ð ð ð á"�H˜dð ” Ñ(¨4Ô+=ÀÑ+DÑDðð ð r   )r   r=   r;   ©r   r   r   s   `  r   r   zSGDOptimizer._get_updates¨   s…   ø€ ð
ð 
ð 
ð 
å"% d¤o°uÑ"=Ô"=ð
ñ 
ô 
ˆð "ˆŒàŒ=ð 	ðð ð ð å&)¨$¬/¸5Ñ&AÔ&Aðñ ô ˆGð
 ˆr   )r   r.   r/   Tr0   )	r(   r)   r*   r+   r   r    r'   r   Ú__classcell__©r>   s   @r   r-   r-   H   sŒ   ø€ € € € € ð-ð -ðd ØØØØðEð Eð Eð Eð Eð Eð"ð ð ðð ð ð ð ð ð ð ð ð r   r-   c                   ó,   ‡ — e Zd ZdZ	 dˆ fd„	Zd„ Zˆ xZS )	ÚAdamOptimizeraÊ  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>
    çü©ñÒMbP?r/   ç+‡ÙÎ÷ï?ç:Œ0âŽyE>c                 óÆ   •— t          ¦   «                              |¦  «         || _        || _        || _        d| _        d„ |D ¦   «         | _        d„ |D ¦   «         | _        d S )Nr   c                 ó6   — g | ]}t          j        |¦  «        ‘ŒS r   r3   r6   s     r   r7   z*AdamOptimizer.__init__.<locals>.<listcomp>ü   ó"   € Ð<Ð<Ð<¨E•2”= Ñ'Ô'Ð<Ð<Ð<r   c                 ó6   — g | ]}t          j        |¦  «        ‘ŒS r   r3   r6   s     r   r7   z*AdamOptimizer.__init__.<locals>.<listcomp>ý   rV   r   )r8   r   Úbeta_1Úbeta_2ÚepsilonÚtÚmsÚvs)r   r   r   rX   rY   rZ   r>   s         €r   r   zAdamOptimizer.__init__ó   sh   ø€ õ 	‰Œ×ÒÐ+Ñ,Ô,Ð,àˆŒØˆŒØˆŒØˆŒØ<Ð<°VÐ<Ñ<Ô<ˆŒØ<Ð<°VÐ<Ñ<Ô<ˆŒˆˆr   c                 ó”  ‡ — ‰ xj         dz  c_         ˆ fd„t          ‰ j        |¦  «        D ¦   «         ‰ _        ˆ fd„t          ‰ j        |¦  «        D ¦   «         ‰ _        ‰ j        t          j        d‰ j        ‰ j         z  z
  ¦  «        z  d‰ j        ‰ j         z  z
  z  ‰ _	        ˆ fd„t          ‰ j        ‰ j        ¦  «        D ¦   «         }|S )rE   rA   c                 óF   •— g | ]\  }}‰j         |z  d ‰j         z
  |z  z   ‘ŒS )rA   )rX   )r   ÚmrJ   r   s      €r   r7   z.AdamOptimizer._get_updates.<locals>.<listcomp>  sC   ø€ ð 
ð 
ð 
á��4ð ŒK˜!‰O˜q 4¤;™°$Ñ6Ñ6ð
ð 
ð 
r   c                 óL   •— g | ] \  }}‰j         |z  d ‰j         z
  |dz  z  z   ‘Œ!S )rA   é   )rY   )r   ÚvrJ   r   s      €r   r7   z.AdamOptimizer._get_updates.<locals>.<listcomp>  sG   ø€ ð 
ð 
ð 
á��4ð ŒK˜!‰O˜q 4¤;™°4¸±7Ñ;Ñ;ð
ð 
ð 
r   c                 óf   •— g | ]-\  }}‰j          |z  t          j        |¦  «        ‰j        z   z  ‘Œ.S r   )r
   r4   ÚsqrtrZ   )r   r`   rc   r   s      €r   r7   z.AdamOptimizer._get_updates.<locals>.<listcomp>  sL   ø€ ð 
ð 
ð 
á��1ð ÔÐ !Ñ#¥r¤w¨q¡z¤z°D´LÑ'@ÑAð
ð 
ð 
r   )
r[   r   r\   r]   r   r4   re   rY   rX   r
   rL   s   `  r   r   zAdamOptimizer._get_updatesÿ   s  ø€ ð 	ˆŒ�!‰ˆŒð
ð 
ð 
ð 
å˜tœw¨Ñ.Ô.ð
ñ 
ô 
ˆŒð
ð 
ð 
ð 
å˜tœw¨Ñ.Ô.ð
ñ 
ô 
ˆŒð
 Ô#ÝŒg�a˜$œ+ t¤vÑ-Ñ-Ñ.Ô.ñ/à�4”; ¤Ñ&Ñ&ñ(ð 	Ôð

ð 
ð 
ð 
å˜DœG T¤WÑ-Ô-ð
ñ 
ô 
ˆð ˆr   )rQ   r/   rR   rS   )r(   r)   r*   r+   r   r   rM   rN   s   @r   rP   rP   Å   s^   ø€ € € € € ð+ð +ð\ SWð
=ð 
=ð 
=ð 
=ð 
=ð 
=ð ð  ð  ð  ð  ð  ð  r   rP   )r+   Únumpyr4   r   r-   rP   r   r   r   ú<module>rg      s³   ðØ -Ð -ð
 Ð Ð Ð ð<ð <ð <ð <ð <ñ <ô <ð <ð~zð zð zð zð z�=ñ zô zð zðzZð Zð Zð Zð Z�Mñ Zô Zð Zð Zð Zr   