Esempio n. 1
0
def run_classify(n_train,n_test,n_valid,alpha,lamda,batch,NN_type,Reg_type):
#check and load data
    #if(not data_process.check()):
    #data_process.process()
    #data = data_process.loaddata()
    #n_exemple = data.shape[0]
   # d = data.shape[1]-1
   # data[:,:-1]=tools.normalisation(data[:,:-1])
    data = numpy.loadtxt("nntest.txt")
    data[:,:-1] = data_process.normalisation(data[:,:-1])
    n_exemple = data.shape[0]
    d = data.shape[1]-1
    #print data
#shuffle the data
    inds = range(n_exemple)
    random.shuffle(inds)

#split data
    tmp_test = n_train+n_test
    tmp_valid = tmp_test + n_valid
    inds_train = inds[:]
    inds_test = inds[n_train:tmp_test]
    inds_valid = inds[tmp_test:tmp_valid]
    train_data = data[inds_train,:]

    print "Train data shape: ",train_data.shape
    test_data = data[inds_test,:]
    valid_data = data[inds_valid,:]
    test_input = test_data[:,:-1]
    test_labels = test_data[:,-1]
    valid_input  = valid_data[:,:-1]
    valid_labels = valid_data[:,-1]

#define param
# Nombre de classes
    n_classes = 2
    m = n_classes

#create and train the model
    model = NN.NN(m,d,alpha,lamda,batch,NN_type,Reg_type)
    model.train(train_data,valid_input,valid_labels,test_input,test_labels)

#compute the prediction on train data
    t1 = time.clock()
    les_comptes = model.compute_predictions(train_data[:,:-1])
    t2 = time.clock()
    print 'It takes ', t2-t1, ' secondes to compute the prediction on ', test_data.shape[0],' points of test'
    classes_pred = numpy.argmax(les_comptes,axis=1)+1
    confmat = tools.teste(train_data[:,-1], classes_pred,n_classes)
    print 'La matrice de confusion est:'
    print confmat

    # Error of test
    sum_preds = numpy.sum(confmat)
    sum_correct = numpy.sum(numpy.diag(confmat))
    print "The error of train is ", 100*(1.0 - (float(sum_correct) / sum_preds)),"%"

#compute the prediction on valid data
    t1 = time.clock()
    les_comptes = model.compute_predictions(valid_input)
    t2 = time.clock()
    print 'It takes ', t2-t1, ' secondes to compute the prediction on ', test_data.shape[0],' points of test'
    classes_pred = numpy.argmax(les_comptes,axis=1)+1
    confmat = tools.teste(valid_labels, classes_pred,n_classes)
    print 'La matrice de confusion est:'
    print confmat

# Error of test
    sum_preds = numpy.sum(confmat)
    sum_correct = numpy.sum(numpy.diag(confmat))
    print "The error of validation is ", 100*(1.0 - (float(sum_correct) / sum_preds)),"%"


#compute the prediction on test data
    t1 = time.clock()
    les_comptes = model.compute_predictions(test_input)
    t2 = time.clock()
    print 'It takes ', t2-t1, ' secondes to compute the prediction on ', test_data.shape[0],' points of test'
    classes_pred = numpy.argmax(les_comptes,axis=1)+1
    confmat = tools.teste(test_labels, classes_pred,n_classes)
    print 'La matrice de confusion est:'
    print confmat

# Error of test
    sum_preds = numpy.sum(confmat)
    sum_correct = numpy.sum(numpy.diag(confmat))
    print "The error of test is ", 100*(1.0 - (float(sum_correct) / sum_preds)),"%"
Esempio n. 2
0
    def train(self,train_set,valide_set,valide_labels,test_set,test_labels):
        nb_valide=valide_set.shape[0]
        changeTimer = 0
        print 'training rate is ',self.alpha
        finish = False
        nb_train=train_set.shape[0]
        max_iter = 10*nb_train
        itera=0
        seuil = 1
        taux = 100.0
        taux_valid =100.0
        k=0
        X=mat(train_set)
        print max_iter;
        while not finish:
            inds = range(train_set.shape[0])
            random.shuffle(inds)
            for i in range(self.batch):
                self.calculate_forward(X[inds[i],:-1])
                self.calculate_backward(X[inds[i],:])
                self.G1+=self.S1
                self.GaW1+=self.aW1
                itera+=1
                k+=1
                k=k%nb_train

            self.GaW1 = self.GaW1/self.batch
            self.G1 = self.G1/self.batch
            self.adjust_weight()

            if (itera%nb_train == 0):
                les_comptes=self.compute_predictions_train(test_set)
                classes_pred = argmax(les_comptes,axis=1)+1
                confmat = tools.teste(test_labels, classes_pred,self.m)
                sum_preds = sum(confmat)
                sum_correct = sum(diag(confmat))
                taux_test= 100*(1.0 - (float(sum_correct) / sum_preds))

                #taux de erreur pour validation
                les_comptes=self.compute_predictions_train(valide_set)
                classes_pred = argmax(les_comptes,axis=1)+1
                confmat = tools.teste(valide_labels, classes_pred,self.m)
                sum_preds = sum(confmat)
                sum_correct = sum(diag(confmat))
                taux_valid= 100*(1.0 - (float(sum_correct) / sum_preds))
                print "L'erreur de test est de validation est ", taux_valid,"%",time.clock()

                #taux de erreur pour train_set
                les_comptes=self.compute_predictions_train(train_set[:,:-1])
                classes_pred = argmax(les_comptes,axis=1)+1
                confmat = tools.teste(train_set[:,-1], classes_pred,self.m)
                sum_preds = sum(confmat)
                sum_correct = sum(diag(confmat))
                taux= 100*(1.0 - (float(sum_correct) / sum_preds))
                #print "iteration :", itera, ". L'erreur de test est de training est ", taux,"%",time.clock()
                #print "iteration :", itera
                print "L'erreur de test est de training est ", taux,"%",time.clock()


                if (taux_valid < self.best_model_valid_error):
                    self.best_model_valid_error = taux_valid
                    self.best_model_test_error = taux_test
                    print "Best model until now with error of validation:",self.best_model_valid_error,"%, computing time is ",time.clock()," seconds"
                    print "The best model has error rate ", self.best_model_test_error,"%, on test set. "
                    self.bestW1 = self.W1
                    self.bestB1 = self.B1

            self.G1 = self.G1*0.0
            self.GaW1 =self.GaW1*0.0



            if (taux < 30) and (changeTimer == 0):
                self.alpha=self.alpha/2
                changeTimer += 1
                print "alpha changed !!! because taux < 30"
                print self.alpha



            if (taux < 10) and (changeTimer == 1):
                self.alpha=self.alpha/2
                changeTimer += 1
                print "alpha changed !!! because taux < 10"
                print self.alpha


            if (taux_valid< seuil) or (itera > max_iter):
                self.bestW1 = self.W1
                self.bestB1 = self.B1
                finish = True



        print self.bestW1.shape
        print self.bestB1.shape