Ë
    ÷Q(hx:  ã                   óª   — 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y)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            	       ó$  ‡ — 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_constraintsgš™™™™™¹?é
   )r   r   r   r   r   c                óX   — || _         || _        || _        || _        || _        || _        y ©Nr   )Úselfr   r   r   r   r   r   s          úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sklearn/neural_network/_rbm.pyÚ__init__zBernoulliRBM.__init__Œ   s1   € ð )ˆÔØ*ˆÔØ$ˆŒØˆŒØˆŒØ(ˆÕó    c                 ó–   — t        | «       t        | |ddt        j                  t        j                  f¬«      }| j                  |«      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�   sA   € ô 	˜ÔäØ�! 5°¼b¿j¹jÌ"Ï*É*Ð=Uô
ˆð ×!Ñ! !Ó$Ð$r%   c                 óz   — 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±   s8   € ô ˜A˜t×/Ñ/×1Ñ1Ó2ˆØ	ˆT×#Ñ#Ñ#ˆÜ�Q˜AŒÐr%   c                 ób   — | j                  |«      }|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Â   s.   € ð  ×Ñ˜qÓ!ˆØ�{‰{ §¡ˆ{Ó(¨1Ñ,Ð,r%   c                 ó¸   — t        j                  || j                  «      }|| j                  z  }t	        ||¬«       |j                  |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Õ   sM   € ô  �F‰F�1�d×&Ñ&Ó'ˆØ	ˆT×$Ñ$Ñ$ˆÜˆa�Q�Ø�{‰{ §¡ˆ{Ó(¨1Ñ,Ð,r%   c                 óÔ   — t        || j                  «       t        j                  dt        || j                  j
                  «      | j                  z   «      j                  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ê   sZ   € ô    4×#:Ñ#:Ó;Ð;¼b¿l¹lØŒ˜q $×"2Ñ"2×"4Ñ"4Ó5¸×8NÑ8NÑNó?
ç
‰#�1ˆ#‹+ñð 	r%   c                 óØ   — t        | «       t        | d«      st        | j                  «      | _        | j                  || 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û   s^   € ô 	˜ÔÜ�t˜_Ô-Ü!3°D×4EÑ4EÓ!FˆDÔØ×!Ñ! ! T×%7Ñ%7Ó8ˆØ×"Ñ" 2 t×'9Ñ'9Ó:ˆàˆ	r%   T)Úprefer_skip_nested_validationc           	      ó  — t        | d«       }t        | |dt        j                  |¬«      }t        | d«      st	        | j
                  «      | _        t        | d«      snt        j                  | 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«      s0t        j                  | j                   | j                  f«      | _        | j%                  || j                  «       y)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  s<  € ô" !  }Ó5Ð5ˆ
ÜØ�! 5´·
±
À*ô
ˆô �t˜_Ô-Ü!3°D×4EÑ4EÓ!FˆDÔÜ�t˜]Ô+Ü!Ÿz™zØ×"Ñ"×)Ñ)¨!¨T°D×4EÑ4EÀqÇwÁwÈqÁzÐ3RÓSØô ˆDÔð $(×#3Ñ#3×#9Ñ#9¸!Ñ#<ˆDÔ Ü�tÐ0Ô1Ü%'§X¡XØ×!Ñ!ó&ˆDÔ"ô �tÐ1Ô2Ü&(§h¡hØ—‘˜‘
ó'ˆDÔ#ô �t˜\Ô*Ü Ÿh™h¨¯©¸×9JÑ9JÐ'KÓLˆDŒOà�	‰	�!�T×'Ñ'Õ(r%   c                 ó,  — | j                  |«      }| j                  | j                  |«      }| j                  |«      }t        | j                  «      |j
                  d   z  }t        |j                  |d¬«      j                  }|t        j                  |j                  |«      z  }| xj                  ||z  z  c_
        | xj                  ||j                  d¬«      |j                  d¬«      z
  z  z  c_        | xj                  |t        j                  |j                  d¬«      «      j                  «       |j                  d¬«      z
  z  z  c_        d||j!                  |j
                  ¬«      |k  <   t        j"                  ||«      | _        y)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:  sH  € ð ×"Ñ" 5Ó)ˆØ×%Ñ% d§o¡o°sÓ;ˆØ×"Ñ" 5Ó)ˆä�4×%Ñ%Ó&¨¯©°Q©Ñ7ˆÜ  §¡¨%¸dÔC×EÑEˆØ”"—&‘&˜Ÿ™ %Ó(Ñ(ˆØ×Ò˜B ™KÑ'ÕØ×Ò "¨¯	©	°q¨	Ó(9¸E¿I¹IÈ1¸IÓ<MÑ(MÑ"NÑNÕØ×Ò 2Ü�J‰J�u—y‘y a�yÓ(Ó)×1Ñ1Ó3°e·i±iÀQ°iÓ6GÑGñ$
ñ 	
Õð 8;ˆˆc�k‰k˜uŸ{™{ˆkÓ+¨eÑ3Ñ4ÜŸ(™( 5¨%Ó0ˆ�r%   c                 ó*  — t        | «       t        | |dd¬«      }t        | j                  «      }t	        j
                  |j                  d   «      |j                  d|j                  d   |j                  d   «      f}t        j                  |«      r™d||   z  dz   }t        |t        j                  «      r?|t        j                  |j                  j                  «       |f|j                  ¬«      z   }nP|t        j                  |j                  «       |f|j                  ¬«      z   }n|j!                  «       }d||   z
  ||<   | j#                  |«      }| j#                  |«      }|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  s>  € ô& 	˜Ôä˜$ °¸eÔDˆÜ  ×!2Ñ!2Ó3ˆô �y‰y˜Ÿ™ ™Ó$ c§k¡k°!°Q·W±W¸Q±ZÀÇÁÈÁÓ&LÐMˆÜ�;‰;�qŒ>Ø˜˜#™‘; ‘?ˆDÜ˜$¤§	¡	Ô*ØœŸ™¨¯©¯©«¸Ð'<ÀAÇGÁGÔLÑL‘àœŸ™ t§z¡z£|°SÐ&9ÀÇÁÔIÑI‘à—‘“ˆBØ˜"˜S™'‘kˆBˆs‰Gà×Ñ˜qÓ!ˆØ×Ñ Ó#ˆà—‘˜‘
