SHOGUN  6.1.3
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 #include <shogun/lib/Signal.h>
15 
16 using namespace shogun;
17 
19 : CLinearMachine(), learn_rate(0.1), max_iter(1000)
20 {
21 }
22 
24 : CLinearMachine(), learn_rate(.1), max_iter(1000)
25 {
26  set_features(traindat);
27  set_labels(trainlab);
28 }
29 
31 {
32 }
33 
35 {
38 
39  if (data)
40  {
41  if (!data->has_property(FP_DOT))
42  SG_ERROR("Specified features are not of type CDotFeatures\n")
43  set_features((CDotFeatures*) data);
44  }
46  bool converged=false;
47  int32_t iter=0;
48  SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
49  int32_t num_feat=features->get_dim_feature_space();
50  int32_t num_vec=features->get_num_vectors();
51 
52  ASSERT(num_vec==train_labels.vlen)
53  SGVector<float64_t> w(num_feat);
54  float64_t* tmp_w=SG_MALLOC(float64_t, num_feat);
55  memset(tmp_w, 0, sizeof(float64_t)*num_feat);
56  float64_t* output=SG_MALLOC(float64_t, num_vec);
57 
58  //start with uniform w, bias=0, tmp_bias=0
59  bias=0;
60  float64_t tmp_bias=0;
61  for (int32_t i=0; i<num_feat; i++)
62  w[i]=1.0/num_feat;
63 
64  //loop till we either get everything classified right or reach max_iter
65 
66  while (!(cancel_computation()) && (!converged && iter < max_iter))
67  {
68  converged=true;
69  SG_INFO("Iteration Number : %d of max %d\n", iter, max_iter);
70 
71  for (int32_t i=0; i<num_vec; i++)
72  {
73  output[i] = features->dense_dot(i, w.vector, w.vlen) + bias;
74 
75  if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
76  {
77  converged=false;
78  bias+=learn_rate*train_labels.vector[i];
79  features->add_to_dense_vec(learn_rate*train_labels.vector[i], i, w.vector, w.vlen);
80  }
81 
82  // Add current w to tmp_w, and current bias to tmp_bias
83  // To calculate the sum of each iteration's w, bias
84  for (int32_t j=0; j<num_feat; j++)
85  tmp_w[j]+=w[j];
86  tmp_bias+=bias;
87  }
88  iter++;
89  }
90 
91  if (converged)
92  SG_INFO("Averaged Perceptron algorithm converged after %d iterations.\n", iter)
93  else
94  SG_WARNING("Averaged Perceptron algorithm did not converge after %d iterations.\n", max_iter)
95 
96  // calculate and set the average paramter of w, bias
97  for (int32_t i=0; i<num_feat; i++)
98  w[i]=tmp_w[i]/(num_vec*iter);
99  bias=tmp_bias/(num_vec*iter);
100 
101  SG_FREE(output);
102  SG_FREE(tmp_w);
103 
104  set_w(w);
105 
106  return converged;
107 }
#define SG_INFO(...)
Definition: SGIO.h:117
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
virtual void set_w(const SGVector< float64_t > src_w)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:436
#define SG_ERROR(...)
Definition: SGIO.h:128
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:44
virtual int32_t get_dim_feature_space() const =0
#define ASSERT(x)
Definition: SGIO.h:176
double float64_t
Definition: common.h:60
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
SG_FORCED_INLINE bool cancel_computation() const
Definition: Machine.h:319
CDotFeatures * features
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:69
virtual bool train_machine(CFeatures *data=NULL)
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
#define SG_WARNING(...)
Definition: SGIO.h:127
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:72
index_t vlen
Definition: SGVector.h:571

SHOGUN Machine Learning Toolbox - Documentation