SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AveragedPerceptron.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011 Hidekazu Oiwa
8  */
9 
11 #include <shogun/labels/Labels.h>
14 
15 using namespace shogun;
16 
18 : CLinearMachine(), learn_rate(0.1), max_iter(1000)
19 {
20 }
21 
23 : CLinearMachine(), learn_rate(.1), max_iter(1000)
24 {
25  set_features(traindat);
26  set_labels(trainlab);
27 }
28 
30 {
31 }
32 
34 {
37 
38  if (data)
39  {
40  if (!data->has_property(FP_DOT))
41  SG_ERROR("Specified features are not of type CDotFeatures\n")
42  set_features((CDotFeatures*) data);
43  }
45  bool converged=false;
46  int32_t iter=0;
47  SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
48  int32_t num_feat=features->get_dim_feature_space();
49  int32_t num_vec=features->get_num_vectors();
50 
51  ASSERT(num_vec==train_labels.vlen)
52  w=SGVector<float64_t>(num_feat);
53  float64_t* tmp_w=SG_MALLOC(float64_t, num_feat);
54  float64_t* output=SG_MALLOC(float64_t, num_vec);
55 
56  //start with uniform w, bias=0, tmp_bias=0
57  bias=0;
58  float64_t tmp_bias=0;
59  for (int32_t i=0; i<num_feat; i++)
60  w[i]=1.0/num_feat;
61 
62  //loop till we either get everything classified right or reach max_iter
63 
64  while (!converged && iter<max_iter)
65  {
66  converged=true;
67  for (int32_t i=0; i<num_vec; i++)
68  {
69  output[i]=apply_one(i);
70 
71  if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
72  {
73  converged=false;
74  bias+=learn_rate*train_labels.vector[i];
75  features->add_to_dense_vec(learn_rate*train_labels.vector[i], i, w.vector, w.vlen);
76  }
77 
78  // Add current w to tmp_w, and current bias to tmp_bias
79  // To calculate the sum of each iteration's w, bias
80  for (int32_t j=0; j<num_feat; j++)
81  tmp_w[j]+=w[j];
82  tmp_bias+=bias;
83  }
84  iter++;
85  }
86 
87  if (converged)
88  SG_INFO("Averaged Perceptron algorithm converged after %d iterations.\n", iter)
89  else
90  SG_WARNING("Averaged Perceptron algorithm did not converge after %d iterations.\n", max_iter)
91 
92  // calculate and set the average paramter of w, bias
93  for (int32_t i=0; i<num_feat; i++)
94  w[i]=tmp_w[i]/(num_vec*iter);
95  bias=tmp_bias/(num_vec*iter);
96 
97  SG_FREE(output);
98  SG_FREE(tmp_w);
99 
100  return converged;
101 }

SHOGUN Machine Learning Toolbox - Documentation