Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "classifier/Perceptron.h"
00012 #include "features/Labels.h"
00013 #include "lib/Mathematics.h"
00014
00015 using namespace shogun;
00016
00017 CPerceptron::CPerceptron()
00018 : CLinearClassifier(), learn_rate(0.1), max_iter(1000)
00019 {
00020 }
00021
00022 CPerceptron::CPerceptron(CDotFeatures* traindat, CLabels* trainlab)
00023 : CLinearClassifier(), 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(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 int32_t num_train_labels=0;
00046 int32_t* train_labels=labels->get_int_labels(num_train_labels);
00047 int32_t num_feat=features->get_dim_feature_space();
00048 int32_t num_vec=features->get_num_vectors();
00049
00050 ASSERT(num_vec==num_train_labels);
00051 delete[] w;
00052 w_dim=num_feat;
00053 w=new float64_t[num_feat];
00054 float64_t* output=new float64_t[num_vec];
00055
00056
00057 bias=0;
00058 for (int32_t i=0; i<num_feat; i++)
00059 w[i]=1.0/num_feat;
00060
00061
00062
00063 while (!converged && iter<max_iter)
00064 {
00065 converged=true;
00066 for (int32_t i=0; i<num_vec; i++)
00067 output[i]=classify_example(i);
00068
00069 for (int32_t i=0; i<num_vec; i++)
00070 {
00071 if (CMath::sign<float64_t>(output[i]) != train_labels[i])
00072 {
00073 converged=false;
00074 bias+=learn_rate*train_labels[i];
00075 features->add_to_dense_vec(learn_rate*train_labels[i], i, w, w_dim);
00076 }
00077 }
00078
00079 iter++;
00080 }
00081
00082 if (converged)
00083 SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter);
00084 else
00085 SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter);
00086
00087 delete[] output;
00088 delete[] train_labels;
00089
00090 return converged;
00091 }