o
    Z&iN                      @   s   d Z ddlZddlmZ ddlmZ dd Zdd Zd	d
 Z	dd Z
dd Zdd Zeee
e	eedZdd Zdd Zdd Zdd ZeeeedZd$ddZd$ddZd$dd Zd$d!d"Zeeeed#ZdS )%z(Utilities for the neural network modules    N)expit)xlogyc                 C      dS )zSimply leave the input array unchanged.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        Data, where `n_samples` is the number of samples
        and `n_features` is the number of features.
    N Xr   r   GC:\wamp64\www\opt\env\Lib\site-packages\sklearn/neural_network/_base.pyinplace_identity       r	   c                 C      t j| | d dS )zCompute the exponential inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.
    outN)npexpr   r   r   r   inplace_exp      r   c                 C   s   t | | d dS )zCompute the logistic function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.
    r   N)logistic_sigmoidr   r   r   r   inplace_logistic"   s   r   c                 C   r   )zCompute the hyperbolic tan function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.
    r   N)r   tanhr   r   r   r   inplace_tanh-   r   r   c                 C   s   t j| d| d dS )zCompute the rectified linear unit function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.
    r   r   N)r   maximumr   r   r   r   inplace_relu8   s   r   c                 C   sN   | | j ddddtjf  }tj|| d | | jddddtjf  } dS )zCompute the K-way softmax function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.
       axisNr   )maxr   Znewaxisr   sum)r   tmpr   r   r   inplace_softmaxC   s   "r   )identityr   r   logisticreluZsoftmaxc                 C   r   )a  Apply the derivative of the identity function: do nothing.

    Parameters
    ----------
    Z : {array-like, sparse matrix}, shape (n_samples, n_features)
        The data which was output from the identity activation function during
        the forward pass.

    delta : {array-like}, shape (n_samples, n_features)
         The backpropagated error signal to be modified inplace.
    Nr   Zdeltar   r   r   inplace_identity_derivativeZ   r
   r%   c                 C   s   || 9 }|d|  9 }dS )a  Apply the derivative of the logistic sigmoid function.

    It exploits the fact that the derivative is a simple function of the output
    value from logistic function.

    Parameters
    ----------
    Z : {array-like, sparse matrix}, shape (n_samples, n_features)
        The data which was output from the logistic activation function during
        the forward pass.

    delta : {array-like}, shape (n_samples, n_features)
         The backpropagated error signal to be modified inplace.
    r   Nr   r"   r   r   r   inplace_logistic_derivativei   s   r&   c                 C   s   |d| d  9 }dS )a  Apply the derivative of the hyperbolic tanh function.

    It exploits the fact that the derivative is a simple function of the output
    value from hyperbolic tangent.

    Parameters
    ----------
    Z : {array-like, sparse matrix}, shape (n_samples, n_features)
        The data which was output from the hyperbolic tangent activation
        function during the forward pass.

    delta : {array-like}, shape (n_samples, n_features)
         The backpropagated error signal to be modified inplace.
    r      Nr   r"   r   r   r   inplace_tanh_derivative|   s   r(   c                 C   s   d|| dk< dS )a  Apply the derivative of the relu function.

    It exploits the fact that the derivative is a simple function of the output
    value from rectified linear units activation function.

    Parameters
    ----------
    Z : {array-like, sparse matrix}, shape (n_samples, n_features)
        The data which was output from the rectified linear units activation
        function during the forward pass.

    delta : {array-like}, shape (n_samples, n_features)
         The backpropagated error signal to be modified inplace.
    r   Nr   r"   r   r   r   inplace_relu_derivative   s   r)   )r   r   r    r!   c                 C   s    dt j| | d |dd  S )a  Compute the squared loss for regression.

    Parameters
    ----------
    y_true : array-like or label indicator matrix
        Ground truth (correct) values.

    y_pred : array-like or label indicator matrix
        Predicted values, as returned by a regression estimator.

    sample_weight : array-like of shape (n_samples,), default=None
        Sample weights.

    Returns
    -------
    loss : float
        The degree to which the samples are correctly predicted.
    g      ?r'   r   weightsr   )r   averagemeany_trueZy_predsample_weightr   r   r   squared_loss   s   r1   c                 C   s&   t jt| | | |  | |dd S )a  Compute (half of the) Poisson deviance loss for regression.

    Parameters
    ----------
    y_true : array-like or label indicator matrix
        Ground truth (correct) labels.

    y_pred : array-like or label indicator matrix
        Predicted values, as returned by a regression estimator.

    sample_weight : array-like of shape (n_samples,), default=None
        Sample weights.

    Returns
    -------
    loss : float
        The degree to which the samples are correctly predicted.
    r   r*   )r   r,   r   r   r.   r   r   r   poisson_loss   s
   r2   c                 C   s   t |jj}t ||d| }|jd dkr!t jd| |dd}| jd dkr2t jd|  | dd} t jt| ||dd	  S )a  Compute Logistic loss for classification.

    Parameters
    ----------
    y_true : array-like or label indicator matrix
        Ground truth (correct) labels.

    y_prob : array-like of float, shape = (n_samples, n_classes)
        Predicted probabilities, as returned by a classifier's
        predict_proba method.

    sample_weight : array-like of shape (n_samples,), default=None
        Sample weights.

    Returns
    -------
    loss : float
        The degree to which the samples are correctly predicted.
    r   r   r   r*   )
r   finfodtypeepsclipshapeappendr,   r   r   r/   Zy_probr0   r5   r   r   r   log_loss   s   r:   c                 C   sN   t |jj}t ||d| }t jt| |td|  d|  |dd  S )a}  Compute binary logistic loss for classification.

    This is identical to log_loss in binary classification case,
    but is kept for its use in multilabel case.

    Parameters
    ----------
    y_true : array-like or label indicator matrix
        Ground truth (correct) labels.

    y_prob : array-like of float, shape = (n_samples, 1)
        Predicted probabilities, as returned by a classifier's
        predict_proba method.

    sample_weight : array-like of shape (n_samples,), default=None
        Sample weights.

    Returns
    -------
    loss : float
        The degree to which the samples are correctly predicted.
    r   r   r*   )r   r3   r4   r5   r6   r,   r   r   r9   r   r   r   binary_log_loss   s   r;   )Zsquared_errorZpoissonr:   r;   )N)__doc__numpyr   Zscipy.specialr   r   r   r	   r   r   r   r   r   ZACTIVATIONSr%   r&   r(   r)   ZDERIVATIVESr1   r2   r:   r;   ZLOSS_FUNCTIONSr   r   r   r   <module>   sF    




!
