§
    §éUg“;  ã                   ó®   — 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  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_fittedc            	       óJ  — 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d„ Zd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          úZ/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/sklearn/neural_network/_rbm.pyÚ__init__zBernoulliRBM.__init__�   s7   € ð )ˆÔØ*ˆÔØ$ˆŒØˆŒØˆŒØ(ˆÔÐÐó    c                 ó¨   — 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   Ú_validate_dataÚnpÚfloat64Úfloat32Ú_mean_hiddens)r"   ÚXs     r#   Ú	transformzBernoulliRBM.transform    sT   € õ 	˜ÑÔÐà×ÒØ˜U¨%½¼
ÅBÄJÐ7Oð  ñ 
ô 
ˆð ×!Ò! !Ñ$Ô$Ð$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"   r8   Úrngr9   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.
        r3   r;   )r,   Údotr5   Úintercept_visible_r   r=   r>   )r"   Úhr?   r9   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   rC   r,   Ú	logaddexpr5   r6   r7   Úsum)r"   r8   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   rM   r@   rE   )r"   r8   Ú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¦  «         }|                      |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.
        r5   r'   )r(   r*   r)   rM   r   ç{®Gáz„?r   ÚF)Úorderr7   rC   Ú
h_samples_N)rN   r+   r,   r-   r   r   rM   ÚasarrayÚnormalr   r>   r5   Ú_n_features_outÚzerosr7   rC   r   rW   Ú_fit)r"   r0   ÚyÚ
first_passs       r#   Úpartial_fitzBernoulliRBM.partial_fit  sx  € õ" !  }Ñ5Ô5Ð5ˆ
Ø×ÒØ˜U­"¬*¸Jð  ñ 
ô 
ˆõ �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_outputrG   g      ð?r;   N)r/   rE   rW   Úfloatr   r>   r   r6   r,   rB   r5   r7   rJ   rC   rX   Ú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                 óF  — 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ÚcopyrK   rI   )	r"   r0   r8   r?   ÚindÚdatarP   ÚfeÚfe_s	            r#   Úscore_sampleszBernoulliRBM.score_samples[  si  € õ& 	˜ÑÔÐà×Ò °¸eÐÑ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           	      óŽ  — |                       |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   rT   r   rU   )rV   r*   )r*   )Ú	n_samplesz9[%s] Iteration %d, pseudo-likelihood = %.2f, time = %.2fs)!r+   r,   r-   r.   r>   r   r   rX   rY   r   r*   r5   rZ   r[   r7   rC   r   rW   ÚintÚceilrb   Úlistr   r   ÚtimeÚranger   r\   ÚprintÚtypeÚ__name__r|   Úmean)r"   r0   r]   r~   r?   Ú	n_batchesÚbatch_slicesr   ÚbeginÚ	iterationÚbatch_sliceÚends               r#   ÚfitzBernoulliRBM.fit„  s  € ð" ×Ò °½r¼zÍ2Ì:Ð>VÐÑ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                 ó>   — dddœt           j        t           j        gdœS )Nz&fails for the decision_function methodz"fails for the score_samples method)Úcheck_methods_subset_invarianceÚ%check_methods_sample_order_invariance)Ú_xfail_checksÚpreserves_dtype)r,   r-   r.   )r"   s    r#   Ú
_more_tagszBernoulliRBM._more_tags¼  s3   € ð =ð 9ðð õ !#¤
­B¬JÐ7ð

ð 

ð 
	
r%   )r   r!   )r†   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   ÚdictÚ__annotations__r$   r1   r/   r@   rE   rK   rQ   r
   r_   r\   r|   rŽ   r”   © r%   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.sparseÚsparsero   Úscipy.specialr   Úbaser   r   r	   r
   Úutilsr   r   Úutils._param_validationr   Úutils.extmathr   Úutils.validationr   r   rš   r%   r#   ú<module>r¥      s.  ðØ "Ð "ð €€€Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð ð ð 8Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø .Ð .Ð .Ð .Ð .Ð .Ø +Ð +Ð +Ð +Ð +Ð +Ø .Ð .Ð .Ð .Ð .Ð .ðk
ð k
ð k
ð k
ð k
Ð2Ð4DÀmñ k
ô k
ð k
ð k
ð k
r%   