U
    ½mœd:  ã                   @   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 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd„ deeeƒZdS )zRestricted Boltzmann Machine
é    N)ÚIntegralÚReal)Úexpité   )ÚBaseEstimator)ÚTransformerMixin)ÚClassNamePrefixFeaturesOutMixin)Úcheck_random_state)Úgen_even_slices)Úsafe_sparse_dot)Úlog_logistic)Úcheck_is_fitted)ÚIntervalc                	   @   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d)dd„Zd d!„ Zd"d#„ Zd*d$d%„Zd&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)
    é   NÚleft)Úclosedr   Z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   úT/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/sklearn/neural_network/_rbm.pyÚ__init__Œ   s    
zBernoulliRBM.__init__c                 C   s,   t | ƒ | j|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   Ú_validate_dataÚ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   r1   Úrngr2   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,   r3   )r&   Údotr.   Úintercept_visible_r   r5   r6   )r   Úhr7   r2   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   ©Zaxis)r   r:   r&   Z	logaddexpr.   r/   r0   Úsum)r   r1   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   r@   r8   r<   )r   r1   Zh_Úv_r   r   r   Úgibbsû   s    
zBernoulliRBM.gibbsc                 C   sâ   |   ¡  t| dƒ }| j|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
ƒs–t | j
¡| _t| dƒs²t |jd ¡| _t| dƒsÐt | 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.
        r.   r!   )r"   r$   r#   r@   r   ç{®Gáz„?r   ÚF)Úorderr0   r:   Ú
h_samples_N)Ú_validate_paramsrA   r%   r&   r'   r	   r   r@   ÚasarrayÚnormalr   r6   r.   Ú_n_features_outÚzerosr0   r:   r   rG   Ú_fit)r   r*   ÚyZ
first_passr   r   r   Úpartial_fit  s6       ÿ

þ
ÿ
ÿ
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)Zdense_outputr=   g      ð?r3   N)r)   r<   rG   Úfloatr   r6   r   r/   r&   r9   r.   r0   r>   r:   rI   Zsqueezer5   Úfloor)r   Zv_posr7   Zh_posZv_negZh_negÚlrÚupdater   r   r   rM   <  s    

& ÿzBernoulliRBM._fitc           	      C   sÈ   t | ƒ | j|ddd�}t| jƒ}t |jd ¡| d|jd |jd ¡f}t 	|¡r†d||  d }|tj
|j ¡ |f|jd� }n| ¡ }d||  ||< |  |¡}|  |¡}|jd t|| ƒ 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   éþÿÿÿ)r6   )r   r%   r	   r   r&   Zaranger6   ÚrandintÚspÚissparseZ
csr_matrixÚAZravelÚcopyr?   r   )	r   r*   r1   r7   ÚindÚdatarB   ZfeZfe_r   r   r   Úscore_samplesZ  s    
*
 

zBernoulliRBM.score_samplesc                 C   sb  |   ¡  | j|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|�r t ¡ }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   rD   r   rE   )rF   r$   )r$   )Ú	n_samplesz9[%s] Iteration %d, pseudo-likelihood = %.2f, time = %.2fs)"rH   r%   r&   r'   r(   r6   r	   r   rI   rJ   r   r$   r.   rK   rL   r0   r:   r   rG   ÚintÚceilrP   Úlistr
   r   ÚtimeÚranger   rM   ÚprintÚtypeÚ__name__r\   Zmean)r   r*   rN   r]   r7   Z	n_batchesZbatch_slicesr   ÚbeginÚ	iterationZbatch_sliceÚendr   r   r   Úfit  sF    

ýÿüÿÿ	zBernoulliRBM.fitc                 C   s   dddœt jt jgdœS )Nz&fails for the decision_function methodz"fails for the score_samples method)Zcheck_methods_subset_invarianceZ%check_methods_sample_order_invariance)Z_xfail_checksZpreserves_dtype)r&   r'   r(   )r   r   r   r   Ú
_more_tags¹  s
    û
÷zBernoulliRBM._more_tags)r   )N)N)re   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   ÚdictÚ__annotations__r    r+   r)   r8   r<   r?   rC   rO   rM   r\   ri   rj   r   r   r   r   r      s6   
gú þø
,%
:r   )rm   ra   Únumbersr   r   Únumpyr&   Zscipy.sparseÚsparserV   Zscipy.specialr   Úbaser   r   r   Úutilsr	   r
   Zutils.extmathr   r   Zutils.validationr   Zutils._param_validationr   r   r   r   r   r   Ú<module>   s   	