AveragedPerceptron.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) 2011 Hidekazu Oiwa
00008  */
00009 
00010 #include <shogun/classifier/AveragedPerceptron.h>
00011 #include <shogun/features/Labels.h>
00012 #include <shogun/mathematics/Math.h>
00013 
00014 using namespace shogun;
00015 
00016 CAveragedPerceptron::CAveragedPerceptron()
00017 : CLinearMachine(), learn_rate(0.1), max_iter(1000)
00018 {
00019 }
00020 
00021 CAveragedPerceptron::CAveragedPerceptron(CDotFeatures* traindat, CLabels* trainlab)
00022 : CLinearMachine(), learn_rate(.1), max_iter(1000)
00023 {
00024     set_features(traindat);
00025     set_labels(trainlab);
00026 }
00027 
00028 CAveragedPerceptron::~CAveragedPerceptron()
00029 {
00030 }
00031 
00032 bool CAveragedPerceptron::train(CFeatures* data)
00033 {
00034     ASSERT(labels);
00035     if (data)
00036     {
00037         if (!data->has_property(FP_DOT))
00038             SG_ERROR("Specified features are not of type CDotFeatures\n");
00039         set_features((CDotFeatures*) data);
00040     }
00041     ASSERT(features);
00042     bool converged=false;
00043     int32_t iter=0;
00044     SGVector<int32_t> train_labels=labels->get_int_labels();
00045     int32_t num_feat=features->get_dim_feature_space();
00046     int32_t num_vec=features->get_num_vectors();
00047 
00048     ASSERT(num_vec==train_labels.vlen);
00049     SG_FREE(w);
00050     w_dim=num_feat;
00051     w=SG_MALLOC(float64_t, num_feat);
00052     float64_t* tmp_w=SG_MALLOC(float64_t, num_feat);
00053 
00054     float64_t* output=SG_MALLOC(float64_t, num_vec);
00055     //start with uniform w, bias=0, tmp_bias=0
00056     bias=0;
00057     float64_t tmp_bias=0;
00058     for (int32_t i=0; i<num_feat; i++)
00059         w[i]=1.0/num_feat;
00060 
00061     //loop till we either get everything classified right or reach max_iter
00062 
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(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, w_dim);
00075             }
00076 
00077             // Add current w to tmp_w, and current bias to tmp_bias
00078             // To calculate the sum of each iteration's w, bias
00079             for (int32_t j=0; j<num_feat; j++)
00080                 tmp_w[j]+=w[j];
00081             tmp_bias+=bias;
00082         }
00083         iter++;
00084     }
00085 
00086     if (converged)
00087         SG_INFO("Averaged Perceptron algorithm converged after %d iterations.\n", iter);
00088     else
00089         SG_WARNING("Averaged Perceptron algorithm did not converge after %d iterations.\n", max_iter);
00090 
00091     // calculate and set the average paramter of w, bias
00092     for (int32_t i=0; i<num_feat; i++)
00093         w[i]=tmp_w[i]/(num_vec*iter);
00094     bias=tmp_bias/(num_vec*iter);
00095 
00096     SG_FREE(output);
00097     train_labels.free_vector();
00098     SG_FREE(tmp_w);
00099 
00100     return converged;
00101 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation