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 }

SHOGUN Machine Learning Toolbox - Documentation