ó
þÍSc           @   sé   d  Z  d Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 j
 Z d d l m Z m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 d
 d d d d d d „ Z e d k rå e ƒ  n  d S(   sƒ  
This tutorial introduces the multilayer perceptron using Theano.

 A multilayer perceptron is a logistic regressor where
instead of feeding the input to the logistic regression you insert a
intermediate layer, called the hidden layer, that has a nonlinear
activation function (usually tanh or sigmoid) . One can use many such
hidden layers making the architecture deep. The tutorial will also tackle
the problem of MNIST digit classification.

.. math::

    f(x) = G( b^{(2)} + W^{(2)}( s( b^{(1)} + W^{(1)} x))),

References:

    - textbooks: "Pattern Recognition and Machine Learning" -
                 Christopher M. Bishop, section 5

s   restructedtext eniÿÿÿÿN(   t   LogisticRegressiont	   load_datat   HiddenLayerc           B   s   e  Z d d e j d  „ Z RS(   c      	   C   sg  | |  _  | d k r´ t j | j d t j d | | ƒ d t j d | | ƒ d | | f ƒ d t j j ƒ} | t j	 j
 j k r“ | d 9} n  t j d | d d	 d
 t ƒ } n  | d k rÿ t j | f d t j j ƒ}	 t j d |	 d d d
 t ƒ } n  | |  _ | |  _ t j | |  j ƒ |  j }
 | d k r?|
 n	 | |
 ƒ |  _ |  j |  j g |  _ d S(   sX  
        Typical hidden layer of a MLP: units are fully-connected and have
        sigmoidal activation function. Weight matrix W is of shape (n_in,n_out)
        and the bias vector b is of shape (n_out,).

        NOTE : The nonlinearity used here is tanh

        Hidden unit activation is given by: tanh(dot(input,W) + b)

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dmatrix
        :param input: a symbolic tensor of shape (n_examples, n_in)

        :type n_in: int
        :param n_in: dimensionality of input

        :type n_out: int
        :param n_out: number of hidden units

        :type activation: theano.Op or function
        :param activation: Non linearity to be applied in the hidden
                           layer
        t   lowg      @t   hight   sizet   dtypei   t   valuet   namet   Wt   borrowt   bN(   t   inputt   Nonet   numpyt   asarrayt   uniformt   sqrtt   theanot   configt   floatXt   tensort   nnett   sigmoidt   sharedt   Truet   zerosR	   R   t   Tt   dott   outputt   params(   t   selft   rngR   t   n_int   n_outR	   R   t
   activationt   W_valuest   b_valuest
   lin_output(    (    sB   /net/nas-lsis-3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/CODE/mlp.pyt   __init__(   s$    	!!		N(   t   __name__t
   __module__R   R   t   tanhR'   (    (    (    sB   /net/nas-lsis-3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/CODE/mlp.pyR   '   s   t   MLPc           B   s   e  Z d  Z d „  Z RS(   s¢  Multi-Layer Perceptron Class

    A multilayer perceptron is a feedforward artificial neural network model
    that has one layer or more of hidden units and nonlinear activations.
    Intermediate layers usually have as activation function tanh or the
    sigmoid function (defined here by a ``HiddenLayer`` class)  while the
    top layer is a softamx layer (defined here by a ``LogisticRegression``
    class).
    c         C   sê   t  d | d | d | d | d t j ƒ |  _ t d |  j j d | d | ƒ |  _ t |  j j ƒ j	 ƒ  t |  j j ƒ j	 ƒ  |  _
 |  j j d j	 ƒ  |  j j d j	 ƒ  |  _ |  j j |  _ |  j j |  _ |  j j |  j j |  _ d S(   s§  Initialize the parameters for the multilayer perceptron

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.TensorType
        :param input: symbolic variable that describes the input of the
        architecture (one minibatch)

        :type n_in: int
        :param n_in: number of input units, the dimension of the space in
        which the datapoints lie

        :type n_hidden: int
        :param n_hidden: number of hidden units

        :type n_out: int
        :param n_out: number of output units, the dimension of the space in
        which the labels lie

        R    R   R!   R"   R#   i   N(   R   R   R*   t   hiddenLayerR    R   t   logRegressionLayert   absR	   t   sumt   L1t   L2_sqrt   negative_log_likelihoodt   errorsR   (   R   R    R   R!   t   n_hiddenR"   (    (    sB   /net/nas-lsis-3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/CODE/mlp.pyR'   t   s    (   R(   R)   t   __doc__R'   (    (    (    sB   /net/nas-lsis-3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/CODE/mlp.pyR+   i   s   	g{®Gáz„?g        g-Cëâ6?iè  s   mnist.pkl.gzi   iô  c   1      C   sÙ  t  | ƒ } | d \ } }	 | d \ }
 } | d \ } } | j d t ƒ j d | } |
 j d t ƒ j d | } | j d t ƒ j d | } d GHt j ƒ  } t j d ƒ } t j d ƒ } t j	 j
 d ƒ } t d	 | d
 | d d  d | d d ƒ } | j | ƒ | | j | | j } t j d | g d | j | ƒ d i | | | | d | !| 6| | | | d | !| 6ƒ } t j d | g d | j | ƒ d i |
 | | | d | !| 6| | | | d | !| 6ƒ } g  } x0 | j D]% } t j | | ƒ } | j | ƒ qìWg  } x; t | j | ƒ D]' \ } } | j | | |  | f ƒ q.Wt j d | g d | d | d i | | | | d | !| 6|	 | | | d | !| 6ƒ } d GHd } d } d }  t | | d ƒ }! d }" t j }# d }$ d }% t j ƒ  }& d }' t }( xf|' | k  ry|( ry|' d }' xBt | ƒ D]4}) | |) ƒ }* |' d | |) }+ |+ d |! d k r\g  t | ƒ D] }, | |, ƒ ^ qƒ}- t j |- ƒ }. d |' |) d | |. d f GH|. |# k  r\|. |# |  k  rùt | |+ | ƒ } n  |. }# |+ }$ g  t | ƒ D] }, | |, ƒ ^ q}/ t j |/ ƒ }% d |' |) d | |% d f GHq\n  | |+ k r>t }( Pq>q>WqWt j ƒ  }0 d |# d |$ d |% d f GHt j  d t! j" j# t$ ƒ d d |0 |& d IJd S(!   sÕ  
    Demonstrate stochastic gradient descent optimization for a multilayer
    perceptron

    This is demonstrated on MNIST.

    :type learning_rate: float
    :param learning_rate: learning rate used (factor for the stochastic
    gradient

    :type L1_reg: float
    :param L1_reg: L1-norm's weight when added to the cost (see
    regularization)

    :type L2_reg: float
    :param L2_reg: L2-norm's weight when added to the cost (see
    regularization)

    :type n_epochs: int
    :param n_epochs: maximal number of epochs to run the optimizer

    :type dataset: string
    :param dataset: the path of the MNIST dataset file from
                 http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz


   i    i   i   R
   s   ... building the modelt   xt   yiÒ  R    R   R!   i   R4   R"   i
   t   inputst   outputst   givenst   updatess   ... trainingi'  g×£p=
