SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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/features/Labels.h>
13 
14 using namespace shogun;
15 
17 : CLinearMachine(), learn_rate(0.1), max_iter(1000)
18 {
19 }
20 
22 : CLinearMachine(), learn_rate(.1), max_iter(1000)
23 {
24  set_features(traindat);
25  set_labels(trainlab);
26 }
27 
29 {
30 }
31 
33 {
34  ASSERT(labels);
35  if (data)
36  {
37  if (!data->has_property(FP_DOT))
38  SG_ERROR("Specified features are not of type CDotFeatures\n");
39  set_features((CDotFeatures*) data);
40  }
42  bool converged=false;
43  int32_t iter=0;
44  SGVector<int32_t> train_labels=labels->get_int_labels();
45  int32_t num_feat=features->get_dim_feature_space();
46  int32_t num_vec=features->get_num_vectors();
47 
48  ASSERT(num_vec==train_labels.vlen);
49  SG_FREE(w);
50  w_dim=num_feat;
51  w=SG_MALLOC(float64_t, num_feat);
52  float64_t* tmp_w=SG_MALLOC(float64_t, num_feat);
53 
54  float64_t* output=SG_MALLOC(float64_t, num_vec);
55  //start with uniform w, bias=0, tmp_bias=0
56  bias=0;
57  float64_t tmp_bias=0;
58  for (int32_t i=0; i<num_feat; i++)
59  w[i]=1.0/num_feat;
60 
61  //loop till we either get everything classified right or reach max_iter
62 
63  while (!converged && iter<max_iter)
64  {
65  converged=true;
66  for (int32_t i=0; i<num_vec; i++)
67  {
68  output[i]=apply(i);
69 
70  if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
71  {
72  converged=false;
73  bias+=learn_rate*train_labels.vector[i];
74  features->add_to_dense_vec(learn_rate*train_labels.vector[i], i, w, w_dim);
75  }
76 
77  // Add current w to tmp_w, and current bias to tmp_bias
78  // To calculate the sum of each iteration's w, bias
79  for (int32_t j=0; j<num_feat; j++)
80  tmp_w[j]+=w[j];
81  tmp_bias+=bias;
82  }
83  iter++;
84  }
85 
86  if (converged)
87  SG_INFO("Averaged Perceptron algorithm converged after %d iterations.\n", iter);
88  else
89  SG_WARNING("Averaged Perceptron algorithm did not converge after %d iterations.\n", max_iter);
90 
91  // calculate and set the average paramter of w, bias
92  for (int32_t i=0; i<num_feat; i++)
93  w[i]=tmp_w[i]/(num_vec*iter);
94  bias=tmp_bias/(num_vec*iter);
95 
96  SG_FREE(output);
97  train_labels.free_vector();
98  SG_FREE(tmp_w);
99 
100  return converged;
101 }
bool has_property(EFeatureProperty p)
Definition: Features.cpp:337
#define SG_INFO(...)
Definition: SGIO.h:73
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
virtual int32_t get_num_vectors() const =0
#define SG_ERROR(...)
Definition: SGIO.h:75
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)=0
Features that support dot products among other operations.
Definition: DotFeatures.h:41
virtual int32_t get_dim_feature_space() const =0
CLabels * labels
Definition: Machine.h:251
virtual void free_vector()
Definition: DataType.h:212
#define ASSERT(x)
Definition: SGIO.h:102
SGVector< int32_t > get_int_labels()
Definition: Labels.cpp:152
double float64_t
Definition: common.h:56
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:61
#define SG_FREE(ptr)
Definition: memory.h:39
CDotFeatures * features
The class Features is the base class of all feature objects.
Definition: Features.h:56
virtual void set_features(CDotFeatures *feat)
#define SG_WARNING(...)
Definition: SGIO.h:74
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:63
virtual CLabels * apply()
#define SG_MALLOC(type, len)
Definition: memory.h:36
virtual bool train(CFeatures *data=NULL)
index_t vlen
Definition: DataType.h:248

SHOGUN Machine Learning Toolbox - Documentation