diff baseline/mlp/mlp_nist.py @ 445:868f82777839

added jobman all test + val error and sigmoid output
author xaviermuller
date Tue, 04 May 2010 11:17:27 -0400
parents 3dba84c0fbc1
children
line wrap: on
line diff
--- a/baseline/mlp/mlp_nist.py	Mon May 03 18:43:24 2010 -0400
+++ b/baseline/mlp/mlp_nist.py	Tue May 04 11:17:27 2010 -0400
@@ -49,7 +49,7 @@
 
 
 
-    def __init__(self, input, n_in, n_hidden, n_out,learning_rate):
+    def __init__(self, input, n_in, n_hidden, n_out,learning_rate,detection_mode):
         """Initialize the parameters for the multilayer perceptron
 
         :param input: symbolic variable that describes the input of the 
@@ -108,12 +108,21 @@
         
 
         # symbolic expression computing the values of the top layer 
-        self.p_y_given_x= T.nnet.softmax(T.dot(self.hidden, self.W2)+self.b2)
+        if(detection_mode==0):
+            self.p_y_given_x= T.nnet.softmax(T.dot(self.hidden, self.W2)+self.b2)
+        else:
+            self.p_y_given_x= T.nnet.sigmoid(T.dot(self.hidden, self.W2)+self.b2)
+            
+        
+        
+       # self.y_out_sig= T.sigmoid(T.dot(self.hidden, self.W2)+self.b2)
 
         # compute prediction as class whose probability is maximal in 
         # symbolic form
         self.y_pred = T.argmax( self.p_y_given_x, axis =1)
-        self.y_pred_num = T.argmax( self.p_y_given_x[0:9], axis =1)
+        
+       # self.y_pred_sig = T.argmax( self.y_out_sig, axis =1)
+        
         
         
         
@@ -143,6 +152,12 @@
         :correct label
         """
         return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y])
+    
+    
+    def cross_entropy(self, y):
+        return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y]+T.sum(T.log(1-self.p_y_given_x), axis=1)-T.log(1-self.p_y_given_x)[T.arange(y.shape[0]),y])
+    
+    
 
 
 
@@ -210,12 +225,22 @@
     vmaj_error_count=0.0
     vmaj_exemple_count=0.0
     
+    nbc_error_count=0.0
+    vnbc_error_count=0.0
+    
     
 
     if data_set==0:
-        dataset=datasets.nist_all()
+        print 'using nist'
+    	dataset=datasets.nist_all()
     elif data_set==1:
+        print 'using p07'
         dataset=datasets.nist_P07()
+    elif data_set==2:
+        print 'using pnist'
+        dataset=datasets.PNIST07()
+        
+   
 
 
 
@@ -243,7 +268,12 @@
         wanted_class=yt[0]
         if(predicted_class!=wanted_class):
             total_error_count = total_error_count +1
-            
+        
+        
+        if(not(predicted_class==wanted_class or ( (((predicted_class+26)==wanted_class) or ((predicted_class-26)==wanted_class)) and wanted_class>9)   )):
+            nbc_error_count = nbc_error_count +1
+               
+               
         #treat digit error
         if(wanted_class<10):
             nb_exemple_count=nb_exemple_count + 1
@@ -294,6 +324,9 @@
         if(predicted_class!=wanted_class):
             vtotal_error_count = vtotal_error_count +1
             
+        if(not(predicted_class==wanted_class or ( (((predicted_class+26)==wanted_class) or ((predicted_class-26)==wanted_class)) and wanted_class>9)   )):
+            vnbc_error_count = nbc_error_count +1
+            
         #treat digit error
         if(wanted_class<10):
             vnb_exemple_count=vnb_exemple_count + 1
@@ -326,20 +359,63 @@
     print (('char error = %f') % ((char_error_count/char_exemple_count)*100.0))
     print (('min error = %f') % ((min_error_count/min_exemple_count)*100.0))
     print (('maj error = %f') % ((maj_error_count/maj_exemple_count)*100.0))
+    print (('36 error = %f') % ((nbc_error_count/total_exemple_count)*100.0))
     
     print (('valid total error = %f') % ((vtotal_error_count/vtotal_exemple_count)*100.0))
     print (('valid number error = %f') % ((vnb_error_count/vnb_exemple_count)*100.0))
     print (('valid char error = %f') % ((vchar_error_count/vchar_exemple_count)*100.0))
     print (('valid min error = %f') % ((vmin_error_count/vmin_exemple_count)*100.0))
     print (('valid maj error = %f') % ((vmaj_error_count/vmaj_exemple_count)*100.0))
+    print (('valid 36 error = %f') % ((vnbc_error_count/vtotal_exemple_count)*100.0))
     
