comparison baseline/mlp/mlp_nist.py @ 404:1509b9bba4cc

added digit/char error
author xaviermuller
date Wed, 28 Apr 2010 11:45:14 -0400
parents 60a4432b8071
children 195f95c3d461
comparison
equal deleted inserted replaced
403:a11692910312 404:1509b9bba4cc
167 def mlp_get_nist_error(model_name='/u/mullerx/ift6266h10_sandbox_db/xvm_final_lr1_p073/8/best_model.npy.npz', 167 def mlp_get_nist_error(model_name='/u/mullerx/ift6266h10_sandbox_db/xvm_final_lr1_p073/8/best_model.npy.npz',
168 data_set=0): 168 data_set=0):
169 169
170 170
171 171
172 # allocate symbolic variables for the data 172
173 x = T.fmatrix() # the data is presented as rasterized images 173
174 y = T.lvector() # the labels are presented as 1D vector of 174
175 # [long int] labels
176 175
177 # load the data set and create an mlp based on the dimensions of the model 176 # load the data set and create an mlp based on the dimensions of the model
178 model=numpy.load(model_name) 177 model=numpy.load(model_name)
179 W1=model['W1'] 178 W1=model['W1']
180 W2=model['W2'] 179 W2=model['W2']
181 b1=model['b1'] 180 b1=model['b1']
182 b2=model['b2'] 181 b2=model['b2']
183 nb_hidden=b1.shape[0] 182
184 input_dim=W1.shape[0] 183 total_error_count=0.0
185 nb_targets=b2.shape[0] 184 total_exemple_count=0.0
186 learning_rate=0.1 185
187 186 nb_error_count=0.0
187 nb_exemple_count=0.0
188
189 char_error_count=0.0
190 char_exemple_count=0.0
191
192 min_error_count=0.0
193 min_exemple_count=0.0
194
195 maj_error_count=0.0
196 maj_exemple_count=0.0
197
198
188 199
189 if data_set==0: 200 if data_set==0:
190 dataset=datasets.nist_all() 201 dataset=datasets.nist_all()
191 elif data_set==1: 202 elif data_set==1:
192 dataset=datasets.nist_P07() 203 dataset=datasets.nist_P07()
193
194
195 classifier = MLP( input=x,\
196 n_in=input_dim,\
197 n_hidden=nb_hidden,\
198 n_out=nb_targets,
199 learning_rate=learning_rate)
200
201
202 #overwrite weights with weigths from model
203 classifier.W1.value=W1
204 classifier.W2.value=W2
205 classifier.b1.value=b1
206 classifier.b2.value=b2
207
208
209 cost = classifier.negative_log_likelihood(y) \
210 + 0.0 * classifier.L1 \
211 + 0.0 * classifier.L2_sqr
212
213 # compiling a theano function that computes the mistakes that are made by
214 # the model on a minibatch
215 test_model = theano.function([x,y], classifier.errors(y))
216 204
217 205
218 206
219 #get the test error 207 #get the test error
220 #use a batch size of 1 so we can get the sub-class error 208 #use a batch size of 1 so we can get the sub-class error
221 #without messing with matrices (will be upgraded later) 209 #without messing with matrices (will be upgraded later)
222 test_score=0 210 test_score=0
223 temp=0 211 temp=0
224 for xt,yt in dataset.test(20): 212 for xt,yt in dataset.test(1):
225 test_score += test_model(xt,yt) 213
226 temp = temp+1 214 total_exemple_count = total_exemple_count +1
227 test_score /= temp 215 #get activation for layer 1
228 216 a0=numpy.dot(numpy.transpose(W1),numpy.transpose(xt[0])) + b1
229 217 #add non linear function to layer 1 activation
230 return test_score*100 218 a0_out=numpy.tanh(a0)
219
220 #get activation for output layer
221 a1= numpy.dot(numpy.transpose(W2),a0_out) + b2
222 #add non linear function for output activation (softmax)
223 a1_exp = numpy.exp(a1)
224 sum_a1=numpy.sum(a1_exp)
225 a1_out=a1_exp/sum_a1
226
227 predicted_class=numpy.argmax(a1_out)
228 wanted_class=yt[0]
229 if(predicted_class!=wanted_class):
230 total_error_count = total_error_count +1
231
232 #treat digit error
233 if(wanted_class<10):
234 nb_exemple_count=nb_exemple_count + 1
235 predicted_class=numpy.argmax(a1_out[0:10])
236 if(predicted_class!=wanted_class):
237 nb_error_count = nb_error_count +1
238
239 if(wanted_class>9):
240 char_exemple_count=char_exemple_count + 1
241 predicted_class=numpy.argmax(a1_out[10:62])+10
242 if((predicted_class!=wanted_class) and ((predicted_class+26)!=wanted_class) and ((predicted_class-26)!=wanted_class)):
243 char_error_count = char_error_count +1
244
245
246
247 print (('total error = %f') % ((total_error_count/total_exemple_count)*100.0))
248 print (('number error = %f') % ((nb_error_count/nb_exemple_count)*100.0))
249 print (('char error = %f') % ((char_error_count/char_exemple_count)*100.0))
250 return (total_error_count/total_exemple_count)*100.0
231 251
232 252
233 253
234 254
235 255