
    {Kg;                         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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_fittedc            	          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y)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          _/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/sklearn/neural_network/_rbm.py__init__zBernoulliRBM.__init__   s1     )*$(    c                     t        |        | j                  |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   _validate_datanpfloat64float32_mean_hiddens)r!   Xs     r"   	transformzBernoulliRBM.transform   sJ     	U%

BJJ7O   
 !!!$$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     At//112	T###QA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      FF1d&&'	T$$$aQ{{{(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#:#:;;bllq$"2"2"4"458N8N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_-!3D4E4E!FD!!!T%7%78""2t'9'9:	r$   T)prefer_skip_nested_validationc           	         t        | d       }| j                  |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   {Gz?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  sE   " !}55
U"**J   
 t_-!3D4E4E!FDt]+!zz""))!TD4E4Eqwwqz3RS D $(#3#3#9#9!#<D t01%'XX!!&D" t12&(hh
'D# t\* hh9J9J'KLDO		!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)%%doos;""5)4%%&Q7 %dCEE"&&%((BK'"		q	(9EII1I<M(M"NN2JJuyyay()113eiiQi6GG$
 	
 8;ckku{{k+e34((5%0r$   c                 4   t        |        | j                  |dd      }t        | j                        }t	        j
                  |j                  d         |j                  d|j                  d   |j                  d         f}t        j                  |      rd||   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_samples[  sE   & 	eD !2!23 yy$ckk!QWWQZ&LM;;q>#;?D$		*'<AGGLLtzz|S&9IIB"S'kBsGq!#
{R\\!sRx[999r$   c           	         | j                  |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   " rzz2::>VWGGAJ	 !2!23::JJq$!2!2AGGAJ ?@''

  $//55a8!#$*;*;177!K"$((1771:QWW"E((DOOT5F5F#GqwwWi 04?? BCD	I7iX
 ,,		q$++/2I+		!K.#.  , iikOT
++!**1-224e	  3" r$   c                 N    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  s-     = 9 !#

BJJ7

 
	
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    r$   r"   r   r      s    gT "(AtFCD"4DCD!T&ABHaf=>;'($D  ) )"%("-&-*"* 5') 6')R1<':R 55 65n
r$   r   )r   r   numbersr   r   numpyr+   scipy.sparsesparsern   scipy.specialr   baser   r   r	   r
   utilsr   r   utils._param_validationr   utils.extmathr   utils.validationr   r   r   r$   r"   <module>r      sE    "  "     8 . + .k
24Dm k
r$   