Mercurial > ift6266
annotate deep/convolutional_dae/sgd_opt.py @ 411:4f69d915d142
Better description of the model parameters.
author | Arnaud Bergeron <abergeron@gmail.com> |
---|---|
date | Thu, 29 Apr 2010 13:18:15 -0400 |
parents | 8babd43235dd |
children |
rev | line source |
---|---|
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
1 import time |
294
8babd43235dd
Save best valid score and test score in the db.
Arnaud Bergeron <abergeron@gmail.com>
parents:
288
diff
changeset
|
2 import sys, os |
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
3 |
288
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
4 from ift6266.utils.seriestables import * |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
5 |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
6 default_series = { |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
7 'train_error' : DummySeries(), |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
8 'valid_error' : DummySeries(), |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
9 'test_error' : DummySeries() |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
10 } |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
11 |
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
12 def sgd_opt(train, valid, test, training_epochs=10000, patience=10000, |
288
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
13 patience_increase=2., improvement_threshold=0.995, net=None, |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
14 validation_frequency=None, series=default_series): |
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
15 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
16 if validation_frequency is None: |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
17 validation_frequency = patience/2 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
18 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
19 start_time = time.clock() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
20 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
21 best_params = None |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
22 best_validation_loss = float('inf') |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
23 test_score = 0. |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
24 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
25 start_time = time.clock() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
26 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
27 for epoch in xrange(1, training_epochs+1): |
288
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
28 series['train_error'].append((epoch,), train()) |
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
29 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
30 if epoch % validation_frequency == 0: |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
31 this_validation_loss = valid() |
288
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
32 series['valid_error'].append((epoch,), this_validation_loss*100.) |
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
33 print('epoch %i, validation error %f %%' % \ |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
34 (epoch, this_validation_loss*100.)) |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
35 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
36 # if we got the best validation score until now |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
37 if this_validation_loss < best_validation_loss: |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
38 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
39 #improve patience if loss improvement is good enough |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
40 if this_validation_loss < best_validation_loss * \ |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
41 improvement_threshold : |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
42 patience = max(patience, epoch * patience_increase) |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
43 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
44 # save best validation score and epoch number |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
45 best_validation_loss = this_validation_loss |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
46 best_epoch = epoch |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
47 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
48 # test it on the test set |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
49 test_score = test() |
288
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
50 series['test_error'].append((epoch,), test_score*100.) |
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
51 print((' epoch %i, test error of best model %f %%') % |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
52 (epoch, test_score*100.)) |
288
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
53 if net is not None: |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
54 net.save('best.net.new') |
80ee63c3e749
Add net saving (only the best model) and error saving using SeriesTable
Arnaud Bergeron <abergeron@gmail.com>
parents:
276
diff
changeset
|
55 os.rename('best.net.new', 'best.net') |
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
56 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
57 if patience <= epoch: |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
58 break |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
59 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
60 end_time = time.clock() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
61 print(('Optimization complete with best validation score of %f %%,' |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
62 'with test performance %f %%') % |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
63 (best_validation_loss * 100., test_score*100.)) |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
64 print ('The code ran for %f minutes' % ((end_time-start_time)/60.)) |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
65 |
294
8babd43235dd
Save best valid score and test score in the db.
Arnaud Bergeron <abergeron@gmail.com>
parents:
288
diff
changeset
|
66 return best_validation_loss, test_score |