Mercurial > ift6266
annotate baseline_algorithms/log_reg/log_reg.py @ 167:1f5937e9e530
More moves - transformations into data_generation, added "deep" folder
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 14:15:38 -0500 |
parents | d1bb6e06497a |
children |
rev | line source |
---|---|
158
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
1 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
2 This tutorial introduces logistic regression using Theano and stochastic |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
3 gradient descent. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
4 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
5 Logistic regression is a probabilistic, linear classifier. It is parametrized |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
6 by a weight matrix :math:`W` and a bias vector :math:`b`. Classification is |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
7 done by projecting data points onto a set of hyperplanes, the distance to |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
8 which is used to determine a class membership probability. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
9 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
10 Mathematically, this can be written as: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
11 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
12 .. math:: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
13 P(Y=i|x, W,b) &= softmax_i(W x + b) \\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
14 &= \frac {e^{W_i x + b_i}} {\sum_j e^{W_j x + b_j}} |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
15 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
16 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
17 The output of the model or prediction is then done by taking the argmax of |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
18 the vector whose i'th element is P(Y=i|x). |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
19 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
20 .. math:: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
21 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
22 y_{pred} = argmax_i P(Y=i|x,W,b) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
23 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
24 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
25 This tutorial presents a stochastic gradient descent optimization method |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
26 suitable for large datasets, and a conjugate gradient optimization method |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
27 that is suitable for smaller datasets. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
28 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
29 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
30 References: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
31 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
32 - textbooks: "Pattern Recognition and Machine Learning" - |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
33 Christopher M. Bishop, section 4.3.2 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
34 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
35 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
36 __docformat__ = 'restructedtext en' |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
37 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
38 import numpy, time, cPickle, gzip |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
39 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
40 import theano |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
41 import theano.tensor as T |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
42 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
43 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
44 class LogisticRegression(object): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
45 """Multi-class Logistic Regression Class |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
46 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
47 The logistic regression is fully described by a weight matrix :math:`W` |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
48 and bias vector :math:`b`. Classification is done by projecting data |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
49 points onto a set of hyperplanes, the distance to which is used to |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
50 determine a class membership probability. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
51 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
52 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
53 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
54 def __init__( self, input, n_in, n_out ): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
55 """ Initialize the parameters of the logistic regression |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
56 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
57 :type input: theano.tensor.TensorType |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
58 :param input: symbolic variable that describes the input of the |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
59 architecture (one minibatch) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
60 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
61 :type n_in: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
62 :param n_in: number of input units, the dimension of the space in |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
63 which the datapoints lie |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
64 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
65 :type n_out: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
66 :param n_out: number of output units, the dimension of the space in |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
67 which the labels lie |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
68 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
69 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
70 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
71 # initialize with 0 the weights W as a matrix of shape (n_in, n_out) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
72 self.W = theano.shared( value = numpy.zeros(( n_in, n_out ), dtype = theano.config.floatX ), |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
73 name =' W') |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
74 # initialize the baises b as a vector of n_out 0s |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
75 self.b = theano.shared( value = numpy.zeros(( n_out, ), dtype = theano.config.floatX ), |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
76 name = 'b') |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
77 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
78 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
79 # compute vector of class-membership probabilities in symbolic form |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
80 self.p_y_given_x = T.nnet.softmax( T.dot( input, self.W ) + self.b ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
81 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
82 # compute prediction as class whose probability is maximal in |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
83 # symbolic form |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
84 self.y_pred=T.argmax( self.p_y_given_x, axis =1 ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
85 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
86 # parameters of the model |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
87 self.params = [ self.W, self.b ] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
88 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
89 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
90 def negative_log_likelihood( self, y ): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
91 """Return the mean of the negative log-likelihood of the prediction |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
92 of this model under a given target distribution. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
93 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
94 .. math:: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
95 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
96 \frac{1}{|\mathcal{D}|} \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
97 \frac{1}{|\mathcal{D}|} \sum_{i=0}^{|\mathcal{D}|} \log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
98 \ell (\theta=\{W,b\}, \mathcal{D}) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
99 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
100 :type y: theano.tensor.TensorType |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
101 :param y: corresponds to a vector that gives for each example the |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
102 correct label |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
103 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
104 Note: we use the mean instead of the sum so that |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
105 the learning rate is less dependent on the batch size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
106 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
107 # y.shape[0] is (symbolically) the number of rows in y, i.e., number of examples (call it n) in the minibatch |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
108 # T.arange(y.shape[0]) is a symbolic vector which will contain [0,1,2,... n-1] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
109 # T.log(self.p_y_given_x) is a matrix of Log-Probabilities (call it LP) with one row per example and one column per class |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
110 # LP[T.arange(y.shape[0]),y] is a vector v containing [LP[0,y[0]], LP[1,y[1]], LP[2,y[2]], ..., LP[n-1,y[n-1]]] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
111 # and T.mean(LP[T.arange(y.shape[0]),y]) is the mean (across minibatch examples) of the elements in v, |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
112 # i.e., the mean log-likelihood across the minibatch. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
113 return -T.mean( T.log( self.p_y_given_x )[ T.arange( y.shape[0] ), y ] ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
114 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
115 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
116 def errors( self, y ): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
117 """Return a float representing the number of errors in the minibatch |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
118 over the total number of examples of the minibatch ; zero one |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
119 loss over the size of the minibatch |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
120 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
121 :type y: theano.tensor.TensorType |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
122 :param y: corresponds to a vector that gives for each example the |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
123 correct label |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
124 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
125 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
126 # check if y has same dimension of y_pred |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
127 if y.ndim != self.y_pred.ndim: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
128 raise TypeError( 'y should have the same shape as self.y_pred', |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
129 ( 'y', target.type, 'y_pred', self.y_pred.type ) ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
130 # check if y is of the correct datatype |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
131 if y.dtype.startswith('int'): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
132 # the T.neq operator returns a vector of 0s and 1s, where 1 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
133 # represents a mistake in prediction |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
134 return T.mean( T.neq( self.y_pred, y ) ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
135 else: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
136 raise NotImplementedError() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
137 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
138 def shared_dataset( data_xy ): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
139 """ Function that loads the dataset into shared variables |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
140 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
141 The reason we store our dataset in shared variables is to allow |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
142 Theano to copy it into the GPU memory (when code is run on GPU). |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
143 Since copying data into the GPU is slow, copying a minibatch everytime |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
144 is needed (the default behaviour if the data is not in a shared |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
145 variable) would lead to a large decrease in performance. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
146 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
147 data_x, data_y = data_xy |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
148 shared_x = theano.shared( numpy.asarray( data_x, dtype = theano.config.floatX ) ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
149 shared_y = theano.shared( numpy.asarray( data_y, dtype = theano.config.floatX ) ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
150 # When storing data on the GPU it has to be stored as floats |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
151 # therefore we will store the labels as ``floatX`` as well |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
152 # (``shared_y`` does exactly that). But during our computations |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
153 # we need them as ints (we use labels as index, and if they are |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
154 # floats it doesn't make sense) therefore instead of returning |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
155 # ``shared_y`` we will have to cast it to int. This little hack |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
156 # lets ous get around this issue |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
157 return shared_x, T.cast( shared_y, 'int32' ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
158 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
159 def load_data_pkl_gz( dataset ): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
160 ''' Loads the dataset |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
161 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
162 :type dataset: string |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
163 :param dataset: the path to the dataset (here MNIST) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
164 ''' |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
165 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
166 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
167 # Load Data |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
168 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
169 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
170 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
171 print '... loading data' |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
172 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
173 # Load the dataset |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
174 f = gzip.open(dataset,'rb') |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
175 train_set, valid_set, test_set = cPickle.load(f) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
176 f.close() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
177 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
178 test_set_x, test_set_y = shared_dataset( test_set ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
179 valid_set_x, valid_set_y = shared_dataset( valid_set ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
180 train_set_x, train_set_y = shared_dataset( train_set ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
181 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
182 rval = [ ( train_set_x, train_set_y ), ( valid_set_x,valid_set_y ), ( test_set_x, test_set_y ) ] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
183 return rval |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
184 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
185 ##def load_data_ft( verbose = False,\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
186 ## data_path = '/data/lisa/data/nist/by_class/'\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
187 ## train_data = 'all/all_train_data.ft',\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
188 ## train_labels = 'all/all_train_labels.ft',\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
189 ## test_data = 'all/all_test_data.ft',\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
190 ## test_labels = 'all/all_test_labels.ft'): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
191 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
192 ## train_data_file = open(data_path + train_data) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
193 ## train_labels_file = open(data_path + train_labels) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
194 ## test_labels_file = open(data_path + test_data) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
195 ## test_data_file = open(data_path + test_labels) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
196 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
197 ## raw_train_data = ft.read( train_data_file) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
198 ## raw_train_labels = ft.read(train_labels_file) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
199 ## raw_test_data = ft.read( test_labels_file) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
200 ## raw_test_labels = ft.read( test_data_file) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
201 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
202 ## f.close() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
203 ## g.close() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
204 ## i.close() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
205 ## h.close() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
206 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
207 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
208 ## test_set_x, test_set_y = shared_dataset(test_set) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
209 ## valid_set_x, valid_set_y = shared_dataset(valid_set) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
210 ## train_set_x, train_set_y = shared_dataset(train_set) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
211 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
212 ## rval = [(train_set_x, train_set_y), (valid_set_x,valid_set_y), (test_set_x, test_set_y)] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
213 ## return rval |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
214 ## #create a validation set the same size as the test size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
215 ## #use the end of the training array for this purpose |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
216 ## #discard the last remaining so we get a %batch_size number |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
217 ## test_size=len(raw_test_labels) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
218 ## test_size = int(test_size/batch_size) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
219 ## test_size*=batch_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
220 ## train_size = len(raw_train_data) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
221 ## train_size = int(train_size/batch_size) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
222 ## train_size*=batch_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
223 ## validation_size =test_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
224 ## offset = train_size-test_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
225 ## if verbose == True: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
226 ## print 'train size = %d' %train_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
227 ## print 'test size = %d' %test_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
228 ## print 'valid size = %d' %validation_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
229 ## print 'offset = %d' %offset |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
230 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
231 ## |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
232 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
233 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
234 # MAIN |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
235 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
236 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
237 def log_reg( learning_rate = 0.13, nb_max_examples =1000000, batch_size = 50, \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
238 dataset_name = 'mnist.pkl.gz', image_size = 28 * 28, nb_class = 10, \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
239 patience = 5000, patience_increase = 2, improvement_threshold = 0.995): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
240 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
241 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
242 Demonstrate stochastic gradient descent optimization of a log-linear |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
243 model |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
244 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
245 This is demonstrated on MNIST. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
246 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
247 :type learning_rate: float |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
248 :param learning_rate: learning rate used (factor for the stochastic |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
249 gradient) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
250 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
251 :type nb_max_examples: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
252 :param nb_max_examples: maximal number of epochs to run the optimizer |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
253 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
254 :type batch_size: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
255 :param batch_size: size of the minibatch |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
256 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
257 :type dataset_name: string |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
258 :param dataset: the path of the MNIST dataset file from |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
259 http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
260 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
261 :type image_size: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
262 :param image_size: size of the input image in pixels (width * height) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
263 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
264 :type nb_class: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
265 :param nb_class: number of classes |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
266 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
267 :type patience: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
268 :param patience: look as this many examples regardless |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
269 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
270 :type patience_increase: int |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
271 :param patience_increase: wait this much longer when a new best is found |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
272 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
273 :type improvement_threshold: float |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
274 :param improvement_threshold: a relative improvement of this much is considered significant |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
275 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
276 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
277 """ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
278 datasets = load_data_pkl_gz( dataset_name ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
279 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
280 train_set_x, train_set_y = datasets[0] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
281 valid_set_x, valid_set_y = datasets[1] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
282 test_set_x , test_set_y = datasets[2] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
283 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
284 # compute number of minibatches for training, validation and testing |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
285 n_train_batches = train_set_x.value.shape[0] / batch_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
286 n_valid_batches = valid_set_x.value.shape[0] / batch_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
287 n_test_batches = test_set_x.value.shape[0] / batch_size |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
288 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
289 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
290 # Build actual model |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
291 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
292 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
293 print '... building the model' |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
294 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
295 # allocate symbolic variables for the data |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
296 index = T.lscalar( ) # index to a [mini]batch |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
297 x = T.matrix('x') # the data is presented as rasterized images |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
298 y = T.ivector('y') # the labels are presented as 1D vector of |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
299 # [int] labels |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
300 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
301 # construct the logistic regression class |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
302 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
303 classifier = LogisticRegression( input = x, n_in = image_size, n_out = nb_class ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
304 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
305 # the cost we minimize during training is the negative log likelihood of |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
306 # the model in symbolic format |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
307 cost = classifier.negative_log_likelihood( y ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
308 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
309 # compiling a Theano function that computes the mistakes that are made by |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
310 # the model on a minibatch |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
311 test_model = theano.function( inputs = [ index ], |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
312 outputs = classifier.errors( y ), |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
313 givens = { |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
314 x:test_set_x[ index * batch_size: ( index + 1 ) * batch_size ], |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
315 y:test_set_y[ index * batch_size: ( index + 1 ) * batch_size ] } ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
316 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
317 validate_model = theano.function( inputs = [ index ], |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
318 outputs = classifier.errors( y ), |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
319 givens = { |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
320 x:valid_set_x[ index * batch_size: ( index + 1 ) * batch_size ], |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
321 y:valid_set_y[ index * batch_size: ( index + 1 ) * batch_size ] } ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
322 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
323 # compute the gradient of cost with respect to theta = ( W, b ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
324 g_W = T.grad( cost = cost, wrt = classifier.W ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
325 g_b = T.grad( cost = cost, wrt = classifier.b ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
326 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
327 # specify how to update the parameters of the model as a dictionary |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
328 updates = { classifier.W: classifier.W - learning_rate * g_W,\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
329 classifier.b: classifier.b - learning_rate * g_b} |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
330 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
331 # compiling a Theano function `train_model` that returns the cost, but in |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
332 # the same time updates the parameter of the model based on the rules |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
333 # defined in `updates` |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
334 train_model = theano.function( inputs = [ index ], |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
335 outputs = cost, |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
336 updates = updates, |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
337 givens = { |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
338 x: train_set_x[ index * batch_size: ( index + 1 ) * batch_size ], |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
339 y: train_set_y[ index * batch_size: ( index + 1 ) * batch_size ] } ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
340 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
341 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
342 # Train model |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
343 #-------------------------------------------------------------------------------------------------------------------- |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
344 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
345 print '... training the model' |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
346 # early-stopping parameters |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
347 patience = 5000 # look as this many examples regardless |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
348 patience_increase = 2 # wait this much longer when a new best is |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
349 # found |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
350 improvement_threshold = 0.995 # a relative improvement of this much is |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
351 # considered significant |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
352 validation_frequency = min( n_train_batches, patience * 0.5 ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
353 # go through this many |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
354 # minibatche before checking the network |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
355 # on the validation set; in this case we |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
356 # check every epoch |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
357 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
358 best_params = None |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
359 best_validation_loss = float('inf') |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
360 test_score = 0. |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
361 start_time = time.clock() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
362 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
363 done_looping = False |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
364 n_epochs = nb_max_examples / train_set_x.value.shape[0] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
365 epoch = 0 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
366 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
367 while ( epoch < n_epochs ) and ( not done_looping ): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
368 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
369 epoch = epoch + 1 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
370 for minibatch_index in xrange( n_train_batches ): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
371 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
372 minibatch_avg_cost = train_model( minibatch_index ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
373 # iteration number |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
374 iter = epoch * n_train_batches + minibatch_index |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
375 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
376 if ( iter + 1 ) % validation_frequency == 0: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
377 # compute zero-one loss on validation set |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
378 validation_losses = [ validate_model( i ) for i in xrange( n_valid_batches ) ] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
379 this_validation_loss = numpy.mean( validation_losses ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
380 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
381 print('epoch %i, minibatch %i/%i, validation error %f %%' % \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
382 ( epoch, minibatch_index + 1,n_train_batches, \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
383 this_validation_loss*100. ) ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
384 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
385 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
386 # if we got the best validation score until now |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
387 if this_validation_loss < best_validation_loss: |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
388 #improve patience if loss improvement is good enough |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
389 if this_validation_loss < best_validation_loss * \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
390 improvement_threshold : |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
391 patience = max( patience, iter * patience_increase ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
392 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
393 best_validation_loss = this_validation_loss |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
394 # test it on the test set |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
395 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
396 test_losses = [test_model(i) for i in xrange(n_test_batches)] |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
397 test_score = numpy.mean(test_losses) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
398 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
399 print((' epoch %i, minibatch %i/%i, test error of best ' |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
400 'model %f %%') % \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
401 (epoch, minibatch_index+1, n_train_batches,test_score*100.)) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
402 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
403 if patience <= iter : |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
404 done_looping = True |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
405 break |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
406 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
407 end_time = time.clock() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
408 print(('Optimization complete with best validation score of %f %%,' |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
409 'with test performance %f %%') % |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
410 ( best_validation_loss * 100., test_score * 100.)) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
411 print ('The code ran for %f minutes' % ((end_time-start_time) / 60.)) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
412 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
413 ###### return validation_error, test_error, nb_exemples, time |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
414 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
415 if __name__ == '__main__': |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
416 log_reg() |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
417 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
418 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
419 def jobman_log_reg(state, channel): |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
420 (validation_error, test_error, nb_exemples, time) = log_reg( learning_rate = state.learning_rate,\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
421 nb_max_examples = state.nb_max_examples,\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
422 batch_size = state.batch_size,\ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
423 dataset_name = state.dataset_name, \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
424 image_size = state.image_size, \ |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
425 nb_class = state.nb_class ) |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
426 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
427 state.validation_error = validation_error |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
428 state.test_error = test_error |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
429 state.nb_exemples = nb_exemples |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
430 state.time = time |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
431 return channel.COMPLETE |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
432 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
433 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
434 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
435 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
436 |
d1bb6e06497a
nouveau répertoire régression logistique
Myriam Cote <cotemyri@iro.umontreal.ca>
parents:
diff
changeset
|
437 |