KernelLogisticRegressor.h
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 
7 
8 #ifndef NUKLEI_KERNEL_LOGISTIC_REGRESSOR_H
9 #define NUKLEI_KERNEL_LOGISTIC_REGRESSOR_H
10 
11 
13 
14 namespace nuklei
15 {
16 
17  /**
18  * @ingroup kernels
19  * @brief Implements kernel logistic regression.
20  *
21  * This class implements a two-class nonlinear classifier for @f$
22  * SE(3) @f$ data. KLR is discussed in @ref kernels_klr.
23  *
24  * This class is based on @c libklr, which is provided with Makoto Yamada's <a
25  * href="http://sugiyama-www.cs.titech.ac.jp/~yamada/iwklr.html">IWKLR</a>.
26  */
28  {
29  /** @brief */
31  /**
32  * @brief Imports input from @p data and @p labels, and computes the Gram
33  * matrix of the data.
34  *
35  * The two structures @p data and @p labels should contain the same number
36  * of elements. The @f$ i^{\mathrm{th}} @f$ element of @p labels indicates
37  * the class of the @f$ i^{\mathrm{th}} @f$ element of @p data. The two
38  * allowed values in @p labels are @c 1 and @c 2.
39  */
41  const std::vector<int>& labels);
42  /**
43  * @brief Imports input from @p data and @p labels and the Gram matrix @p
44  * gramMatrix.
45  *
46  * The two structures @p data and @p labels should contain the same number
47  * of elements. The @f$ i^{\mathrm{th}} @f$ element of @p labels indicates
48  * the class of the @f$ i^{\mathrm{th}} @f$ element of @p data. The two
49  * allowed values in @p labels are @c 1 and @c 2.
50  *
51  * The data held in @p gramMatrix should correspond to the Gram matrix of @p
52  * data.
53  */
55  const GMatrix& gramMatrix,
56  const std::vector<int>& labels);
57  /**
58  * @brief Imports input from @p data and @p labels, and computes the Gram
59  * matrix of the data.
60  *
61  * The two structures @p data and @p labels should contain the same number
62  * of elements. The @f$ i^{\mathrm{th}} @f$ element of @p labels indicates
63  * the class of the @f$ i^{\mathrm{th}} @f$ element of @p data. The two
64  * allowed values in @p labels are @c 1 and @c 2.
65  *
66  * This method also resets all the other member variables of the class.
67  */
68  void setData(const KernelCollection &data,
69  const std::vector<int>& labels);
70 
71  /**
72  * @brief Computes KLR weights.
73  *
74  * The implementation follows <a
75  * href="http://dx.doi.org/10.1016/j.sigpro.2009.06.001">Semi-supervised
76  * speaker identification under covariate shift</a>.
77  */
78  void train(const double delta = 0.0001, const unsigned itrNewton = 5);
79 
80  /**
81  * @brief Returns @c true if the classifier has been trained.
82  */
83  bool isTrained()
84  {
85  return static_cast<bool>(vklr_);
86  };
87 
88  /**
89  * @brief Returns a pair of values which indicate the probability of classes
90  * @c 1 and @c 2.
91  */
92  Vector2 test(const kernel::base &t) const;
93  /**
94  * @brief Returns the probabilities of classes @c 1 and @c 2 for all data
95  * points in @p testSet.
96  *
97  * This method returns a @f$ 2 \times n @f$ matrix, where @f$ n @f$ is the
98  * number of elements in @p testSet.
99  */
100  GMatrix test(const KernelCollection &testSet) const;
101 
102  /**
103  * @brief Returns KLR weights.
104  *
105  * The parameter @p delta is the regularization constant.
106  */
107  const GMatrix& vklr() const
108  {
109  NUKLEI_TRACE_BEGIN();
110  NUKLEI_ASSERT(vklr_);
111  return *vklr_;
112  NUKLEI_TRACE_END();
113  }
114  private:
115  void computeGramMatrix();
116 
117  KernelCollection trainSet_;
118  GMatrix gramMatrix_;
119  std::vector<int> labels_;
120  boost::optional<GMatrix> vklr_;
121  };
122 }
123 
124 
125 #endif
Public namespace.
Definition: Color.cpp:9
bool isTrained()
Returns true if the classifier has been trained.
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.
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
Polymorphic kernel class.
Definition: Kernel.h:45
Vector2 test(const kernel::base &t) const
Returns a pair of values which indicate the probability of classes 1 and 2.
Implements kernel logistic regression.
© 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.