@@ -135,14 +135,14 @@ class dA(object): |
135 | 135 |
|
136 | 136 | """ |
137 | 137 |
|
138 | | - def __init__(self, n_visible= 784, n_hidden= 500, input= None): |
| 138 | + def __init__(self, n_visible= 784, n_hidden= 500, input= None, corruption_level = 0.1): |
139 | 139 | """ |
140 | 140 | Initialize the dA class by specifying the number of visible units (the |
141 | 141 | dimension d of the input ), the number of hidden units ( the dimension |
142 | 142 | d' of the latent or hidden space ) and by giving a symbolic variable |
143 | 143 | for the input. Such a symbolic variable is useful when the input is |
144 | 144 | the result of some computations. For example when dealing with SdAs, |
145 | | - the dA on layer 2 gets as input the output of the DAE on layer 1. |
| 145 | + the dA on layer 2 gets as input the output of the dA on layer 1. |
146 | 146 | This output can be written as a function of the input to the entire |
147 | 147 | model, and as such can be computed by theano whenever needed. |
148 | 148 | |
@@ -152,6 +152,13 @@ def __init__(self, n_visible= 784, n_hidden= 500, input= None): |
152 | 152 |
|
153 | 153 | :param input: a symbolic description of the input or None |
154 | 154 |
|
| 155 | + :param corruption_level: the corruption mechanism picks up randomly this fraction |
| 156 | + of entries of the input and turns them to 0 |
| 157 | + |
| 158 | + |
| 159 | + amount of entries from the input to 0 from the input, defaul |
| 160 | + is 0.1, which means 10% of entries are corrupted to 0 |
| 161 | +
|
155 | 162 | """ |
156 | 163 | self.n_visible = n_visible |
157 | 164 | self.n_hidden = n_hidden |
@@ -198,8 +205,8 @@ def __init__(self, n_visible= 784, n_hidden= 500, input= None): |
198 | 205 | # third argument is the probability of success of any trial |
199 | 206 | # |
200 | 207 | # this will produce an array of 0s and 1s where 1 has a |
201 | | - # probability of 0.9 and 0 if 0.1 |
202 | | - self.tilde_x = theano_rng.binomial( self.x.shape, 1, 0.9) * self.x |
| 208 | + # probability of 1 - corruption_level and 0 if corruption_level |
| 209 | + self.tilde_x = theano_rng.binomial( self.x.shape, 1, 1 - corruption_level) * self.x |
203 | 210 | # Equation (2) |
204 | 211 | # note : y is stored as an attribute of the class so that it can be |
205 | 212 | # used later when stacking dAs. |
@@ -358,7 +365,9 @@ def shared_dataset(data_xy): |
358 | 365 | # construct the logistic regression class |
359 | 366 | classifier = SdA( input=x, n_ins=28*28, \ |
360 | 367 | hidden_layers_sizes = [1000, 1000, 1000], n_outs=10) |
361 | | - |
| 368 | + |
| 369 | + |
| 370 | + start_time = time.clock() |
362 | 371 | ## Pre-train layer-wise |
363 | 372 | for i in xrange(classifier.n_layers): |
364 | 373 | cost = classifier.layers[i].cost |
@@ -387,7 +396,9 @@ def shared_dataset(data_xy): |
387 | 396 |
|
388 | 397 |
|
389 | 398 |
|
| 399 | + end_time = time.clock() |
390 | 400 |
|
| 401 | + print ('Pretraining took %f minutes' %((end_time-start_time)/60.)) |
391 | 402 | # Fine-tune the entire model |
392 | 403 | # the cost we minimize during training is the negative log likelihood of |
393 | 404 | # the model |
@@ -435,9 +446,9 @@ def shared_dataset(data_xy): |
435 | 446 |
|
436 | 447 | # early-stopping parameters |
437 | 448 | patience = 10000 # look as this many examples regardless |
438 | | - patience_increase = 2 # wait this much longer when a new best is |
| 449 | + patience_increase = 2. # wait this much longer when a new best is |
439 | 450 | # found |
440 | | - improvement_threshold = 0.995 # a relative improvement of this much is |
| 451 | + improvement_threshold = 0.99 # a relative improvement of this much is |
441 | 452 | # considered significant |
442 | 453 | validation_frequency = min(n_train_batches, patience/2) |
443 | 454 | # go through this many |
@@ -450,15 +461,19 @@ def shared_dataset(data_xy): |
450 | 461 | best_validation_loss = float('inf') |
451 | 462 | test_score = 0. |
452 | 463 | start_time = time.clock() |
453 | | - cost_ij = [] |
454 | | - for epoch in xrange(training_epochs): |
| 464 | + |
| 465 | + done_looping = False |
| 466 | + epoch = 0 |
| 467 | + |
| 468 | + while (epoch < training_epochs) and (not done_looping): |
| 469 | + epoch = epoch + 1 |
455 | 470 | for minibatch_index in xrange(n_train_batches): |
456 | 471 |
|
457 | | - cost_ij += [train_model(minibatch_index)] |
| 472 | + cost_ij = train_model(minibatch_index) |
458 | 473 | iter = epoch * n_train_batches + minibatch_index |
459 | 474 |
|
460 | 475 | if (iter+1) % validation_frequency == 0: |
461 | | - cost_ij = [] |
| 476 | + |
462 | 477 | validation_losses = [validate_model(i) for i in xrange(n_valid_batches)] |
463 | 478 | this_validation_loss = numpy.mean(validation_losses) |
464 | 479 | print('epoch %i, minibatch %i/%i, validation error %f %%' % \ |
@@ -488,6 +503,7 @@ def shared_dataset(data_xy): |
488 | 503 |
|
489 | 504 |
|
490 | 505 | if patience <= iter : |
| 506 | + done_looping = True |
491 | 507 | break |
492 | 508 |
|
493 | 509 | end_time = time.clock() |
|