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 — end of line white space · jekky/DeepLearningTutorials@6172cdf

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
+37-37Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Classifying MNIST digits using Logistic Regression
2424

2525
.. _floatX: http://deeplearning.net/software/theano/library/config.html#config.floatX
2626

27-
.. _GPU: http://deeplearning.net/software/theano/tutorial/using_gpu.html
27+
.. _GPU: http://deeplearning.net/software/theano/tutorial/using_gpu.html
2828

2929
In this section, we show how Theano can be used to implement the most basic
3030
classifier: the logistic regression. We start off with a quick primer of the
@@ -40,7 +40,7 @@ The Model
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
4242
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.
4444

4545
Mathematically, this can be written as:
4646

@@ -68,21 +68,21 @@ The code to do this in Theano is the following:
6868
b = theano.shared(numpy.zeros((10,)), name = 'b')
6969
W = theano.shared(numpy.zeros((784,10)), name = 'W')
7070

71-
# symbolic expression for computing the vector of
72-
# class-membership probabilities
71+
# symbolic expression for computing the vector of
72+
# class-membership probabilities
7373
p_y_given_x = T.nnet.softmax(T.dot(x,W)+b)
7474

7575
# compiled Theano function that returns the vector of class-membership
7676
# probabilities
7777
get_p_y_given_x = theano.function( inputs = [x], outputs = p_y_given_x)
7878

79-
# print the probability of some example represented by x_value
79+
# print the probability of some example represented by x_value
8080
# x_value is not a symbolic variable but a numpy array describing the
8181
# datapoint
8282
print 'Probability that x is of class %i is %f' % (i, get_p_y_given_x(x_value)[i])
8383

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
8686
y_pred = T.argmax(p_y_given_x)
8787

8888
# compiled theano function that returns this value
@@ -95,7 +95,7 @@ training, we allocate shared variables for :math:`W,b`.
9595
This declares them both as being symbolic Theano variables, but also
9696
initializes their contents. The dot and softmax operators are then used to compute the vector
9797
: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.
9999

100100
Up to this point, we have only defined the graph of computations which Theano
101101
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
132132

133133
.. math::
134134

135-
\mathcal{L} (\theta=\{W,b\}, \mathcal{D}) =
135+
\mathcal{L} (\theta=\{W,b\}, \mathcal{D}) =
136136
\sum_{i=0}^{|\mathcal{D}|} \log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\
137137
\ell (\theta=\{W,b\}, \mathcal{D}) = - \mathcal{L} (\theta=\{W,b\}, \mathcal{D})
138138

@@ -175,29 +175,29 @@ similar to what we have covered so far, and should be self explanatory.
175175
""" Initialize the parameters of the logistic regression
176176

177177
: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
179179
architecture (e.g., one minibatch of input images)
180180

181181
: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
183183
which the datapoint lies
184184

185185
: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
187187
which the target lies
188-
"""
188+
"""
189189

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)
191191
self.W = theano.shared( value=numpy.zeros((n_in,n_out),
192192
dtype = theano.config.floatX), name ='W' )
193193
# 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,),
195195
dtype = theano.config.floatX), name = 'b' )
196196

197197
# compute vector of class-membership probabilities in symbolic form
198198
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)+self.b)
199199

200-
# compute prediction as class whose probability is maximal in
200+
# compute prediction as class whose probability is maximal in
201201
# symbolic form
202202
self.y_pred=T.argmax(self.p_y_given_x, axis=1)
203203

@@ -208,9 +208,9 @@ similar to what we have covered so far, and should be self explanatory.
208208

209209
.. math::
210210

211-
\frac{1}{|\mathcal{D}|} \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) =
211+
\frac{1}{|\mathcal{D}|} \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) =
212212
\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})
214214

215215

216216
: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}`,
263263
and :math:`\partial{\ell}/\partial{b}`, This can get pretty tricky for complex
264264
models, as expressions for :math:`\partial{\ell}/\partial{\theta}` can get
265265
fairly complex, especially when taking into account problems of numerical
266-
stability.
266+
stability.
267267

268268
With Theano, this work is greatly simplified as it performs
269269
automatic differentiation and applies certain math transforms to improve
@@ -284,19 +284,19 @@ follows:
284284

285285
.. code-block:: python
286286

287-
# compute the gradient of cost with respect to theta = (W,b)
287+
# compute the gradient of cost with respect to theta = (W,b)
288288
g_W = T.grad(cost = cost, wrt = classifier.W)
289289
g_b = T.grad(cost = cost, wrt = classifier.b)
290290

291291
# specify how to update the parameters of the model as a dictionary
292292
updates ={classifier.W: classifier.W - learning_rate*g_W,\
293293
classifier.b: classifier.b - learning_rate*g_b}
294294

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
297297
# defined in `updates`
298-
train_model = theano.function(inputs = [index],
299-
outputs = cost,
298+
train_model = theano.function(inputs = [index],
299+
outputs = cost,
300300
updates = updates,
301301
givens={
302302
x:train_set_x[index*batch_size:(index+1)*batch_size],
@@ -305,20 +305,20 @@ follows:
305305

306306

307307
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
309309
what to replace certain variables of the graph. The function ``train_model`` is then
310310
defined such that:
311311

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
314314
corresponding labels :math:`y`
315315
* the return value is the cost/loss associated with the x, y defined by
316-
the ``index``
316+
the ``index``
317317
* on every function call, it will first replace ``x`` and ``y`` with the
318318
corresponding slices from the training set as defined by the
319319
``index`` and afterwards it will evaluate the cost
320320
associated with that minibatch and apply the operations defined by the
321-
``updates`` dictionary.
321+
``updates`` dictionary.
322322

323323
Each time ``train_model(index)`` function is called, it will thus compute and
324324
return the appropriate cost, while also performing a step of MSGD. The entire
@@ -344,9 +344,9 @@ The code is as follows:
344344
...
345345

346346
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
348348
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
350350
"""
351351
return T.mean(T.neq(self.y_pred, y))
352352

@@ -355,21 +355,21 @@ We then create a function ``test_model`` and a function ``validate_model``, whic
355355
value. As you will see shortly, ``validate_model`` is key to our early-stopping
356356
implementation (see :ref:`opt_early_stopping`). Both of these function
357357
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
359359
is that one draws its batches from the testing set, while
360360
the other from the validation set.
361361

362362
.. code-block:: python
363363

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
365365
# the model on a minibatch
366-
test_model = theano.function(inputs = [index],
366+
test_model = theano.function(inputs = [index],
367367
outputs = classifier.errors(y),
368368
givens={
369369
x:test_set_x[index*batch_size:(index+1)*batch_size],
370370
y:test_set_y[index*batch_size:(index+1)*batch_size]})
371371

372-
validate_model = theano.function( inputs = [index],
372+
validate_model = theano.function( inputs = [index],
373373
outputs = classifier.errors(y),
374374
givens={
375375
x:valid_set_x[index*batch_size:(index+1)*batch_size],
@@ -407,14 +407,14 @@ The output one should expect is of the form :
407407
On an Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00 Ghz the code runs with
408408
approximately 1.936 epochs/sec and it took 75 epochs to reach a test
409409
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.
411411

412412
.. rubric:: Footnotes
413413

414414
.. [#f1] For smaller datasets and simpler models, more sophisticated descent
415415
algorithms can be more effective. The sample code
416416
`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
418418
on the logistic regression task.
419419

420420

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.