Mercurial > ift6266
annotate deep/convolutional_dae/sgd_opt.py @ 286:1cc535f3e254
correction d'un bug pour affichage des resultats de pre-train avec P07
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Thu, 25 Mar 2010 12:20:27 -0400 |
parents | 727ed56fad12 |
children | 80ee63c3e749 |
rev | line source |
---|---|
276
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
1 import time |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
2 import sys |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
3 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
4 def sgd_opt(train, valid, test, training_epochs=10000, patience=10000, |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
5 patience_increase=2., improvement_threshold=0.995, |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
6 validation_frequency=None): |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
7 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
8 if validation_frequency is None: |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
9 validation_frequency = patience/2 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
10 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
11 start_time = time.clock() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
12 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
13 best_params = None |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
14 best_validation_loss = float('inf') |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
15 test_score = 0. |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
16 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
17 start_time = time.clock() |
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 for epoch in xrange(1, training_epochs+1): |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
20 train() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
21 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
22 if epoch % validation_frequency == 0: |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
23 this_validation_loss = valid() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
24 print('epoch %i, validation error %f %%' % \ |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
25 (epoch, this_validation_loss*100.)) |
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 # 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
|
28 if this_validation_loss < best_validation_loss: |
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 #improve patience if loss improvement is good enough |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
31 if this_validation_loss < best_validation_loss * \ |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
32 improvement_threshold : |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
33 patience = max(patience, epoch * patience_increase) |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
34 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
35 # save best validation score and epoch number |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
36 best_validation_loss = this_validation_loss |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
37 best_epoch = epoch |
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 # test it on the test set |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
40 test_score = test() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
41 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
|
42 (epoch, test_score*100.)) |
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 if patience <= epoch: |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
45 break |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
46 |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
47 end_time = time.clock() |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
48 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
|
49 'with test performance %f %%') % |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
50 (best_validation_loss * 100., test_score*100.)) |
727ed56fad12
Add reworked code for convolutional auto-encoder.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
51 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
|
52 |