×ï?g        s1   epoch %i, minibatch %i/%i, validation error %f %%g      Y@s>        epoch %i, minibatch %i/%i, test error of best model %f %%sk   Optimization complete. Best validation score of %f %% obtained at iteration %i, with test performance %f %%s   The code for file s    ran for %.2fmg      N@Ni  (%   R   t	   get_valueR   t   shapeR   t   lscalart   matrixt   ivectorR   t   randomt   RandomStateR+   R2   R0   R1   R   t   functionR3   R   t   gradt   appendt   zipt   minR   t   inft   timet   clockt   Falset   xranget   meant   maxt   syst   stderrt   ost   patht   splitt   __file__(1   t   learning_ratet   L1_regt   L2_regt   n_epochst   datasett
   batch_sizeR4   t   datasetst   train_set_xt   train_set_yt   valid_set_xt   valid_set_yt
   test_set_xt
   test_set_yt   n_train_batchest   n_valid_batchest   n_test_batchest   indexR6   R7   R    t
   classifiert   costt
   test_modelt   validate_modelt   gparamst   paramt   gparamR;   t   train_modelt   patiencet   patience_increaset   improvement_thresholdt   validation_frequencyt   best_paramst   best_validation_losst	   best_itert
   test_scoret
   start_timet   epocht   done_loopingt   minibatch_indext   minibatch_avg_costt   itert   it   validation_lossest   this_validation_losst   test_lossest   end_time(    (    sB   /net/nas-lsis-3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/CODE/mlp.pyt   test_mlp°   s¤    %   	
"
"t   __main__(   R5   t   __docformat__t   cPicklet   gzipRQ   RO   RI   R   R   t   theano.tensorR   R   t   logistic_sgdR    R   t   objectR   R+   R   R(   (    (    (    sB   /net/nas-lsis-3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/CODE/mlp.pyt   <module>   s    BG´