SHOGUN  6.1.3
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 #include <shogun/lib/Signal.h>
16 #include <shogun/base/range.h>
17 
18 using namespace shogun;
19 
21 : CLinearMachine(), learn_rate(0.1), max_iter(1000), m_initialize_hyperplane(true)
22 {
23 }
24 
26 : CLinearMachine(), learn_rate(0.1), max_iter(1000), m_initialize_hyperplane(true)
27 {
28  set_features(traindat);
29  set_labels(trainlab);
30 }
31 
33 {
34 }
35 
37 {
40 
41  if (data)
42  {
43  if (!data->has_property(FP_DOT))
44  SG_ERROR("Specified features are not of type CDotFeatures\n")
45  set_features((CDotFeatures*) data);
46  }
47 
49  bool converged=false;
50  int32_t iter=0;
51  SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
52  int32_t num_feat=features->get_dim_feature_space();
53  int32_t num_vec=features->get_num_vectors();
54 
55  ASSERT(num_vec==train_labels.vlen)
56  float64_t* output=SG_MALLOC(float64_t, num_vec);
57 
59  if (m_initialize_hyperplane)
60  {
61  w = SGVector<float64_t>(num_feat);
62  //start with uniform w, bias=0
63  bias=0;
64  for (int32_t i=0; i<num_feat; i++)
65  w.vector[i]=1.0/num_feat;
66  }
67 
68 
69  //loop till we either get everything classified right or reach max_iter
70  while (!(cancel_computation()) && (!converged && iter < max_iter))
71  {
72  converged=true;
73  for (auto example_idx : features->index_iterator())
74  {
75  const auto predicted_label = features->dense_dot(example_idx, w.vector, w.vlen) + bias;
76  const auto true_label = train_labels[example_idx];
77  output[example_idx] = predicted_label;
78 
79  if (CMath::sign<float64_t>(predicted_label) != true_label)
80  {
81  converged = false;
82  const auto gradient = learn_rate * train_labels[example_idx];
83  bias += gradient;
84  features->add_to_dense_vec(gradient, example_idx, w.vector, w.vlen);
85  }
86  }
87 
88  iter++;
89  }
90 
91  if (converged)
92  SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter)
93  else
94  SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter)
95 
96  SG_FREE(output);
97 
98  set_w(w);
99 
100  return converged;
101 }
102 
103 void CPerceptron::set_initialize_hyperplane(bool initialize_hyperplane)
104 {
105  m_initialize_hyperplane = initialize_hyperplane;
106 }
107 
109 {
110  return m_initialize_hyperplane;
111 }
#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
bool get_initialize_hyperplane()
get if the hyperplane should be initialized
Definition: Perceptron.cpp:108
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 Range< int32_t > index_iterator() const
Definition: Features.h:123
void set_initialize_hyperplane(bool initialize_hyperplane)
set if the hyperplane should be initialized
Definition: Perceptron.cpp:103
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
virtual SGVector< float64_t > get_w() const
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
float64_t learn_rate
Definition: Perceptron.h:89
virtual bool train_machine(CFeatures *data=NULL)
Definition: Perceptron.cpp:36
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
virtual ~CPerceptron()
Definition: Perceptron.cpp:32
index_t vlen
Definition: SGVector.h:571

SHOGUN Machine Learning Toolbox - Documentation