annotate deep/stacked_dae/v_youssouf/sgd_optimization.py @ 371:8cf52a1c8055

initial commit of sda with 36 classes
author youssouf
date Sun, 25 Apr 2010 12:31:22 -0400
parents
children
rev   line source
371
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
1 #!/usr/bin/python
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
2 # coding: utf-8
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
3
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
4 # Generic SdA optimization loop, adapted from the deeplearning.net tutorial
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
5
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
6 import numpy
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
7 import theano
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
8 import time
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
9 import datetime
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
10 import theano.tensor as T
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
11 import sys
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
12 import pickle
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
13
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
14 from jobman import DD
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
15 import jobman, jobman.sql
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
16 from copy import copy
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
17
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
18 from stacked_dae import SdA
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
19
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
20 from ift6266.utils.seriestables import *
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
21
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
22 #For test purpose only
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
23 buffersize=1000
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
24
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
25 default_series = { \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
26 'reconstruction_error' : DummySeries(),
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
27 'training_error' : DummySeries(),
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
28 'validation_error' : DummySeries(),
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
29 'test_error' : DummySeries(),
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
30 'params' : DummySeries()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
31 }
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
32
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
33 def itermax(iter, max):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
34 for i,it in enumerate(iter):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
35 if i >= max:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
36 break
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
37 yield it
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
38
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
39 class SdaSgdOptimizer:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
40 def __init__(self, dataset, hyperparameters, n_ins, n_outs,
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
41 examples_per_epoch, series=default_series, max_minibatches=None):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
42 self.dataset = dataset
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
43 self.hp = hyperparameters
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
44 self.n_ins = n_ins
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
45 self.n_outs = n_outs
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
46 self.parameters_pre=[]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
47
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
48 self.max_minibatches = max_minibatches
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
49 print "SdaSgdOptimizer, max_minibatches =", max_minibatches
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
50 print "Reduce Label: ", self.hp.reduce_label
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
51
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
52 self.ex_per_epoch = examples_per_epoch
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
53 self.mb_per_epoch = examples_per_epoch / self.hp.minibatch_size
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
54
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
55 self.series = series
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
56
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
57 self.rng = numpy.random.RandomState(1234)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
58
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
59 self.init_classifier()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
60
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
61 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
62
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
63 def init_classifier(self):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
64 print "Constructing classifier"
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
65
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
66 # we don't want to save arrays in DD objects, so
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
67 # we recreate those arrays here
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
68 nhl = self.hp.num_hidden_layers
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
69 layers_sizes = [self.hp.hidden_layers_sizes] * nhl
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
70 corruption_levels = [self.hp.corruption_levels] * nhl
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
71
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
72 # construct the stacked denoising autoencoder class
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
73 self.classifier = SdA( \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
74 batch_size = self.hp.minibatch_size, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
75 n_ins= self.n_ins, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
76 hidden_layers_sizes = layers_sizes, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
77 n_outs = self.n_outs, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
78 corruption_levels = corruption_levels,\
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
79 rng = self.rng,\
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
80 pretrain_lr = self.hp.pretraining_lr, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
81 finetune_lr = self.hp.finetuning_lr, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
82 detection_mode = self.hp.detection_mode, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
83 )
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
84
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
85 #theano.printing.pydotprint(self.classifier.pretrain_functions[0], "function.graph")
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
86
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
87 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
88
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
89 def train(self):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
90 self.pretrain(self.dataset)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
91 self.finetune(self.dataset)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
92
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
93 def pretrain(self,dataset):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
94 print "STARTING PRETRAINING, time = ", datetime.datetime.now()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
95 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
96
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
97 un_fichier=int(819200.0/self.hp.minibatch_size) #Number of batches in a P07 file
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
98
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
99 start_time = time.clock()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
100 ## Pre-train layer-wise
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
101 for i in xrange(self.classifier.n_layers):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
102 # go through pretraining epochs
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
103 for epoch in xrange(self.hp.pretraining_epochs_per_layer):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
104 # go through the training set
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
105 batch_index=0
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
106 count=0
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
107 num_files=0
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
108 for x,y in dataset.train(self.hp.minibatch_size):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
109 c = self.classifier.pretrain_functions[i](x)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
110 count +=1
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
111
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
112 self.series["reconstruction_error"].append((epoch, batch_index), c)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
113 batch_index+=1
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
114
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
115 #if batch_index % 100 == 0:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
116 # print "100 batches"
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
117
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
118 # useful when doing tests
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
119 if self.max_minibatches and batch_index >= self.max_minibatches:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
120 break
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
121
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
122 #When we pass through the data only once (the case with P07)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
123 #There is approximately 800*1024=819200 examples per file (1k per example and files are 800M)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
124 if self.hp.pretraining_epochs_per_layer == 1 and count%un_fichier == 0:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
125 print 'Pre-training layer %i, epoch %d, cost '%(i,num_files),c
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
126 num_files+=1
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
127 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
128 self.series['params'].append((num_files,), self.classifier.all_params)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
129
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
130 #When NIST is used
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
131 if self.hp.pretraining_epochs_per_layer > 1:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
132 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
133 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
134
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
135 self.series['params'].append((epoch,), self.classifier.all_params)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
136
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
137 end_time = time.clock()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
138
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
139 print ('Pretraining took %f minutes' %((end_time-start_time)/60.))
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
140 self.hp.update({'pretraining_time': end_time-start_time})
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
141
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
142 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
143
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
144 #To be able to load them later for tests on finetune
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
145 self.parameters_pre=[copy(x.value) for x in self.classifier.params]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
146 f = open('params_pretrain.txt', 'w')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
147 pickle.dump(self.parameters_pre,f)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
148 f.close()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
149
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
150
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
151 def finetune(self,dataset,dataset_test,num_finetune,ind_test,special=0,decrease=0):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
152
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
153 if special != 0 and special != 1:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
154 sys.exit('Bad value for variable special. Must be in {0,1}')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
155 print "STARTING FINETUNING, time = ", datetime.datetime.now()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
156
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
157 minibatch_size = self.hp.minibatch_size
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
158 if ind_test == 0 or ind_test == 20:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
159 nom_test = "NIST"
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
160 nom_train="P07"
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
161 else:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
162 nom_test = "P07"
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
163 nom_train = "NIST"
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
164
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
165
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
166 # create a function to compute the mistakes that are made by the model
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
167 # on the validation set, or testing set
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
168 test_model = \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
169 theano.function(
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
170 [self.classifier.x,self.classifier.y], self.classifier.errors)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
171 # givens = {
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
172 # self.classifier.x: ensemble_x,
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
173 # self.classifier.y: ensemble_y]})
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
174
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
175 validate_model = \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
176 theano.function(
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
177 [self.classifier.x,self.classifier.y], self.classifier.errors)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
178 # givens = {
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
179 # self.classifier.x: ,
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
180 # self.classifier.y: ]})
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
181
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
182
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
183 # early-stopping parameters
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
184 patience = 10000 # look as this many examples regardless
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
185 patience_increase = 2. # wait this much longer when a new best is
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
186 # found
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
187 improvement_threshold = 0.995 # a relative improvement of this much is
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
188 # considered significant
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
189 validation_frequency = min(self.mb_per_epoch, patience/2)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
190 # go through this many
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
191 # minibatche before checking the network
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
192 # on the validation set; in this case we
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
193 # check every epoch
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
194 if self.max_minibatches and validation_frequency > self.max_minibatches:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
195 validation_frequency = self.max_minibatches / 2
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
196
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
197 best_params = None
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
198 best_validation_loss = float('inf')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
199 test_score = 0.
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
200 start_time = time.clock()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
201
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
202 done_looping = False
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
203 epoch = 0
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
204
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
205 total_mb_index = 0
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
206 minibatch_index = 0
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
207 parameters_finetune=[]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
208
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
209 if ind_test == 21:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
210 learning_rate = self.hp.finetuning_lr / 10.0
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
211 else:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
212 learning_rate = self.hp.finetuning_lr #The initial finetune lr
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
213
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
214
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
215 while (epoch < num_finetune) and (not done_looping):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
216 epoch = epoch + 1
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
217
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
218 for x,y in dataset.train(minibatch_size,bufsize=buffersize):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
219 minibatch_index += 1
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
220
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
221 if self.hp.reduce_label:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
222 y[y > 35] = y[y > 35]-26
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
223
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
224 if special == 0:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
225 cost_ij = self.classifier.finetune(x,y,learning_rate)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
226 elif special == 1:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
227 cost_ij = self.classifier.finetune2(x,y)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
228 total_mb_index += 1
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
229
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
230 self.series["training_error"].append((epoch, minibatch_index), cost_ij)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
231
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
232 if (total_mb_index+1) % validation_frequency == 0:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
233 #minibatch_index += 1
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
234 #The validation set is always NIST (we want the model to be good on NIST)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
235 if ind_test == 0 | ind_test == 20:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
236 iter=dataset_test.valid(minibatch_size,bufsize=buffersize)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
237 else:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
238 iter = dataset.valid(minibatch_size,bufsize=buffersize)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
239 if self.max_minibatches:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
240 iter = itermax(iter, self.max_minibatches)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
241 validation_losses = [validate_model(x,y) for x,y in iter]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
242 this_validation_loss = numpy.mean(validation_losses)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
243
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
244 self.series["validation_error"].\
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
245 append((epoch, minibatch_index), this_validation_loss*100.)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
246
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
247 print('epoch %i, minibatch %i, validation error on NIST : %f %%' % \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
248 (epoch, minibatch_index+1, \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
249 this_validation_loss*100.))
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
250
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
251
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
252 # if we got the best validation score until now
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
253 if this_validation_loss < best_validation_loss:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
254
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
255 #improve patience if loss improvement is good enough
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
256 if this_validation_loss < best_validation_loss * \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
257 improvement_threshold :
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
258 patience = max(patience, total_mb_index * patience_increase)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
259
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
260 # save best validation score, iteration number and parameters
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
261 best_validation_loss = this_validation_loss
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
262 best_iter = total_mb_index
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
263 parameters_finetune=[copy(x.value) for x in self.classifier.params]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
264
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
265 # test it on the test set
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
266 iter = dataset.test(minibatch_size,bufsize=buffersize)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
267 if self.max_minibatches:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
268 iter = itermax(iter, self.max_minibatches)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
269 test_losses = [test_model(x,y) for x,y in iter]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
270 test_score = numpy.mean(test_losses)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
271
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
272 #test it on the second test set
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
273 iter2 = dataset_test.test(minibatch_size,bufsize=buffersize)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
274 if self.max_minibatches:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
275 iter2 = itermax(iter2, self.max_minibatches)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
276 test_losses2 = [test_model(x,y) for x,y in iter2]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
277 test_score2 = numpy.mean(test_losses2)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
278
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
279 self.series["test_error"].\
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
280 append((epoch, minibatch_index), test_score*100.)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
281
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
282 print((' epoch %i, minibatch %i, test error on dataset %s (train data) of best '
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
283 'model %f %%') %
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
284 (epoch, minibatch_index+1,nom_train,
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
285 test_score*100.))
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
286
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
287 print((' epoch %i, minibatch %i, test error on dataset %s of best '
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
288 'model %f %%') %
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
289 (epoch, minibatch_index+1,nom_test,
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
290 test_score2*100.))
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
291
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
292 if patience <= total_mb_index:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
293 done_looping = True
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
294 break #to exit the FOR loop
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
295
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
296 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
297
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
298 # useful when doing tests
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
299 if self.max_minibatches and minibatch_index >= self.max_minibatches:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
300 break
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
301
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
302 if decrease == 1:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
303 learning_rate /= 2 #divide the learning rate by 2 for each new epoch
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
304
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
305 self.series['params'].append((epoch,), self.classifier.all_params)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
306
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
307 if done_looping == True: #To exit completly the fine-tuning
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
308 break #to exit the WHILE loop
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
309
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
310 end_time = time.clock()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
311 self.hp.update({'finetuning_time':end_time-start_time,\
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
312 'best_validation_error':best_validation_loss,\
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
313 'test_score':test_score,
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
314 'num_finetuning_epochs':epoch})
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
315
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
316 print(('\nOptimization complete with best validation score of %f %%,'
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
317 'with test performance %f %% on dataset %s ') %
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
318 (best_validation_loss * 100., test_score*100.,nom_train))
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
319 print(('The test score on the %s dataset is %f')%(nom_test,test_score2*100.))
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
320
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
321 print ('The finetuning ran for %f minutes' % ((end_time-start_time)/60.))
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
322
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
323 sys.stdout.flush()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
324
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
325 #Save a copy of the parameters in a file to be able to get them in the future
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
326
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
327 if special == 1: #To keep a track of the value of the parameters
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
328 f = open('params_finetune_stanford.txt', 'w')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
329 pickle.dump(parameters_finetune,f)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
330 f.close()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
331
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
332 elif ind_test == 0 | ind_test == 20: #To keep a track of the value of the parameters
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
333 f = open('params_finetune_P07.txt', 'w')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
334 pickle.dump(parameters_finetune,f)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
335 f.close()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
336
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
337
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
338 elif ind_test== 1: #For the run with 2 finetunes. It will be faster.
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
339 f = open('params_finetune_NIST.txt', 'w')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
340 pickle.dump(parameters_finetune,f)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
341 f.close()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
342
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
343 elif ind_test== 21: #To keep a track of the value of the parameters
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
344 f = open('params_finetune_P07_then_NIST.txt', 'w')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
345 pickle.dump(parameters_finetune,f)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
346 f.close()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
347
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
348
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
349 #Set parameters like they where right after pre-train or finetune
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
350 def reload_parameters(self,which):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
351
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
352 #self.parameters_pre=pickle.load('params_pretrain.txt')
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
353 f = open(which)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
354 self.parameters_pre=pickle.load(f)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
355 f.close()
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
356 for idx,x in enumerate(self.parameters_pre):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
357 if x.dtype=='float64':
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
358 self.classifier.params[idx].value=theano._asarray(copy(x),dtype=theano.config.floatX)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
359 else:
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
360 self.classifier.params[idx].value=copy(x)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
361
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
362 def training_error(self,dataset):
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
363 # create a function to compute the mistakes that are made by the model
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
364 # on the validation set, or testing set
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
365 test_model = \
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
366 theano.function(
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
367 [self.classifier.x,self.classifier.y], self.classifier.errors)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
368
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
369 iter2 = dataset.train(self.hp.minibatch_size,bufsize=buffersize)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
370 train_losses2 = [test_model(x,y) for x,y in iter2]
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
371 train_score2 = numpy.mean(train_losses2)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
372 print "Training error is: " + str(train_score2)
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
373
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
374
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
375
8cf52a1c8055 initial commit of sda with 36 classes
youssouf
parents:
diff changeset
376