ˆ{œRŸ\™\¨!¨s°R©x¨[Ó9Ñ9Ð9r%   c           	      óä  — t        | |dt        j                  t        j                  f¬«      }|j                  d   }t        | j                  «      }t        j                  |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        j0                  «       }t3        d| j4                  dz   «      D ]|  }	|D ]  }
| j7                  ||
   |«       Œ |sŒ"t1        j0                  «       }t9        d
t;        | «      j<                  |	| j?                  |«      jA                  «       ||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¹zÌ2Ï:É:Ð>VÔWˆØ—G‘G˜A‘Jˆ	Ü  ×!2Ñ!2Ó3ˆäŸ:™:Ø�J‰J�q˜$ ×!2Ñ!2°A·G±G¸A±JÐ ?Ó@ØØ—'‘'ô
ˆÔð
  $×/Ñ/×5Ñ5°aÑ8ˆÔÜ!#§¡¨$×*;Ñ*;À1Ç7Á7Ô!KˆÔÜ"$§(¡(¨1¯7©7°1©:¸Q¿W¹WÔ"EˆÔÜŸ(™( D§O¡O°T×5FÑ5FÐ#GÈqÏwÉwÔWˆŒäœŸ™¤ iÓ 0°4·?±?Ñ BÓCÓDˆ	ÜÜ˜I¨¯©Ñ7¸ÈiÔXó
ˆð —,‘,ˆÜ—	‘	“ˆÜ˜q $§+¡+°¡/Ó2ò 	ˆIØ+ò /�Ø—	‘	˜!˜K™.¨#Õ.ð/ò Ü—i‘i“k�ÜØOä˜T›
×+Ñ+Ø!Ø×*Ñ*¨1Ó-×2Ñ2Ó4Ø˜e™ð	ñôð ‘ð	ð" ˆr%   c                 ól   •— 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__¹  s4   ø€ Ü‰wÑ'Ó)ˆØ!%ˆ�‰ÔØ1:¸IÐ0Fˆ×ÑÔ-Øˆ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ñT " (¨A¨t¸FÔCÐDÙ" 4¨¨D¸ÔCÐDÙ ¨!¨T¸&ÔAÐBÙ˜H a¨°fÔ=Ð>Ø�;Ø'Ð(ñ$Ð˜Dó ð ð)ð ØØØØô)ò"%ò(ò"-ò&-ò*ò"ñ* °Ô5ò')ó 6ð')òR1ò<':ñR °Ô5ò5ó 6ð5÷nð r%   r   )rš   r�   Únumbersr   r   Únumpyr+   Úscipy.sparser’   rn   Úscipy.specialr   Úbaser   r   r	   r
   Úutilsr   r   Úutils._param_validationr   Úutils.extmathr   Úutils.validationr   r   r   © r%   r#   ú<module>r¨      sE   ðÙ "ó
 ß "ã Ý Ý ÷ó ÷ 8Ý .Ý +ß =ôdÐ2Ð4DÀmõ dr%   