§
    rŠtj–:  ã                   ó²   — d Z ddlZddlmZmZ ddlZddlmZ	 ddl
mZ ddlmZmZmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZ  G d
„ deee¦  «        ZdS )zRestricted Boltzmann Machineé    N)ÚIntegralÚReal)Úexpit)ÚBaseEstimatorÚClassNamePrefixFeaturesOutMixinÚTransformerMixinÚ_fit_context)Úcheck_random_stateÚgen_even_slices)ÚInterval)Úsafe_sparse_dot)Úcheck_is_fittedÚvalidate_datac            	       óT  ‡ — e Zd ZU dZ eeddd¬¦  «        g eeddd¬¦  «        g eeddd¬¦  «        g eeddd¬¦  «        gdgd	gd
œZee	d<   	 dddddddœd„Z
d„ Zd„ Zd„ Zd„ Zd„ Zd„ Z ed¬¦  «        dd„¦   «         Zd„ Zd„ Z ed¬¦  «        dd„¦   «         Zˆ fd„Zˆ xZS ) ÚBernoulliRBMa  Bernoulli Restricted Boltzmann Machine (RBM).

    A Restricted Boltzmann Machine with binary visible units and
    binary hidden units. Parameters are estimated using Stochastic Maximum
    Likelihood (SML), also known as Persistent Contrastive Divergence (PCD)
    [2].

    The time complexity of this implementation is ``O(d ** 2)`` assuming
    d ~ n_features ~ n_components.

    Read more in the :ref:`User Guide <rbm>`.

    Parameters
    ----------
    n_components : int, default=256
        Number of binary hidden units.

    learning_rate : float, default=0.1
        The learning rate for weight updates. It is *highly* recommended
        to tune this hyper-parameter. Reasonable values are in the
        10**[0., -3.] range.

    batch_size : int, default=10
        Number of examples per minibatch.

    n_iter : int, default=10
        Number of iterations/sweeps over the training dataset to perform
        during training.

    verbose : int, default=0
        The verbosity level. The default, zero, means silent mode. Range
        of values is [0, inf].

    random_state : int, RandomState instance or None, default=None
        Determines random number generation for:

        - Gibbs sampling from visible and hidden layers.

        - Initializing components, sampling from layers during fit.

        - Corrupting the data when scoring samples.

        Pass an int for reproducible results across multiple function calls.
        See :term:`Glossary <random_state>`.

    Attributes
    ----------
    intercept_hidden_ : array-like of shape (n_components,)
        Biases of the hidden units.

    intercept_visible_ : array-like of shape (n_features,)
        Biases of the visible units.

    components_ : array-like of shape (n_components, n_features)
        Weight matrix, where `n_features` is the number of
        visible units and `n_components` is the number of hidden units.

    h_samples_ : array-like of shape (batch_size, n_components)
        Hidden Activation sampled from the model distribution,
        where `batch_size` is the number of examples per minibatch and
        `n_components` is the number of hidden units.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    sklearn.neural_network.MLPRegressor : Multi-layer Perceptron regressor.
    sklearn.neural_network.MLPClassifier : Multi-layer Perceptron classifier.
    sklearn.decomposition.PCA : An unsupervised linear dimensionality
        reduction model.

    References
    ----------

    [1] Hinton, G. E., Osindero, S. and Teh, Y. A fast learning algorithm for
        deep belief nets. Neural Computation 18, pp 1527-1554.
        https://www.cs.toronto.edu/~hinton/absps/fastnc.pdf

    [2] Tieleman, T. Training Restricted Boltzmann Machines using
        Approximations to the Likelihood Gradient. International Conference
        on Machine Learning (ICML) 2008

    Examples
    --------

    >>> import numpy as np
    >>> from sklearn.neural_network import BernoulliRBM
    >>> X = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
    >>> model = BernoulliRBM(n_components=2)
    >>> model.fit(X)
    BernoulliRBM(n_components=2)

    For a more detailed example usage, see
    :ref:`sphx_glr_auto_examples_neural_networks_plot_rbm_logistic_classification.py`.
    é   NÚleft)Úclosedr   ÚneitherÚverboseÚrandom_state©Ún_componentsÚlearning_rateÚ
