SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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/labels/Labels.h>
15 
16 using namespace shogun;
17 
19 : CLinearMachine(), learn_rate(0.1), max_iter(1000), m_initialize_hyperplane(true)
20 {
21 }
22 
24 : CLinearMachine(), learn_rate(0.1), max_iter(1000), m_initialize_hyperplane(true)
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  }
45 
47  bool converged=false;
48  int32_t iter=0;
49  SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
50  int32_t num_feat=features->get_dim_feature_space();
51  int32_t num_vec=features->get_num_vectors();
52 
53  ASSERT(num_vec==train_labels.vlen)
54  float64_t* output=SG_MALLOC(float64_t, num_vec);
55 
56  if (m_initialize_hyperplane)
57  {
58  //start with uniform w, bias=0
59  w=SGVector<float64_t>(num_feat);
60  bias=0;
61  for (int32_t i=0; i<num_feat; i++)
62  w.vector[i]=1.0/num_feat;
63  }
64 
65  //loop till we either get everything classified right or reach max_iter
66  while (!converged && iter<max_iter)
67  {
68  converged=true;
69  for (int32_t i=0; i<num_vec; i++)
70  {
71  output[i]=apply_one(i);
72 
73  if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
74  {
75  converged=false;
76  bias+=learn_rate*train_labels.vector[i];
77  features->add_to_dense_vec(learn_rate*train_labels.vector[i], i, w.vector, w.vlen);
78  }
79  }
80 
81  iter++;
82  }
83 
84  if (converged)
85  SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter)
86  else
87  SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter)
88 
89  SG_FREE(output);
90 
91  return converged;
92 }
93 
94 void CPerceptron::set_initialize_hyperplane(bool initialize_hyperplane)
95 {
96  m_initialize_hyperplane = initialize_hyperplane;
97 }
98 
100 {
101  return m_initialize_hyperplane;
102 }
#define SG_INFO(...)
Definition: SGIO.h:118
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
bool get_initialize_hyperplane()
get if the hyperplane should be initialized
Definition: Perceptron.cpp:99
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
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
virtual float64_t apply_one(int32_t vec_idx)
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
void set_initialize_hyperplane(bool initialize_hyperplane)
set if the hyperplane should be initialized
Definition: Perceptron.cpp:94
SGVector< float64_t > w
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
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:68
float64_t learn_rate
Definition: Perceptron.h:89
virtual bool train_machine(CFeatures *data=NULL)
Definition: Perceptron.cpp:34
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
#define SG_WARNING(...)
Definition: SGIO.h:128
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:73
virtual ~CPerceptron()
Definition: Perceptron.cpp:30

SHOGUN Machine Learning Toolbox - Documentation