SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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)
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  }
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  w=SGVector<float64_t>(num_feat);
55  float64_t* output=SG_MALLOC(float64_t, num_vec);
56 
57  //start with uniform w, bias=0
58  bias=0;
59  for (int32_t i=0; i<num_feat; i++)
60  w.vector[i]=1.0/num_feat;
61 
62  //loop till we either get everything classified right or reach max_iter
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_one(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.vector, w.vlen);
75  }
76  }
77 
78  iter++;
79  }
80 
81  if (converged)
82  SG_INFO("Perceptron algorithm converged after %d iterations.\n", iter);
83  else
84  SG_WARNING("Perceptron algorithm did not converge after %d iterations.\n", max_iter);
85 
86  SG_FREE(output);
87 
88  return converged;
89 }

SHOGUN Machine Learning Toolbox - Documentation