batch_sizeÚn_iterr   r   Ú_parameter_constraintsé   gš™™™™™¹?é
   )r   r   r   r   r   c                óZ   — || _         || _        || _        || _        || _        || _        d S ©Nr   )Úselfr   r   r   r   r   r   s          úY/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sklearn/neural_network/_rbm.pyÚ__init__zBernoulliRBM.__init__Œ   s7   € ð )ˆÔØ*ˆÔØ$ˆŒØˆŒØˆŒØ(ˆÔÐÐó    c                 óž   — t          | ¦  «         t          | |ddt          j        t          j        f¬¦  «        }|                      |¦  «        S )ag  Compute the hidden layer activation probabilities, P(h=1|v=X).

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            The data to be transformed.

        Returns
        -------
        h : ndarray of shape (n_samples, n_components)
            Latent representations of the data.
        ÚcsrF)Úaccept_sparseÚresetÚdtype)r   r   ÚnpÚfloat64Úfloat32Ú_mean_hiddens)r"   ÚXs     r#   Ú	transformzBernoulliRBM.transform�   sP   € õ 	˜ÑÔÐåØ�! 5°½b¼jÍ"Ì*Ð=Uð
ñ 
ô 
ˆð ×!Ò! !Ñ$Ô$Ð$r%   c                 ól   — t          || j        j        ¦  «        }|| j        z  }t	          ||¬¦  «        S )aL  Computes the probabilities P(h=1|v).

        Parameters
        ----------
        v : ndarray of shape (n_samples, n_features)
            Values of the visible layer.

        Returns
        -------
        h : ndarray of shape (n_samples, n_components)
            Corresponding mean field values for the hidden layer.
        ©Úout)r   Úcomponents_ÚTÚintercept_hidden_r   )r"   ÚvÚps      r#   r.   zBernoulliRBM._mean_hiddens±   s6   € õ ˜A˜tÔ/Ô1Ñ2Ô2ˆØ	ˆTÔ#Ñ#ˆÝ�Q˜Aˆ‰ŒÐr%   c                 ój   — |                       |¦  «        }|                     |j        ¬¦  «        |k     S )a‘  Sample from the distribution P(h|v).

        Parameters
        ----------
        v : ndarray of shape (n_samples, n_features)
            Values of the visible layer to sample from.

        rng : RandomState instance
            Random number generator to use.

        Returns
        -------
        h : ndarray of shape (n_samples, n_components)
            Values of the hidden layer.
        ©Úsize)r.   ÚuniformÚshape)r"   r7   Úrngr8   s       r#   Ú_sample_hiddenszBernoulliRBM._sample_hiddensÂ   s2   € ð  ×Ò˜qÑ!Ô!ˆØ�{Š{ ¤ˆ{Ñ(Ô(¨1Ò,Ð,r%   c                 óª   — t          j        || j        ¦  «        }|| j        z  }t	          ||¬¦  «         |                     |j        ¬¦  «        |k     S )a‘  Sample from the distribution P(v|h).

        Parameters
        ----------
        h : ndarray of shape (n_samples, n_components)
            Values of the hidden layer to sample from.

        rng : RandomState instance
            Random number generator to use.

        Returns
        -------
        v : ndarray of shape (n_samples, n_features)
            Values of the visible layer.
        r2   r:   )r+   Údotr4   Úintercept_visible_r   r<   r=   )r"   Úhr>   r8   s       r#   Ú_sample_visibleszBernoulliRBM._sample_visiblesÕ   sO   € õ  ŒF�1�dÔ&Ñ'Ô'ˆØ	ˆTÔ$Ñ$ˆÝˆa�Qˆ‰ŒˆØ�{Š{ ¤ˆ{Ñ(Ô(¨1Ò,Ð,r%   c                 óÂ   — t          || j        ¦  «         t          j        dt          || j        j        ¦  «        | j        z   ¦  «                             d¬¦  «        z
  S )aF  Computes the free energy F(v) = - log sum_h exp(-E(v,h)).

        Parameters
        ----------
        v : ndarray of shape (n_samples, n_features)
            Values of the visible layer.

        Returns
        -------
        free_energy : ndarray of shape (n_samples,)
            The value of the free energy.
        r   r   ©Úaxis)r   rB   r+   Ú	logaddexpr4   r5   r6   Úsum)r"   r7   s     r#   Ú_free_energyzBernoulliRBM._free_energyê   sY   € õ    4Ô#:Ñ;Ô;Ð;½b¼lØ�˜q $Ô"2Ô"4Ñ5Ô5¸Ô8NÑNñ?