-    print ((' num total = %d,%d') % (total_exemple_count,total_error_count))
-    print ((' num nb = %d,%d') % (nb_exemple_count,nb_error_count))
-    print ((' num min = %d,%d') % (min_exemple_count,min_error_count))
-    print ((' num maj = %d,%d') % (maj_exemple_count,maj_error_count))
-    print ((' num char = %d,%d') % (char_exemple_count,char_error_count))
-    return (total_error_count/total_exemple_count)*100.0
+    print (('num total = %d,%d') % (total_exemple_count,total_error_count))
+    print (('num nb = %d,%d') % (nb_exemple_count,nb_error_count))
+    print (('num min = %d,%d') % (min_exemple_count,min_error_count))
+    print (('num maj = %d,%d') % (maj_exemple_count,maj_error_count))
+    print (('num char = %d,%d') % (char_exemple_count,char_error_count))
+    
+    
+    
+    total_error_count/=total_exemple_count
+    nb_error_count/=nb_exemple_count
+    char_error_count/=char_exemple_count
+    min_error_count/=min_exemple_count
+    maj_error_count/=maj_exemple_count
+    nbc_error_count/=total_exemple_count
+    
+    vtotal_error_count/=vtotal_exemple_count
+    vnb_error_count/=vnb_exemple_count
+    vchar_error_count/=vchar_exemple_count
+    vmin_error_count/=vmin_exemple_count
+    vmaj_error_count/=vmaj_exemple_count
+    vnbc_error_count/=vtotal_exemple_count
+    
+    
     
+    return (total_error_count,nb_error_count,char_error_count,min_error_count,maj_error_count,nbc_error_count,\
+            vtotal_error_count,vnb_error_count,vchar_error_count,vmin_error_count,vmaj_error_count,vnbc_error_count)
+            
+def jobman_get_error(state,channel):
+    (all_t_error,nb_t_error,char_t_error,min_t_error,maj_t_error,nbc_t_error,
+     all_v_error,nb_v_error,char_v_error,min_v_error,maj_v_error,nbc_v_error)=mlp_get_nist_error(data_set=state.data_set,\
+                                                                                     model_name=state.model_name)
+    
+    state.all_t_error=all_t_error*100.0
+    state.nb_t_error=nb_t_error*100.0
+    state.char_t_error=char_t_error*100.0
+    state.min_t_error=min_t_error*100.0
+    state.maj_t_error=maj_t_error*100.0
+    state.nbc_t_error=nbc_t_error*100.0
+    
+    state.all_v_error=all_v_error*100.0
+    state.nb_v_error=nb_v_error*100.0
+    state.char_v_error=char_v_error*100.0
+    state.min_v_error=min_v_error*100.0
+    state.maj_v_error=maj_v_error*100.0
+    state.nbc_v_error=nbc_v_error*100.0
+    
+    pylearn.version.record_versions(state,[theano,ift6266,pylearn])
+    return channel.COMPLETE
 
 
 
@@ -358,7 +434,8 @@
                         tau=1e6,\
                         lr_t2_factor=0.5,\
                         init_model=0,\
-                        channel=0):
+                        channel=0,\
+                        detection_mode=0):
    
     
     if channel!=0:
@@ -403,7 +480,8 @@
                         n_in=32*32,\
                         n_hidden=nb_hidden,\
                         n_out=nb_targets,
-                        learning_rate=learning_rate)
+                        learning_rate=learning_rate,
+                        detection_mode=detection_mode)
                         
                         
     # check if we want to initialise the weights with a previously calculated model
@@ -421,9 +499,15 @@
     # the cost we minimize during training is the negative log likelihood of 
     # the model plus the regularization terms (L1 and L2); cost is expressed
     # here symbolically
-    cost = classifier.negative_log_likelihood(y) \
+    if(detection_mode==0):
+        cost = classifier.negative_log_likelihood(y) \
          + L1_reg * classifier.L1 \
          + L2_reg * classifier.L2_sqr 
+    else:
+        cost = classifier.cross_entropy(y) \
+         + L1_reg * classifier.L1 \
+         + L2_reg * classifier.L2_sqr 
+	 
 
     # compiling a theano function that computes the mistakes that are made by 
     # the model on a minibatch
@@ -504,7 +588,6 @@
             
             #train model
             cost_ij = train_model(x,y)
-    
             if (minibatch_index) % validation_frequency == 0: 
                 #save the current learning rate
                 learning_rate_list.append(classifier.lr.value)
@@ -645,6 +728,7 @@
 										lr_t2_factor=state.lr_t2_factor,
                                         data_set=state.data_set,
                                         init_model=state.init_model,
+                                        detection_mode = state.detection_mode,\
                                         channel=channel)
     state.train_error=train_error
     state.validation_error=validation_error