SHOGUN
v1.1.0
|
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 <shogun/classifier/Perceptron.h> 00012 #include <shogun/features/Labels.h> 00013 #include <shogun/mathematics/Math.h> 00014 00015 using namespace shogun; 00016 00017 CPerceptron::CPerceptron() 00018 : CLinearMachine(), learn_rate(0.1), max_iter(1000) 00019 { 00020 } 00021 00022 CPerceptron::CPerceptron(CDotFeatures* traindat, CLabels* trainlab) 00023 : CLinearMachine(), learn_rate(.1), max_iter(1000) 00024 { 00025 set_features(traindat); 00026 set_labels(trainlab); 00027 } 00028 00029 CPerceptron::~CPerceptron() 00030 { 00031 } 00032 00033 bool CPerceptron::train_machine(CFeatures* data) 00034 { 00035 ASSERT(labels); 00036 if (data) 00037 { 00038 if (!data->has_property(FP_DOT)) 00039 SG_ERROR("Specified features are not of type CDotFeatures\n"); 00040 set_features((CDotFeatures*) data); 00041 } 00042 ASSERT(features); 00043 bool converged=false; 00044 int32_t iter=0; 00045 SGVector<int32_t> train_labels=labels->get_int_labels(); 00046 int32_t num_feat=features->get_dim_feature_space(); 00047 int32_t num_vec=features->get_num_vectors(); 00048 00049 ASSERT(num_vec==train_labels.vlen); 00050 SG_FREE(w); 00051 w_dim=num_feat; 00052 w=SG_MALLOC(float64_t, num_feat); 00053 float64_t* output=SG_MALLOC(float64_t, num_vec); 00054 00055 //start with uniform w, bias=0 00056 bias=0; 00057 for (int32_t i=0; i<num_feat; i++) 00058 w[i]=1.0/num_feat; 00059 00060 //loop till we either get everything classified right or reach max_iter 00061 00062 while (!converged && iter<max_iter) 00063 { 00064 converged=true; 00065 for (int32_t i=0; i<num_vec; i++) 00066 { 00067 output[i]=apply(i); 00068 00069 if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i]) 00070 { 00071 converged=false; 00072 bias+=learn_rate*train_labels.vector[i]; 00073 features->add_to_dense_vec(learn_rate*train_labels.vector[i], i, w, w_dim); 00074 } 00075 } 00076 00077 iter++; 00078 } 00079 00080 if (converged) 00081 SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter); 00082 else 00083 SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter); 00084 00085 SG_FREE(output); 00086 train_labels.free_vector(); 00087 00088 return converged; 00089 }