SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Perceptron.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) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
12 #include <shogun/features/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 {
35  ASSERT(labels);
36  if (data)
37  {
38  if (!data->has_property(FP_DOT))
39  SG_ERROR("Specified features are not of type CDotFeatures\n");
40  set_features((CDotFeatures*) data);
41  }
43  bool converged=false;
44  int32_t iter=0;
45  SGVector<int32_t> train_labels=labels->get_int_labels();
46  int32_t num_feat=features->get_dim_feature_space();
47  int32_t num_vec=features->get_num_vectors();
48 
49  ASSERT(num_vec==train_labels.vlen);
50  SG_FREE(w);
51  w_dim=num_feat;
52  w=SG_MALLOC(float64_t, num_feat);
53  float64_t* output=SG_MALLOC(float64_t, num_vec);
54 
55  //start with uniform w, bias=0
56  bias=0;
57  for (int32_t i=0; i<num_feat; i++)
58  w[i]=1.0/num_feat;
59 
60  //loop till we either get everything classified right or reach max_iter
61 
62  while (!converged && iter<max_iter)
63  {
64  converged=true;
65  for (int32_t i=0; i<num_vec; i++)
66  {
67  output[i]=apply(i);
68 
69  if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
70  {
71  converged=false;
72  bias+=learn_rate*train_labels.vector[i];
73  features->add_to_dense_vec(learn_rate*train_labels.vector[i], i, w, w_dim);
74  }
75  }
76 
77  iter++;
78  }
79 
80  if (converged)
81  SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter);
82  else
83  SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter);
84 
85  SG_FREE(output);
86  train_labels.free_vector();
87 
88  return converged;
89 }
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
float64_t learn_rate
Definition: Perceptron.h:78
virtual void set_features(CDotFeatures *feat)
virtual bool train_machine(CFeatures *data=NULL)
Definition: Perceptron.cpp:33
#define SG_WARNING(...)
Definition: SGIO.h:74
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:63
virtual CLabels * apply()
virtual ~CPerceptron()
Definition: Perceptron.cpp:29
#define SG_MALLOC(type, len)
Definition: memory.h:36
index_t vlen
Definition: DataType.h:248

SHOGUN Machine Learning Toolbox - Documentation