@@ -24,7 +24,7 @@ Classifying MNIST digits using Logistic Regression |
24 | 24 |
|
25 | 25 | .. _floatX: http://deeplearning.net/software/theano/library/config.html#config.floatX |
26 | 26 |
|
27 | | -.. _GPU: http://deeplearning.net/software/theano/tutorial/using_gpu.html |
| 27 | +.. _GPU: http://deeplearning.net/software/theano/tutorial/using_gpu.html |
28 | 28 |
|
29 | 29 | In this section, we show how Theano can be used to implement the most basic |
30 | 30 | classifier: the logistic regression. We start off with a quick primer of the |
@@ -40,7 +40,7 @@ The Model |
40 | 40 | Logistic regression is a probabilistic, linear classifier. It is parametrized |
41 | 41 | by a weight matrix :math:`W` and a bias vector :math:`b`. Classification is |
42 | 42 | done by projecting data points onto a set of hyperplanes, the distance to |
43 | | -which reflects a class membership probability. |
| 43 | +which reflects a class membership probability. |
44 | 44 |
|
45 | 45 | Mathematically, this can be written as: |
46 | 46 |
|
@@ -68,21 +68,21 @@ The code to do this in Theano is the following: |
68 | 68 | b = theano.shared(numpy.zeros((10,)), name = 'b') |
69 | 69 | W = theano.shared(numpy.zeros((784,10)), name = 'W') |
70 | 70 |
|
71 | | - # symbolic expression for computing the vector of |
72 | | - # class-membership probabilities |
| 71 | + # symbolic expression for computing the vector of |
| 72 | + # class-membership probabilities |
73 | 73 | p_y_given_x = T.nnet.softmax(T.dot(x,W)+b) |
74 | 74 |
|
75 | 75 | # compiled Theano function that returns the vector of class-membership |
76 | 76 | # probabilities |
77 | 77 | get_p_y_given_x = theano.function( inputs = [x], outputs = p_y_given_x) |
78 | 78 |
|
79 | | - # print the probability of some example represented by x_value |
| 79 | + # print the probability of some example represented by x_value |
80 | 80 | # x_value is not a symbolic variable but a numpy array describing the |
81 | 81 | # datapoint |
82 | 82 | print 'Probability that x is of class %i is %f' % (i, get_p_y_given_x(x_value)[i]) |
83 | 83 |
|
84 | | - # symbolic description of how to compute prediction as class whose probability |
85 | | - # is maximal |
| 84 | + # symbolic description of how to compute prediction as class whose probability |
| 85 | + # is maximal |
86 | 86 | y_pred = T.argmax(p_y_given_x) |
87 | 87 |
|
88 | 88 | # compiled theano function that returns this value |
@@ -95,7 +95,7 @@ training, we allocate shared variables for :math:`W,b`. |
95 | 95 | This declares them both as being symbolic Theano variables, but also |
96 | 96 | initializes their contents. The dot and softmax operators are then used to compute the vector |
97 | 97 | :math:`P(Y|x, W,b)`. The resulting variable p_y_given_x is a symbolic variable |
98 | | -of vector-type. |
| 98 | +of vector-type. |
99 | 99 |
|
100 | 100 | Up to this point, we have only defined the graph of computations which Theano |
101 | 101 | should perform. To get the actual numerical value of :math:`P(Y|x, W,b)`, we |
@@ -132,7 +132,7 @@ us first start by defining the likelihood :math:`\cal{L}` and loss |
132 | 132 |
|
133 | 133 | .. math:: |
134 | 134 |
|
135 | | - \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = |
| 135 | + \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = |
136 | 136 | \sum_{i=0}^{|\mathcal{D}|} \log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\ |
137 | 137 | \ell (\theta=\{W,b\}, \mathcal{D}) = - \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) |
138 | 138 |
|
@@ -175,29 +175,29 @@ similar to what we have covered so far, and should be self explanatory. |
175 | 175 | """ Initialize the parameters of the logistic regression |
176 | 176 |
|
177 | 177 | :type input: theano.tensor.TensorType |
178 | | - :param input: symbolic variable that describes the input of the |
| 178 | + :param input: symbolic variable that describes the input of the |
179 | 179 | architecture (e.g., one minibatch of input images) |
180 | 180 |
|
181 | 181 | :type n_in: int |
182 | | - :param n_in: number of input units, the dimension of the space in |
| 182 | + :param n_in: number of input units, the dimension of the space in |
183 | 183 | which the datapoint lies |
184 | 184 |
|
185 | 185 | :type n_out: int |
186 | | - :param n_out: number of output units, the dimension of the space in |
| 186 | + :param n_out: number of output units, the dimension of the space in |
187 | 187 | which the target lies |
188 | | - """ |
| 188 | + """ |
189 | 189 |
|
190 | | - # initialize with 0 the weights W as a matrix of shape (n_in, n_out) |
| 190 | + # initialize with 0 the weights W as a matrix of shape (n_in, n_out) |
191 | 191 | self.W = theano.shared( value=numpy.zeros((n_in,n_out), |
192 | 192 | dtype = theano.config.floatX), name ='W' ) |
193 | 193 | # initialize the baises b as a vector of n_out 0s |
194 | | - self.b = theano.shared( value=numpy.zeros((n_out,), |
| 194 | + self.b = theano.shared( value=numpy.zeros((n_out,), |
195 | 195 | dtype = theano.config.floatX), name = 'b' ) |
196 | 196 |
|
197 | 197 | # compute vector of class-membership probabilities in symbolic form |
198 | 198 | self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)+self.b) |
199 | 199 |
|
200 | | - # compute prediction as class whose probability is maximal in |
| 200 | + # compute prediction as class whose probability is maximal in |
201 | 201 | # symbolic form |
202 | 202 | self.y_pred=T.argmax(self.p_y_given_x, axis=1) |
203 | 203 |
|
@@ -208,9 +208,9 @@ similar to what we have covered so far, and should be self explanatory. |
208 | 208 |
|
209 | 209 | .. math:: |
210 | 210 |
|
211 | | - \frac{1}{|\mathcal{D}|} \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = |
| 211 | + \frac{1}{|\mathcal{D}|} \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = |
212 | 212 | \frac{1}{|\mathcal{D}|} \sum_{i=0}^{|\mathcal{D}|} \log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\ |
213 | | - \ell (\theta=\{W,b\}, \mathcal{D}) |
| 213 | + \ell (\theta=\{W,b\}, \mathcal{D}) |
214 | 214 |
|
215 | 215 |
|
216 | 216 | :param y: corresponds to a vector that gives for each example the |
@@ -263,7 +263,7 @@ with respect to the parameters: in this case :math:`\partial{\ell}/\partial{W}`, |
263 | 263 | and :math:`\partial{\ell}/\partial{b}`, This can get pretty tricky for complex |
264 | 264 | models, as expressions for :math:`\partial{\ell}/\partial{\theta}` can get |
265 | 265 | fairly complex, especially when taking into account problems of numerical |
266 | | -stability. |
| 266 | +stability. |
267 | 267 |
|
268 | 268 | With Theano, this work is greatly simplified as it performs |
269 | 269 | automatic differentiation and applies certain math transforms to improve |
@@ -284,19 +284,19 @@ follows: |
284 | 284 |
|
285 | 285 | .. code-block:: python |
286 | 286 |
|
287 | | - # compute the gradient of cost with respect to theta = (W,b) |
| 287 | + # compute the gradient of cost with respect to theta = (W,b) |
288 | 288 | g_W = T.grad(cost = cost, wrt = classifier.W) |
289 | 289 | g_b = T.grad(cost = cost, wrt = classifier.b) |
290 | 290 |
|
291 | 291 | # specify how to update the parameters of the model as a dictionary |
292 | 292 | updates ={classifier.W: classifier.W - learning_rate*g_W,\ |
293 | 293 | classifier.b: classifier.b - learning_rate*g_b} |
294 | 294 |
|
295 | | - # compiling a Theano function `train_model` that returns the cost, but in |
296 | | - # the same time updates the parameter of the model based on the rules |
| 295 | + # compiling a Theano function `train_model` that returns the cost, but in |
| 296 | + # the same time updates the parameter of the model based on the rules |
297 | 297 | # defined in `updates` |
298 | | - train_model = theano.function(inputs = [index], |
299 | | - outputs = cost, |
| 298 | + train_model = theano.function(inputs = [index], |
| 299 | + outputs = cost, |
300 | 300 | updates = updates, |
301 | 301 | givens={ |
302 | 302 | x:train_set_x[index*batch_size:(index+1)*batch_size], |
@@ -305,20 +305,20 @@ follows: |
305 | 305 |
|
306 | 306 |
|
307 | 307 | The ``updates`` dictionary contains, for each parameter, the |
308 | | -stochastic gradient update operation. The ``givens`` dictionary indicates with |
| 308 | +stochastic gradient update operation. The ``givens`` dictionary indicates with |
309 | 309 | what to replace certain variables of the graph. The function ``train_model`` is then |
310 | 310 | defined such that: |
311 | 311 |
|
312 | | -* the input is the mini-batch index ``index`` that together with the batch |
313 | | - size( which is not an input since it is fixed) defines :math:`x` with |
| 312 | +* the input is the mini-batch index ``index`` that together with the batch |
| 313 | + size( which is not an input since it is fixed) defines :math:`x` with |
314 | 314 | corresponding labels :math:`y` |
315 | 315 | * the return value is the cost/loss associated with the x, y defined by |
316 | | - the ``index`` |
| 316 | + the ``index`` |
317 | 317 | * on every function call, it will first replace ``x`` and ``y`` with the |
318 | 318 | corresponding slices from the training set as defined by the |
319 | 319 | ``index`` and afterwards it will evaluate the cost |
320 | 320 | associated with that minibatch and apply the operations defined by the |
321 | | - ``updates`` dictionary. |
| 321 | + ``updates`` dictionary. |
322 | 322 |
|
323 | 323 | Each time ``train_model(index)`` function is called, it will thus compute and |
324 | 324 | return the appropriate cost, while also performing a step of MSGD. The entire |
@@ -344,9 +344,9 @@ The code is as follows: |
344 | 344 | ... |
345 | 345 |
|
346 | 346 | def errors(self, y): |
347 | | - """Return a float representing the number of errors in the minibatch |
| 347 | + """Return a float representing the number of errors in the minibatch |
348 | 348 | over the total number of examples of the minibatch ; zero |
349 | | - one loss over the size of the minibatch |
| 349 | + one loss over the size of the minibatch |
350 | 350 | """ |
351 | 351 | return T.mean(T.neq(self.y_pred, y)) |
352 | 352 |
|
@@ -355,21 +355,21 @@ We then create a function ``test_model`` and a function ``validate_model``, whic |
355 | 355 | value. As you will see shortly, ``validate_model`` is key to our early-stopping |
356 | 356 | implementation (see :ref:`opt_early_stopping`). Both of these function |
357 | 357 | will get as input a batch offset and will compute the number of |
358 | | -missclassified examples for that mini-batch. The only difference between them |
| 358 | +missclassified examples for that mini-batch. The only difference between them |
359 | 359 | is that one draws its batches from the testing set, while |
360 | 360 | the other from the validation set. |
361 | 361 |
|
362 | 362 | .. code-block:: python |
363 | 363 |
|
364 | | - # compiling a Theano function that computes the mistakes that are made by |
| 364 | + # compiling a Theano function that computes the mistakes that are made by |
365 | 365 | # the model on a minibatch |
366 | | - test_model = theano.function(inputs = [index], |
| 366 | + test_model = theano.function(inputs = [index], |
367 | 367 | outputs = classifier.errors(y), |
368 | 368 | givens={ |
369 | 369 | x:test_set_x[index*batch_size:(index+1)*batch_size], |
370 | 370 | y:test_set_y[index*batch_size:(index+1)*batch_size]}) |
371 | 371 |
|
372 | | - validate_model = theano.function( inputs = [index], |
| 372 | + validate_model = theano.function( inputs = [index], |
373 | 373 | outputs = classifier.errors(y), |
374 | 374 | givens={ |
375 | 375 | x:valid_set_x[index*batch_size:(index+1)*batch_size], |
@@ -407,14 +407,14 @@ The output one should expect is of the form : |
407 | 407 | On an Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00 Ghz the code runs with |
408 | 408 | approximately 1.936 epochs/sec and it took 75 epochs to reach a test |
409 | 409 | error of 7.489%. On the GPU the code does almost 10.0 epochs/sec. For this |
410 | | -instance we used a batch size of 600. |
| 410 | +instance we used a batch size of 600. |
411 | 411 |
|
412 | 412 | .. rubric:: Footnotes |
413 | 413 |
|
414 | 414 | .. [#f1] For smaller datasets and simpler models, more sophisticated descent |
415 | 415 | algorithms can be more effective. The sample code |
416 | 416 | `logistic_cg.py <http://deeplearning.net/tutorial/code/logistic_cg.py>`_ |
417 | | - demonstrates how to use SciPy's conjugate gradient solver with Theano |
| 417 | + demonstrates how to use SciPy's conjugate gradient solver with Theano |
418 | 418 | on the logistic regression task. |
419 | 419 |
|
420 | 420 |
|