ô ?
ç
Š#�1ˆ#‰+Œ+ñð 	r%   c                 óâ   — t          | ¦  «         t          | d¦  «        st          | j        ¦  «        | _        |                      || j        ¦  «        }|                      || j        ¦  «        }|S )aT  Perform one Gibbs sampling step.

        Parameters
        ----------
        v : ndarray of shape (n_samples, n_features)
            Values of the visible layer to start from.

        Returns
        -------
        v_new : ndarray of shape (n_samples, n_features)
            Values of the visible layer after one Gibbs step.
        Úrandom_state_)r   Úhasattrr
   r   rL   r?   rD   )r"   r7   Úh_Úv_s       r#   ÚgibbszBernoulliRBM.gibbsû   sm   € õ 	˜ÑÔÐÝ�t˜_Ñ-Ô-ð 	GÝ!3°DÔ4EÑ!FÔ!FˆDÔØ×!Ò! ! TÔ%7Ñ8Ô8ˆØ×"Ò" 2 tÔ'9Ñ:Ô:ˆàˆ	r%   T)Úprefer_skip_nested_validationc           	      óö  — t          | d¦  «         }t          | |dt          j        |¬¦  «        }t          | d¦  «        st	          | j        ¦  «        | _        t          | d¦  «        s^t          j        | j                             dd| j	        |j
        d         f¦  «        d¬	¦  «        | _        | j        j
        d         | _        t          | d
¦  «        st          j        | j	        ¦  «        | _        t          | d¦  «        s$t          j        |j
        d         ¦  «        | _        t          | d¦  «        s%t          j        | j        | j	        f¦  «        | _        |                      || j        ¦  «         dS )a§  Fit the model to the partial segment of the data X.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs), default=None
            Target values (None for unsupervised transformations).

        Returns
        -------
        self : BernoulliRBM
            The fitted model.
        r4   r'   )r(   r*   r)   rL   r   ç{®Gáz„?r   ÚF)Úorderr6   rB   Ú
h_samples_N)rM   r   r+   r,   r
   r   rL   ÚasarrayÚnormalr   r=   r4   Ú_n_features_outÚzerosr6   rB   r   rV   Ú_fit)r"   r/   ÚyÚ
first_passs       r#   Úpartial_fitzBernoulliRBM.partial_fit  st  € õ" !  }Ñ5Ô5Ð5ˆ
ÝØ�! 5µ´
À*ð
ñ 
ô 
ˆõ �t˜_Ñ-Ô-ð 	GÝ!3°DÔ4EÑ!FÔ!FˆDÔÝ�t˜]Ñ+Ô+ð 	=Ý!œzØÔ"×)Ò)¨!¨T°DÔ4EÀqÄwÈqÄzÐ3RÑSÔSØð ñ  ô  ˆDÔð $(Ô#3Ô#9¸!Ô#<ˆDÔ Ý�tÐ0Ñ1Ô1ð 	Ý%'¤XØÔ!ñ&ô &ˆDÔ"õ �tÐ1Ñ2Ô2ð 	Ý&(¤hØ”˜”
ñ'ô 'ˆDÔ#õ �t˜\Ñ*Ô*ð 	MÝ œh¨¬¸Ô9JÐ'KÑLÔLˆDŒOà�	Š	�!�TÔ'Ñ(Ô(Ð(Ð(Ð(r%   c                 ó$  — |                       |¦  «        }|                      | j        |¦  «        }|                       |¦  «        }t          | j        ¦  «        |j        d         z  }t          |j        |d¬¦  «        j        }|t          j	        |j        |¦  «        z  }| xj
        ||z  z  c_
        | xj        ||                     d¬¦  «        |                     d¬¦  «        z
  z  z  c_        | xj        |t          j        |                     d¬¦  «        ¦  «                             ¦   «         |                     d¬¦  «        z
  z  z  c_        d||                     |j        ¬¦  «        |k     <   t          j        ||¦  «        | _        dS )a  Inner fit for one mini-batch.

        Adjust the parameters to maximize the likelihood of v using
        Stochastic Maximum Likelihood (SML).

        Parameters
        ----------
        v_pos : ndarray of shape (n_samples, n_features)
            The data to use for training.

        rng : RandomState instance
            Random number generator to use for sampling.
        r   T)Údense_outputrF   g      ð?r:   N)r.   rD   rV   Úfloatr   r=   r   r5   r+   rA   r4   r6   rI   rB   rW   Úsqueezer<   Úfloor)r"   Úv_posr>   Úh_posÚv_negÚh_negÚlrÚupdates           r#   r[   zBernoulliRBM._fit:  sl  € ð ×"Ò" 5Ñ)Ô)ˆØ×%Ò% d¤o°sÑ;Ô;ˆØ×"Ò" 5Ñ)Ô)ˆå�4Ô%Ñ&Ô&¨¬°Q¬Ñ7ˆÝ  ¤¨%¸dÐCÑCÔCÔEˆØ•"”&˜œ %Ñ(Ô(Ñ(ˆØÐÔ˜B ™KÑ'ÐÔØÐÔ "¨¯	ª	°q¨	Ñ(9Ô(9¸E¿IºIÈ1¸IÑ<MÔ<MÑ(MÑ"NÑNÐÔØÐÔ 2ÝŒJ�u—y’y a�yÑ(Ô(Ñ)Ô)×1Ò1Ñ3Ô3°e·i²iÀQ°iÑ6GÔ6GÑGñ$
ñ 	
ÐÔð 8;ˆˆc�kŠk˜uœ{ˆkÑ+Ô+¨eÒ3Ñ4Ýœ( 5¨%Ñ0Ô0ˆŒˆˆr%   c                 ó<  — t          | ¦  «         t          | |dd¬¦  «        }t          | j        ¦  «        }t	          j        |j        d         ¦  «        |                     d|j        d         |j        d         ¦  «        f}t          j	        |¦  «        r“d||         z  dz   }t          |t          j        ¦  «        r8|t          j        |j                             ¦   «         |f|j        ¬¦  «        z   }nU|t          j        |                     ¦   «         |f|j        ¬¦  «        z   }n"|                     ¦   «         }d||         z
  ||<   |                      |¦  «        }|                      |¦  «        }|j        d          t	          j        d||z
   ¦  «        z  S )a|  Compute the pseudo-likelihood of X.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Values of the visible layer. Must be all-boolean (not checked).

        Returns
        -------
        pseudo_likelihood : ndarray of shape (n_samples,)
            Value of the pseudo-likelihood (proxy for likelihood).

        Notes
        -----
        This method is not deterministic: it computes a quantity called the
        free energy on X, then on a randomly corrupted version of X, and
        returns the log of the logistic function of the difference.
        r'   F)r(   r)   r   r   éþÿÿÿ)r=   )r   r   r
   r   r+   Úaranger=   ÚrandintÚspÚissparseÚ
