GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Edits for clarification. · iamsile/DeepLearningTutorials@0150aad

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc
    • logreg.txt
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/logreg.txt‎

Copy file name to clipboardExpand all lines: doc/logreg.txt
+56-68Lines changed: 56 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -39,55 +39,40 @@ The Model
3939

4040
Logistic regression is a probabilistic, linear classifier. It is parametrized
4141
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.
4445

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:
4648

4749
.. math::
4850
P(Y=i|x, W,b) &= softmax_i(W x + b) \\
4951
&= \frac {e^{W_i x + b_i}} {\sum_j e^{W_j x + b_j}}
5052

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:
5254

5355
.. math::
5456
y_{pred} = {\rm argmax}_i P(Y=i|x,W,b)
5557

5658
The code to do this in Theano is the following:
5759

58-
.. literalinclude:: ../code/logistic_sgd.py
59-
:start-after: index = T.lscalar()
60-
:end-before: # construct the logistic regression class
61-
62-
6360
.. literalinclude:: ../code/logistic_sgd.py
6461
:start-after: start-snippet-1
6562
:end-before: end-snippet-1
6663

67-
We first start by allocating symbolic variables for the inputs :math:`x,y`.
6864
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
9176
section will thus cover how to learn the optimal parameters.
9277

9378

@@ -146,25 +131,25 @@ We instantiate this class as follows:
146131
:start-after: index = T.lscalar()
147132
:end-before: # the cost we minimize during
148133

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.)
158142

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``.
161145

162146
.. literalinclude:: ../code/logistic_sgd.py
163147
:start-after: classifier = LogisticRegression(input=x, n_in=28 * 28, n_out=10)
164148
:end-before: # compiling a Theano function that computes the mistakes
165149

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.
168153

169154
Learning the Model
170155
++++++++++++++++++
@@ -177,7 +162,7 @@ models, as expressions for :math:`\partial{\ell}/\partial{\theta}` can get
177162
fairly complex, especially when taking into account problems of numerical
178163
stability.
179164

180-
With Theano, this work is greatly simplified as it performs
165+
With Theano, this work is greatly simplified. It performs
181166
automatic differentiation and applies certain math transforms to improve
182167
numerical stability.
183168

@@ -188,33 +173,35 @@ To get the gradients :math:`\partial{\ell}/\partial{W}` and
188173
:start-after: # compute the gradient of cost
189174
:end-before: # start-snippet-3
190175

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:
194179

195180
.. literalinclude:: ../code/logistic_sgd.py
196181
:start-after: start-snippet-3
197182
:end-before: end-snippet-3
198183

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:
203190

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
206193
corresponding labels :math:`y`
207194
* the return value is the cost/loss associated with the x, y defined by
208195
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
212198
associated with that minibatch and apply the operations defined by the
213199
``updates`` list.
214200

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,
218205
and repeatedly calling the ``train_model`` function.
219206

220207

@@ -232,13 +219,14 @@ The code is as follows:
232219
.. literalinclude:: ../code/logistic_sgd.py
233220
:pyobject: LogisticRegression.errors
234221

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.
242230

243231
.. literalinclude:: ../code/logistic_sgd.py
244232
:start-after: cost = classifier.negative_log_likelihood(y)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.