o
    \ix:                     @   s   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 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                	       s   e Zd ZU dZeeddddgeeddddgeeddddgeeddddgdgd	gd
Zee	d< 	d*ddddddddZ
dd Zdd Zdd Zdd Zdd Zdd Zeddd+d d!Zd"d# Zd$d% Zeddd+d&d'Z fd(d)Z  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                C   s(   || _ || _|| _|| _|| _|| _d S Nr   )selfr   r   r   r   r   r    r!   |/var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/lib/python3.10/site-packages/sklearn/neural_network/_rbm.py__init__   s   

zBernoulliRBM.__init__c                 C   s,   t |  t| |ddtjtjfd}| |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    Xr!   r!   r"   	transform   s
   
zBernoulliRBM.transformc                 C   s$   t || jj}|| j7 }t||d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pr!   r!   r"   r+      s   
zBernoulliRBM._mean_hiddensc                 C   s   |  |}|j|jd|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    r3   rngr4   r!   r!   r"   _sample_hiddens   s   
zBernoulliRBM._sample_hiddensc                 C   s6   t || j}|| j7 }t||d |j|jd|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.
        r.   r5   )r(   dotr0   intercept_visible_r   r7   r8   )r    hr9   r4   r!   r!   r"   _sample_visibles   s   
zBernoulliRBM._sample_visiblesc                 C   s2   t || j tdt || jj| j jdd 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   r<   r(   	logaddexpr0   r1   r2   sum)r    r3   r!   r!   r"   _free_energy   s
   zBernoulliRBM._free_energyc                 C   s>   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   rD   r:   r>   )r    r3   h_v_r!   r!   r"   gibbs   s   
zBernoulliRBM.gibbsT)prefer_skip_nested_validationc                 C   s   t | d }t| |dtj|d}t | dst| j| _t | ds;tj| jdd| j	|j
d fdd	| _| jj
d | _t | d
sGt| j	| _t | dsUt|j
d | _t | dsdt| 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.
        r0   r$   )r%   r'   r&   rD   r   {Gz?r   F)orderr2   r<   
h_samples_N)rE   r   r(   r)   r
   r   rD   asarraynormalr   r8   r0   _n_features_outzerosr2   r<   r   rM   _fit)r    r,   y
first_passr!   r!   r"   partial_fit  s.   




zBernoulliRBM.partial_fitc                 C   s   |  |}| | j|}|  |}t| j|jd  }t|j|ddj}|t	|j|8 }|  j
|| 7  _
|  j||jdd|jdd  7  _|  j|t|jdd |jdd  7  _d||j|jd|k < t||| _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_outputr?   g      ?r5   N)r+   r>   rM   floatr   r8   r   r1   r(   r;   r0   r2   rB   r<   rN   squeezer7   floor)r    v_posr9   h_posv_negh_neglrupdater!   r!   r"   rR   :  s   

& zBernoulliRBM._fitc           	      C   s   t |  t| |ddd}t| j}t|jd |d|jd |jd f}t	|rXd||  d }t
|tjrI|tj|j |f|jd }n|tj| |f|jd }n| }d||  ||< | |}| |}|jd  td||   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   )r8   )r   r   r
   r   r(   aranger8   randintspissparse
isinstancematrix
csr_matrixAravel	csr_arraycopyrC   rA   )	r    r,   r3   r9   inddatarG   fefe_r!   r!   r"   score_samplesX  s   
*
 

zBernoulliRBM.score_samplesc                 C   sT  t | |dtjtjfd}|jd }t| j}tj|dd| j	|jd fd|j
d| _| jjd | _tj| j	|j
d| _tj|jd |j
d| _tj| j| j	f|j
d| _ttt|| j }tt|| j ||d	}| j}t }td| jd D ]+}	|D ]
}
| ||
 | q|rt }td
t| j|	| |  || f  |}q|| 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   rJ   r   rK   )rL   r'   )r'   )	n_samplesz9[%s] Iteration %d, pseudo-likelihood = %.2f, time = %.2fs)!r   r(   r)   r*   r8   r
   r   rN   rO   r   r'   r0   rP   rQ   r2   r<   r   rM   intceilrW   listr   r   timeranger   rR   printtype__name__rp   mean)r    r,   rS   rq   r9   	n_batchesbatch_slicesr   begin	iterationbatch_sliceendr!   r!   r"   fit  sF   

	zBernoulliRBM.fitc                    s"   t   }d|j_ddg|j_|S )NTr)   r*   )super__sklearn_tags__
input_tagssparsetransformer_tagspreserves_dtype)r    tags	__class__r!   r"   r     s   
zBernoulliRBM.__sklearn_tags__)r   r   )ry   
__module____qualname____doc__r   r   r   r   dict__annotations__r#   r-   r+   r:   r>   rC   rH   r	   rU   rR   rp   r   r   __classcell__r!   r!   r   r"   r      s<   
 j))7r   )r   ru   numbersr   r   numpyr(   scipy.sparser   rc   scipy.specialr   baser   r   r   r	   utilsr
   r   utils._param_validationr   utils.extmathr   utils.validationr   r   r   r!   r!   r!   r"   <module>   s    