Skip to content

Commit e462881

Browse files
committed
updated implementation details for logreg
1 parent e885614 commit e462881

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

doc/logreg.txt

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -284,39 +284,43 @@ follows:
284284

285285
.. code-block:: python
286286

287-
# set a learning rate
288-
learning_rate=0.01
287+
# compute the gradient of cost with respect to theta = (W,b)
288+
g_W = T.grad(cost = cost, wrt = classifier.W)
289+
g_b = T.grad(cost = cost, wrt = classifier.b)
289290

290291
# specify how to update the parameters of the model as a dictionary
291292
updates ={classifier.W: classifier.W - learning_rate*g_W,\
292293
classifier.b: classifier.b - learning_rate*g_b}
293294

294-
# compiling a Theano function `train_model` that returns the cost, but in
295-
# 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
296297
# defined in `updates`
297-
train_model = theano.function( inputs = [minibatch_offset],
298+
train_model = theano.function(inputs = [index],
298299
outputs = cost,
299300
updates = updates,
300301
givens={
301-
x:train_set_x[minibatch_offset:minibatch_offset+batch_size],
302-
y:train_set_y[minibatch_offset:minibatch_offset+batch_size]})
302+
x:train_set_x[index*batch_size:(index+1)*batch_size],
303+
y:train_set_y[index*batch_size:(index+1)*batch_size]})
304+
303305

304306

305307
The ``updates`` dictionary contains, for each parameter, the
306308
stochastic gradient update operation. The ``givens`` dictionary indicates with
307309
what to replace certain variables of the graph. The function ``train_model`` is then
308310
defined such that:
309311

310-
* the input is the mini-batch offset ``minibatch_offset`` that together with the batch size( which is not an input since it is fixed) defines :math:`x` with corresponding labels :math:`y`
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+
corresponding labels :math:`y`
311315
* the return value is the cost/loss associated with the x, y defined by
312-
the ``minibatch_offset``
316+
the ``index``
313317
* on every function call, it will first replace ``x`` and ``y`` with the
314-
* corresponding slices from the training set as defined by the
315-
* ``minibatch_offset`` and afterwards it will evaluate the cost
316-
* associated with that minibatch and apply the operations defined by the
318+
corresponding slices from the training set as defined by the
319+
``index`` and afterwards it will evaluate the cost
320+
associated with that minibatch and apply the operations defined by the
317321
``updates`` dictionary.
318322

319-
Each time ``train_model(minibatch_offset)`` function is called, it will thus compute and
323+
Each time ``train_model(index)`` function is called, it will thus compute and
320324
return the appropriate cost, while also performing a step of MSGD. The entire
321325
learning algorithm thus consists in looping over all examples in the dataset,
322326
and repeatedly calling the ``train_model`` function.
@@ -357,17 +361,19 @@ the other from the validation set.
357361

358362
.. code-block:: python
359363

360-
test_model = theano.function( inputs = [minibatch_offset],
364+
# compiling a Theano function that computes the mistakes that are made by
365+
# the model on a minibatch
366+
test_model = theano.function(inputs = [index],
361367
outputs = classifier.errors(y),
362368
givens={
363-
x:test_set_x[minibatch_offset:minibatch_offset+batch_size],
364-
y:test_set_y[minibatch_offset:minibatch_offset+batch_size]})
369+
x:test_set_x[index*batch_size:(index+1)*batch_size],
370+
y:test_set_y[index*batch_size:(index+1)*batch_size]})
365371

366-
validate_model =theano.function( inputs = [minibatch_offset],
372+
validate_model = theano.function( inputs = [index],
367373
outputs = classifier.errors(y),
368374
givens={
369-
x:valid_set_x[minibatch_offset:minibatch_offset+batch_size],
370-
y:valid_set_y[minibatch_offset:minibatch_offset+batch_size]})
375+
x:valid_set_x[index*batch_size:(index+1)*batch_size],
376+
y:valid_set_y[index*batch_size:(index+1)*batch_size]})
371377

372378

373379

0 commit comments

Comments
 (0)