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