isinstanceÚmatrixÚ
csr_matrixÚAÚravelÚ	csr_arrayÚcopyrJ   rH   )	r"   r/   r7   r>   ÚindÚdatarO   ÚfeÚfe_s	            r#   Úscore_sampleszBernoulliRBM.score_samplesX  se  € õ& 	˜ÑÔÐå˜$ °¸eÐDÑDÔDˆÝ  Ô!2Ñ3Ô3ˆõ Œy˜œ œÑ$Ô$ c§k¢k°!°Q´W¸Q´ZÀÄÈÄÑ&LÔ&LÐMˆÝŒ;�q‰>Œ>ð 	"Ø˜˜#œ‘; ‘?ˆDÝ˜$¥¤	Ñ*Ô*ð JØ�œ¨¬¯ª©¬¸Ð'<ÀAÄGÐLÑLÔLÑL��à�œ t§z¢z¡|¤|°SÐ&9ÀÄÐIÑIÔIÑI��à—’‘”ˆBØ˜"˜Sœ'‘kˆBˆs‰Gà×Ò˜qÑ!Ô!ˆØ×Ò Ñ#Ô#ˆà”˜”
ˆ{�Rœ\¨!¨s°R©x¨[Ñ9Ô9Ñ9Ð9r%   c           	      ó„  — t          | |dt          j        t          j        f¬¦  «        }|j        d         }t          | j        ¦  «        }t          j        |                     dd| j	        |j        d         f¦  «        d|j
        ¬¦  «        | _        | j        j        d         | _        t          j        | j	        |j
        ¬¦  «        | _        t          j        |j        d         |j
        ¬¦  «        | _        t          j        | j        | j	        f|j
        ¬¦  «        | _        t%          t          j        t)          |¦  «        | j        z  ¦  «        ¦  «        }t+          t-          || j        z  ||¬	¦  «        ¦  «        }| j        }t1          j        ¦   «         }t3          d| j        dz   ¦  «        D ]Š}	|D ]}
|                      ||
         |¦  «         Œ|ret1          j        ¦   «         }t9          d
