SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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 <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), m_initialize_hyperplane(true)
00020 {
00021 }
00022 
00023 CPerceptron::CPerceptron(CDotFeatures* traindat, CLabels* trainlab)
00024 : CLinearMachine(), learn_rate(0.1), max_iter(1000), m_initialize_hyperplane(true)
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     float64_t* output=SG_MALLOC(float64_t, num_vec);
00055 
00056     if (m_initialize_hyperplane)
00057     {
00058         //start with uniform w, bias=0
00059         w=SGVector<float64_t>(num_feat);
00060         bias=0;
00061         for (int32_t i=0; i<num_feat; i++)
00062             w.vector[i]=1.0/num_feat;
00063     }
00064 
00065     //loop till we either get everything classified right or reach max_iter
00066     while (!converged && iter<max_iter)
00067     {
00068         converged=true;
00069         for (int32_t i=0; i<num_vec; i++)
00070         {
00071             output[i]=apply_one(i);
00072 
00073             if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
00074             {
00075                 converged=false;
00076                 bias+=learn_rate*train_labels.vector[i];
00077                 features->add_to_dense_vec(learn_rate*train_labels.vector[i], i, w.vector, w.vlen);
00078             }
00079         }
00080 
00081         iter++;
00082     }
00083 
00084     if (converged)
00085         SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter)
00086     else
00087         SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter)
00088 
00089     SG_FREE(output);
00090 
00091     return converged;
00092 }
00093 
00094 void CPerceptron::set_initialize_hyperplane(bool initialize_hyperplane)
00095 {
00096     m_initialize_hyperplane = initialize_hyperplane;
00097 }
00098 
00099 bool CPerceptron::get_initialize_hyperplane()
00100 {
00101     return m_initialize_hyperplane;
00102 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation