Perceptron.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "classifier/Perceptron.h"
00012 #include "features/Labels.h"
00013 #include "lib/Mathematics.h"
00014 
00015 CPerceptron::CPerceptron()
00016 : CLinearClassifier(), learn_rate(0.1), max_iter(1000)
00017 {
00018 }
00019 
00020 CPerceptron::CPerceptron(CDotFeatures* traindat, CLabels* trainlab)
00021 : CLinearClassifier(), learn_rate(.1), max_iter(1000)
00022 {
00023     set_features(traindat);
00024     set_labels(trainlab);
00025 }
00026 
00027 CPerceptron::~CPerceptron()
00028 {
00029 }
00030 
00031 bool CPerceptron::train()
00032 {
00033     ASSERT(labels);
00034     ASSERT(features);
00035     bool converged=false;
00036     int32_t iter=0;
00037     int32_t num_train_labels=0;
00038     int32_t* train_labels=labels->get_int_labels(num_train_labels);
00039     int32_t num_feat=features->get_dim_feature_space();
00040     int32_t num_vec=features->get_num_vectors();
00041 
00042     ASSERT(num_vec==num_train_labels);
00043     delete[] w;
00044     w_dim=num_feat;
00045     w=new float64_t[num_feat];
00046     float64_t* output=new float64_t[num_vec];
00047 
00048     //start with uniform w, bias=0
00049     bias=0;
00050     for (int32_t i=0; i<num_feat; i++)
00051         w[i]=1.0/num_feat;
00052 
00053     //loop till we either get everything classified right or reach max_iter
00054 
00055     while (!converged && iter<max_iter)
00056     {
00057         converged=true;
00058         for (int32_t i=0; i<num_vec; i++)
00059             output[i]=classify_example(i);
00060 
00061         for (int32_t i=0; i<num_vec; i++)
00062         {
00063             if (CMath::sign<float64_t>(output[i]) != train_labels[i])
00064             {
00065                 converged=false;
00066                 bias+=learn_rate*train_labels[i];
00067                 features->add_to_dense_vec(learn_rate*train_labels[i], i, w, w_dim);
00068             }
00069         }
00070 
00071         iter++;
00072     }
00073 
00074     if (converged)
00075         SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter);
00076     else
00077         SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter);
00078 
00079     delete[] output;
00080     delete[] train_labels;
00081 
00082     return converged;
00083 }

SHOGUN Machine Learning Toolbox - Documentation