Perceptron.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Perceptron
6  *
7  *
8  *
9  * \author O. Krause
10  * \date 2010
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 #ifndef SHARK_ALGORITHMS_TRAINERS_PERCEPTRON_H
35 #define SHARK_ALGORITHMS_TRAINERS_PERCEPTRON_H
36 
37 
40 
41 namespace shark{
42 
43 //! \brief Perceptron online learning algorithm
44 template<class InputType>
45 class Perceptron : public AbstractTrainer<KernelClassifier<InputType>,unsigned int >
46 {
47 public:
48  /// \brief Constructor.
49  ///
50  /// @param kernel is the (Mercer) kernel function.
51  /// @param maxTimesPattern defines the maximum number of times the data is processed before the algorithms stopps.
52  Perceptron(AbstractKernelFunction<InputType>* kernel, std::size_t maxTimesPattern = 10000)
53  :mpe_kernel(kernel),m_maxTimesPattern(maxTimesPattern){}
54 
55  /// \brief From INameable: return the class name.
56  std::string name() const
57  { return "Perceptron"; }
58 
60  std::size_t patterns = dataset.numberOfElements();
61  KernelExpansion<InputType>& model= classifier.decisionFunction();
62  model.setStructure(mpe_kernel,dataset.inputs(),false,1);
63  model.alpha().clear();
64 
65  bool err;
66  std::size_t iter = 0;
67  do {
68  err = false;
69  for (std::size_t i = 0; i != patterns; i++){
70  double result = model(dataset.element(i).input)(0);
71  //perceptron learning rule with modified target from -1;1
72  double label = dataset.element(i).label*2.0-1;
73  if ( result * label <= 0.0){
74  model.alpha(i,0) += label;
75  err = true;
76  }
77  }
78  if (iter > m_maxTimesPattern * patterns) break; // probably non-separable data
79  iter++;
80  } while (err);
81  }
82 private:
84  std::size_t m_maxTimesPattern; //< maximum number of times a training is processed
85 };
86 
87 
88 }
89 #endif