t;          | ¦  «        j        |	|                      |¦  «                              ¦   «         ||z
  fz  ¦  «         |}Œ‹| S )a¤  Fit the model to the data X.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs), default=None
            Target values (None for unsupervised transformations).

        Returns
        -------
        self : BernoulliRBM
            The fitted model.
        r'   )r(   r*   r   rS   r   rT   )rU   r*   )r*   )Ú	n_samplesz9[%s] Iteration %d, pseudo-likelihood = %.2f, time = %.2fs)!r   r+   r,   r-   r=   r
   r   rW   rX   r   r*   r4   rY   rZ   r6   rB   r   rV   ÚintÚceilra   Úlistr   r   ÚtimeÚranger   r[   ÚprintÚtypeÚ__name__r{   Úmean)r"   r/   r\   r}   r>   Ú	n_batchesÚbatch_slicesr   ÚbeginÚ	iterationÚbatch_sliceÚends               r#   ÚfitzBernoulliRBM.fit�  s  € õ" ˜$ °½r¼zÍ2Ì:Ð>VÐWÑWÔWˆØ”G˜A”Jˆ	Ý  Ô!2Ñ3Ô3ˆåœ:Ø�JŠJ�q˜$ Ô!2°A´G¸A´JÐ ?Ñ@Ô@ØØ”'ð
ñ 
ô 
ˆÔð
  $Ô/Ô5°aÔ8ˆÔÝ!#¤¨$Ô*;À1Ä7Ð!KÑ!KÔ!KˆÔÝ"$¤(¨1¬7°1¬:¸Q¼WÐ"EÑ"EÔ"EˆÔÝœ( D¤O°TÔ5FÐ#GÈqÌwÐWÑWÔWˆŒå�œ¥ iÑ 0Ô 0°4´?Ñ BÑCÔCÑDÔDˆ	ÝÝ˜I¨¬Ñ7¸ÈiÐXÑXÔXñ
ô 
ˆð ”,ˆÝ”	‘”ˆÝ˜q $¤+°¡/Ñ2Ô2ð 	ð 	ˆIØ+ð /ð /�Ø—	’	˜!˜Kœ.¨#Ñ.Ô.Ð.Ð.àð Ý”i‘k”k�ÝØOå˜T™
œ
Ô+Ø!Ø×*Ò*¨1Ñ-Ô-×2Ò2Ñ4Ô4Ø˜e™ð	ññô ð ð �øàˆr%   c                 ó|   •— t          ¦   «                              ¦   «         }d|j        _        ddg|j        _        |S )NTr,   r-   )ÚsuperÚ__sklearn_tags__Ú
input_tagsÚsparseÚtransformer_tagsÚpreserves_dtype)r"   ÚtagsÚ	__class__s     €r#   r�   zBernoulliRBM.__sklearn_tags__¹  s7   ø€ Ý‰wŒw×'Ò'Ñ)Ô)ˆØ!%ˆŒÔØ1:¸IÐ0FˆÔÔ-Øˆr%   )r   r!   )r…   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   ÚdictÚ__annotations__r$   r0   r.   r?   rD   rJ   rP   r	   r^   r[   r{   r�   r�   Ú__classcell__)r–   s   @r#   r   r      sá  ø€ € € € € € ðgð gðT "˜ (¨A¨t¸FÐCÑCÔCÐDØ"˜( 4¨¨D¸ÐCÑCÔCÐDØ�x ¨!¨T¸&ÐAÑAÔAÐBØ�8˜H a¨°fÐ=Ñ=Ô=Ð>Ø�;Ø'Ð(ð$ð $Ð˜Dð ð ñ ð ð)ð ØØØØð)ð )ð )ð )ð )ð"%ð %ð %ð(ð ð ð"-ð -ð -ð&-ð -ð -ð*ð ð ð"ð ð ð* €\°Ð5Ñ5Ô5ð')ð ')ð ')ñ 6Ô5ð')ðR1ð 1ð 1ð<':ð ':ð ':ðR €\°Ð5Ñ5Ô5ð5ð 5ð 5ñ 6Ô5ð5ðnð ð ð ð ð ð ð ð r%   r   )r™   r�   Únumbersr   r   Únumpyr+   Úscipy.sparser’   rn   Úscipy.specialr   Úsklearn.baser   r   r   r	   Úsklearn.utilsr
   r   Úsklearn.utils._param_validationr   Úsklearn.utils.extmathr   Úsklearn.utils.validationr   r   r   © r%   r#   ú<module>r§      s4  ðØ "Ð "ð
 €€€Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð ð ð >Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ Cðdð dð dð dð dÐ2Ð4DÀmñ dô dð dð dð dr%   