ó
‡ˆ\c           @   sø   d  Z  d d l Z d d l m Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 i e d	 6e d
 6e d 6e	 d 6e
 d 6Z d „  Z d „  Z d „  Z d „  Z i e d	 6e d
 6e d 6e d 6Z d „  Z d „  Z d „  Z i e d 6e d 6e d 6Z d S(   s)   Utilities for the neural network modules
iÿÿÿÿN(   t   expitc         C   s   |  S(   sj  Simply return the input array.

    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.

    Returns
    -------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        Same as the input data.
    (    (   t   X(    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   identity   s    c         C   s   t  |  d |  ƒS(   s#  Compute the logistic function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.

    Returns
    -------
    X_new : {array-like, sparse matrix}, shape (n_samples, n_features)
        The transformed data.
    t   out(   t   logistic_sigmoid(   R   (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   logistic   s    c         C   s   t  j |  d |  ƒS(   s)  Compute the hyperbolic tan function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.

    Returns
    -------
    X_new : {array-like, sparse matrix}, shape (n_samples, n_features)
        The transformed data.
    R   (   t   npt   tanh(   R   (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyR   -   s    c         C   s,   t  j |  d t  j |  j ƒ j d |  ƒ|  S(   s0  Compute the rectified linear unit function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.

    Returns
    -------
    X_new : {array-like, sparse matrix}, shape (n_samples, n_features)
        The transformed data.
    i    R   (   R   t   clipt   finfot   dtypet   max(   R   (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   relu=   s    (c         C   si   |  |  j  d d ƒ d d … t j f } t j | d |  ƒ|  |  j d d ƒ d d … t j f :}  |  S(   s(  Compute the K-way softmax function inplace.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        The input data.

    Returns
    -------
    X_new : {array-like, sparse matrix}, shape (n_samples, n_features)
        The transformed data.
    t   axisi   NR   (   R   R   t   newaxist   expt   sum(   R   t   tmp(    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   softmaxN   s    ))R   R   R   R   R   c         C   s   d S(   s„  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.
    N(    (   t   Zt   delta(    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   inplace_identity_derivativef   s    c         C   s   | |  9} | d |  9} d S(   só  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.
    i   N(    (   R   R   (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   inplace_logistic_derivativeu   s    
c         C   s   | d |  d 9} d S(   sý  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.
    i   i   N(    (   R   R   (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   inplace_tanh_derivativeˆ   s    c         C   s   d | |  d k <d S(   s  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.
    i    N(    (   R   R   (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   inplace_relu_derivativeš   s    c         C   s   |  | d j  ƒ  d S(   s„  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.

    Returns
    -------
    loss : float
        The degree to which the samples are correctly predicted.
    i   (   t   mean(   t   y_truet   y_pred(    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   squared_loss²   s    c         C   sŸ   t  j | d d ƒ } | j d d k rG t  j d | | d d ƒ} n  |  j d d k ry t  j d |  |  d d ƒ}  n  t  j |  t  j | ƒ ƒ | j d S(   s°  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.

    Returns
    -------
    loss : float
        The degree to which the samples are correctly predicted.
    g»½×Ùß|Û=i   R   i    gAòÿÿÿï?(   R   R   t   shapet   appendR   t   log(   R   t   y_prob(    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   log_lossÅ   s    c         C   sT   t  j | d d ƒ } t  j |  t  j | ƒ d |  t  j d | ƒ ƒ | j d S(   s)  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, n_classes)
        Predicted probabilities, as returned by a classifier's
        predict_proba method.

    Returns
    -------
    loss : float
        The degree to which the samples are correctly predicted.
    g»½×Ùß|Û=i   i    gAòÿÿÿï?(   R   R   R   R   R   (   R   R    (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   binary_log_lossá   s    R   R!   R"   (   t   __doc__t   numpyR   t   scipy.specialR    R   R   R   R   R   R   t   ACTIVATIONSR   R   R   R   t   DERIVATIVESR   R!   R"   t   LOSS_FUNCTIONS(    (    (    s;   lib/python2.7/site-packages/sklearn/neural_network/_base.pyt   <module>   s,   									

			