@@ -39,55 +39,40 @@ The Model |
39 | 39 |
|
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 | | -done by projecting data points onto a set of hyperplanes, the distance to |
43 | | -which reflects a class membership probability. |
| 42 | +done by projecting an input vector onto a set of hyperplanes, each of which |
| 43 | +corresponds to a class. The distance from the input to a hyperplane reflects |
| 44 | +the probability that the input is a member of the corresponding class. |
44 | 45 |
|
45 | | -Mathematically, this can be written as: |
| 46 | +Mathematically, the probability that an input vector :math:`x` is a member of a |
| 47 | +class :math:`i`, a value of a stochastic variable :math:`Y`, can be written as: |
46 | 48 |
|
47 | 49 | .. math:: |
48 | 50 | P(Y=i|x, W,b) &= softmax_i(W x + b) \\ |
49 | 51 | &= \frac {e^{W_i x + b_i}} {\sum_j e^{W_j x + b_j}} |
50 | 52 |
|
51 | | -The output of the model or prediction is then done by taking the argmax of the vector whose i'th element is P(Y=i|x). |
| 53 | +The model's prediction :math:`y_{pred}` is the class whose probability is maximal, specifically: |
52 | 54 |
|
53 | 55 | .. math:: |
54 | 56 | y_{pred} = {\rm argmax}_i P(Y=i|x,W,b) |
55 | 57 |
|
56 | 58 | The code to do this in Theano is the following: |
57 | 59 |
|
58 | | -.. literalinclude:: ../code/logistic_sgd.py |
59 | | - :start-after: index = T.lscalar() |
60 | | - :end-before: # construct the logistic regression class |
61 | | - |
62 | | - |
63 | 60 | .. literalinclude:: ../code/logistic_sgd.py |
64 | 61 | :start-after: start-snippet-1 |
65 | 62 | :end-before: end-snippet-1 |
66 | 63 |
|
67 | | -We first start by allocating symbolic variables for the inputs :math:`x,y`. |
68 | 64 | Since the parameters of the model must maintain a persistent state throughout |
69 | | -training, we allocate shared variables for :math:`W,b`. |
70 | | -This declares them both as being symbolic Theano variables, but also |
71 | | -initializes their contents. The dot and softmax operators are then used to compute the vector |
72 | | -:math:`P(Y|x, W,b)`. The resulting variable p_y_given_x is a symbolic variable |
73 | | -of vector-type. |
74 | | - |
75 | | -Up to this point, we have only defined the graph of computations which Theano |
76 | | -should perform. To get the actual numerical value of :math:`P(Y|x, W,b)`, we |
77 | | -must create a function ``get_p_y_given_x``, which takes as input ``x`` and |
78 | | -returns ``p_y_given_x``. We can then index its return value with the |
79 | | -index :math:`i` to get the membership probability of the :math:`i` th class. |
80 | | - |
81 | | -Now let's finish building the Theano graph. To get the actual model |
82 | | -prediction, we can use the ``T.argmax`` operator, which will return the index at |
83 | | -which ``p_y_given_x`` is maximal (i.e. the class with maximum probability). |
84 | | - |
85 | | -Again, to calculate the actual prediction for a given input, we construct a |
86 | | -function ``classify``. This function takes as argument a batch of inputs x (as a matrix), |
87 | | -and outputs a vector containing the predicted class for each example (row) in x. |
88 | | - |
89 | | -Now of course, the model we have defined so far does not do anything useful yet, |
90 | | -since its parameters are still in their initial random state. The following |
| 65 | +training, we allocate shared variables for :math:`W,b`. This declares them both |
| 66 | +as being symbolic Theano variables, but also initializes their contents. The |
| 67 | +dot and softmax operators are then used to compute the vector :math:`P(Y|x, |
| 68 | +W,b)`. The result ``p_y_given_x`` is a symbolic variable of vector-type. |
| 69 | + |
| 70 | +To get the actual model prediction, we can use the ``T.argmax`` operator, which |
| 71 | +will return the index at which ``p_y_given_x`` is maximal (i.e. the class with |
| 72 | +maximum probability). |
| 73 | + |
| 74 | +Now of course, the model we have defined so far does not do anything useful |
| 75 | +yet, since its parameters are still in their initial state. The following |
91 | 76 | section will thus cover how to learn the optimal parameters. |
92 | 77 |
|
93 | 78 |
|
@@ -146,25 +131,25 @@ We instantiate this class as follows: |
146 | 131 | :start-after: index = T.lscalar() |
147 | 132 | :end-before: # the cost we minimize during |
148 | 133 |
|
149 | | -Note that the inputs x and y are defined outside the scope of the |
150 | | -``LogisticRegression`` object. Since the class requires the input x to build its |
151 | | -graph however, it is passed as a parameter of the ``__init__`` function. |
152 | | -This is usefull in the case when you would want to concatenate such |
153 | | -classes to form a deep network (case in which the input is not a new |
154 | | -variable but the output of the layer below). While in this example we |
155 | | -will not do that, the tutorials are designed such that the code is as |
156 | | -similar as possible among them, making it easy to go from one tutorial |
157 | | -to the other. |
| 134 | +We start by allocating symbolic variables for the training inputs :math:`x` and |
| 135 | +their corresponding classes :math:`y`. Note that ``x`` and ``y`` are defined |
| 136 | +outside the scope of the ``LogisticRegression`` object. Since the class |
| 137 | +requires the input to build its graph, it is passed as a parameter of the |
| 138 | +``__init__`` function. This is useful in case you want to connect instances of |
| 139 | +such classes to form a deep network. The output of one layer can be passed as |
| 140 | +the input of the layer above. (This tutorial does not build a multi-layer |
| 141 | +network, but this code will be reused in future tutorials that do.) |
158 | 142 |
|
159 | | -The last step involves defining a (symbolic) cost variable to minimize, using |
160 | | -the instance method ``classifier.negative_log_likelihood``. |
| 143 | +Finally, we define a (symbolic) ``cost`` variable to minimize, using the instance |
| 144 | +method ``classifier.negative_log_likelihood``. |
161 | 145 |
|
162 | 146 | .. literalinclude:: ../code/logistic_sgd.py |
163 | 147 | :start-after: classifier = LogisticRegression(input=x, n_in=28 * 28, n_out=10) |
164 | 148 | :end-before: # compiling a Theano function that computes the mistakes |
165 | 149 |
|
166 | | -Note how x is an implicit symbolic input to the symbolic definition of cost, |
167 | | -here, because classifier.__init__ has defined its symbolic variables in terms of x. |
| 150 | +Note that ``x`` is an implicit symbolic input to the definition of ``cost``, |
| 151 | +because the symbolic variables of ``classifier`` were defined in terms of ``x`` |
| 152 | +at initialization. |
168 | 153 |
|
169 | 154 | Learning the Model |
170 | 155 | ++++++++++++++++++ |
@@ -177,7 +162,7 @@ models, as expressions for :math:`\partial{\ell}/\partial{\theta}` can get |
177 | 162 | fairly complex, especially when taking into account problems of numerical |
178 | 163 | stability. |
179 | 164 |
|
180 | | -With Theano, this work is greatly simplified as it performs |
| 165 | +With Theano, this work is greatly simplified. It performs |
181 | 166 | automatic differentiation and applies certain math transforms to improve |
182 | 167 | numerical stability. |
183 | 168 |
|
@@ -188,33 +173,35 @@ To get the gradients :math:`\partial{\ell}/\partial{W}` and |
188 | 173 | :start-after: # compute the gradient of cost |
189 | 174 | :end-before: # start-snippet-3 |
190 | 175 |
|
191 | | -``g_W`` and ``g_b`` are again symbolic variables, which can be used as part of a |
192 | | -computation graph. Performing one-step of gradient descent can then be done as |
193 | | -follows: |
| 176 | +``g_W`` and ``g_b`` are symbolic variables, which can be used as part |
| 177 | +of a computation graph. The function ``train_model``, which performs one step |
| 178 | +of gradient descent, can then be defined as follows: |
194 | 179 |
|
195 | 180 | .. literalinclude:: ../code/logistic_sgd.py |
196 | 181 | :start-after: start-snippet-3 |
197 | 182 | :end-before: end-snippet-3 |
198 | 183 |
|
199 | | -The ``updates`` list contains, for each parameter, the |
200 | | -stochastic gradient update operation. The ``givens`` dictionary indicates with |
201 | | -what to replace certain variables of the graph. The function ``train_model`` is then |
202 | | -defined such that: |
| 184 | +``updates`` is a list of pairs. In each pair, the first element is the symbolic |
| 185 | +variable to be updated in the step, and the second element is the symbolic |
| 186 | +function for calculating its new value. Similarly, ``givens`` is a dictionary |
| 187 | +whose keys are symbolic variables and whose values specify |
| 188 | +their replacements during the step. The function ``train_model`` is then defined such |
| 189 | +that: |
203 | 190 |
|
204 | | -* the input is the mini-batch index ``index`` that together with the batch |
205 | | - size( which is not an input since it is fixed) defines :math:`x` with |
| 191 | +* the input is the mini-batch index ``index`` that, together with the batch |
| 192 | + size (which is not an input since it is fixed) defines :math:`x` with |
206 | 193 | corresponding labels :math:`y` |
207 | 194 | * the return value is the cost/loss associated with the x, y defined by |
208 | 195 | the ``index`` |
209 | | -* on every function call, it will first replace ``x`` and ``y`` with the |
210 | | - corresponding slices from the training set as defined by the |
211 | | - ``index`` and afterwards it will evaluate the cost |
| 196 | +* on every function call, it will first replace ``x`` and ``y`` with the slices |
| 197 | + from the training set specified by ``index``. Then, it will evaluate the cost |
212 | 198 | associated with that minibatch and apply the operations defined by the |
213 | 199 | ``updates`` list. |
214 | 200 |
|
215 | | -Each time ``train_model(index)`` function is called, it will thus compute and |
216 | | -return the appropriate cost, while also performing a step of MSGD. The entire |
217 | | -learning algorithm thus consists in looping over all examples in the dataset, |
| 201 | +Each time ``train_model(index)`` is called, it will thus compute and return the |
| 202 | +cost of a minibatch, while also performing a step of MSGD. The entire learning |
| 203 | +algorithm thus consists in looping over all examples in the dataset, considering |
| 204 | +all the examples in one minibatch at a time, |
218 | 205 | and repeatedly calling the ``train_model`` function. |
219 | 206 |
|
220 | 207 |
|
@@ -232,13 +219,14 @@ The code is as follows: |
232 | 219 | .. literalinclude:: ../code/logistic_sgd.py |
233 | 220 | :pyobject: LogisticRegression.errors |
234 | 221 |
|
235 | | -We then create a function ``test_model`` and a function ``validate_model``, which we can call to retrieve this |
236 | | -value. As you will see shortly, ``validate_model`` is key to our early-stopping |
237 | | -implementation (see :ref:`opt_early_stopping`). Both of these function |
238 | | -will get as input a batch offset and will compute the number of |
239 | | -missclassified examples for that mini-batch. The only difference between them |
240 | | -is that one draws its batches from the testing set, while |
241 | | -the other from the validation set. |
| 222 | +We then create a function ``test_model`` and a function ``validate_model``, |
| 223 | +which we can call to retrieve this value. As you will see shortly, |
| 224 | +``validate_model`` is key to our early-stopping implementation (see |
| 225 | +:ref:`opt_early_stopping`). These functions take a minibatch index and compute, |
| 226 | +for the examples in that minibatch, the number that were misclassified by the |
| 227 | +model. The only difference between them is that ``test_model`` draws its |
| 228 | +minibatches from the testing set, while ``validate_model`` draws its from the |
| 229 | +validation set. |
242 | 230 |
|
243 | 231 | .. literalinclude:: ../code/logistic_sgd.py |
244 | 232 | :start-after: cost = classifier.negative_log_likelihood(y) |
|