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