KernelLogisticRegressor.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
8 #include <nuklei/ObservationIO.h>
9 #include <klr_train.h>
10 
11 namespace nuklei
12 {
13 
14 
15  KernelLogisticRegressor::KernelLogisticRegressor
16  (const KernelCollection &data,
17  const std::vector<int>& labels) :
18  trainSet_(data), labels_(labels)
19  {
20  NUKLEI_TRACE_BEGIN();
21  NUKLEI_ASSERT(trainSet_.size() == labels_.size());
22  computeGramMatrix();
23  NUKLEI_TRACE_END();
24  }
25 
26  KernelLogisticRegressor::KernelLogisticRegressor
27  (const KernelCollection &data,
28  const GMatrix& gramMatrix,
29  const std::vector<int>& labels) :
30  trainSet_(data), gramMatrix_(gramMatrix), labels_(labels)
31  {
32  NUKLEI_ASSERT(trainSet_.size() == labels_.size());
33  NUKLEI_ASSERT(gramMatrix_.GetRows() == trainSet_.size());
34  NUKLEI_ASSERT(gramMatrix_.GetColumns() == trainSet_.size());
35  }
36 
37 
39  const std::vector<int>& labels)
40  {
41  trainSet_ = data;
42  labels_ = labels;
43  computeGramMatrix();
44  vklr_ = boost::optional<GMatrix>();
45  }
46 
47  void KernelLogisticRegressor::computeGramMatrix()
48  {
49  NUKLEI_TRACE_BEGIN();
50  gramMatrix_ = GMatrix(trainSet_.size(), trainSet_.size());
51 
52  for (unsigned i = 0; i < trainSet_.size(); ++i)
53  {
54  for (unsigned j = 0; j < trainSet_.size(); ++j)
55  {
56  gramMatrix_(i, j) = trainSet_.at(i).polyEval(trainSet_.at(j));
57  }
58  }
59  NUKLEI_TRACE_END();
60  }
61 
62  void KernelLogisticRegressor::train(const double delta,
63  const unsigned itrNewton)
64  {
65  NUKLEI_TRACE_BEGIN();
66 
67  NUKLEI_ASSERT(trainSet_.size() == labels_.size());
68  NUKLEI_ASSERT(trainSet_.size() != 0);
69 
70  GMatrix vklr(trainSet_.size(), 2);
71 
72  klr_train(gramMatrix_, gramMatrix_.GetRows(), gramMatrix_.GetColumns(),
73  &labels_.front(), labels_.size(),
74  vklr, vklr.GetRows(), vklr.GetColumns(),
75  delta, itrNewton);
76 
77  vklr_ = vklr.Transpose();
78  NUKLEI_TRACE_END();
79  }
80 
81 
83  {
84  NUKLEI_TRACE_BEGIN();
85  NUKLEI_ASSERT(vklr_);
86  Vector2 pr;
87 
88  GMatrix Ktest(trainSet_.size(), 1);
89  for (unsigned i = 0; i < trainSet_.size(); ++i)
90  {
91  Ktest(i, 0) = trainSet_.at(i).polyEval(t);
92  }
93 
94  GMatrix f = *vklr_ * Ktest;
95  double m = std::max(f(0,0), f(1,0));
96 
97  double e1 = std::exp(f(0,0)-m), e2 = std::exp(f(1,0)-m);
98  pr.X() = e1/(e1+e2);
99  pr.Y() = e2/(e1+e2);
100 
101  return pr;
102  NUKLEI_TRACE_END();
103  }
104 
105 
106  GMatrix KernelLogisticRegressor::test(const KernelCollection &testSet) const
107  {
108  NUKLEI_TRACE_BEGIN();
109 
110  GMatrix pr(2, testSet.size());
111 
112  for (KernelCollection::const_iterator ti = testSet.begin();
113  ti != testSet.end(); ++ti)
114  {
115  Vector2 p = test(*ti);
116  pr(0,std::distance(testSet.begin(), ti)) = p.X();
117  pr(1,std::distance(testSet.begin(), ti)) = p.Y();
118  }
119  return pr;
120  NUKLEI_TRACE_END();
121  }
122 
123 
124 }
Public namespace.
Definition: Color.cpp:9
Container::const_iterator const_iterator
KernelCollection iterator.
This class acts as a vector-like container for kernels. It also provides methods related to kernel de...
void train(const double delta=0.0001, const unsigned itrNewton=5)
Computes KLR weights.
void setData(const KernelCollection &data, const std::vector< int > &labels)
Imports input from data and labels, and computes the Gram matrix of the data.
const GMatrix & vklr() const
Returns KLR weights.
Container::reference at(Container::size_type n)
Returns the kernel at index n.
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
Polymorphic kernel class.
Definition: Kernel.h:45
Container::iterator end()
Returns an iterator pointing to the last kernel.
Container::iterator begin()
Returns an iterator pointing to the first kernel.
Vector2 test(const kernel::base &t) const
Returns a pair of values which indicate the probability of classes 1 and 2.
Container::size_type size() const
Returns the number of kernels.
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.