680 |
11 Oct 06 |
jari |
1 |
#ifndef _theplu_yat_classifier_kernel_ |
680 |
11 Oct 06 |
jari |
2 |
#define _theplu_yat_classifier_kernel_ |
330 |
01 Jun 05 |
peter |
3 |
|
616 |
31 Aug 06 |
jari |
// $Id$ |
616 |
31 Aug 06 |
jari |
5 |
|
675 |
10 Oct 06 |
jari |
6 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér |
513 |
18 Feb 06 |
peter |
10 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
12 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
17 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
22 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
25 |
*/ |
675 |
10 Oct 06 |
jari |
26 |
|
680 |
11 Oct 06 |
jari |
27 |
#include "KernelFunction.h" |
675 |
10 Oct 06 |
jari |
28 |
|
1271 |
09 Apr 08 |
peter |
29 |
#include <cstddef> |
545 |
06 Mar 06 |
peter |
30 |
#include <vector> |
330 |
01 Jun 05 |
peter |
31 |
|
330 |
01 Jun 05 |
peter |
32 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
33 |
namespace yat { |
450 |
15 Dec 05 |
peter |
34 |
namespace classifier { |
330 |
01 Jun 05 |
peter |
35 |
|
628 |
05 Sep 06 |
peter |
36 |
class MatrixLookup; |
747 |
11 Feb 07 |
peter |
37 |
class MatrixLookupWeighted; |
628 |
05 Sep 06 |
peter |
38 |
|
749 |
11 Feb 07 |
peter |
39 |
/// |
4200 |
19 Aug 22 |
peter |
/// @brief Interface Class for Kernels. |
330 |
01 Jun 05 |
peter |
41 |
/// |
597 |
28 Aug 06 |
markus |
/// Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$ |
749 |
11 Feb 07 |
peter |
/// is number of samples. Each element in the Kernel corresponds to |
592 |
24 Aug 06 |
peter |
/// the scalar product of the corresponding pair of samples. At the |
592 |
24 Aug 06 |
peter |
/// time being there are two kinds of kernels. Kernel_SEV that is |
592 |
24 Aug 06 |
peter |
/// optimized to be fast and Kernel_MEV that is preferable when |
592 |
24 Aug 06 |
peter |
/// dealing with many samples and memory might be a |
749 |
11 Feb 07 |
peter |
/// bottleneck. A |
592 |
24 Aug 06 |
peter |
/// KernelFunction defines what kind of scalar product the Kernel |
592 |
24 Aug 06 |
peter |
/// represents, e.g. a Polynomial Kernel of degree 1 means we are |
592 |
24 Aug 06 |
peter |
/// dealing with the ordinary linear scalar product. |
592 |
24 Aug 06 |
peter |
52 |
/// |
749 |
11 Feb 07 |
peter |
/// @note If the KernelFunction is destroyed, the Kernel is no |
749 |
11 Feb 07 |
peter |
/// longer defined. |
749 |
11 Feb 07 |
peter |
55 |
/// |
749 |
11 Feb 07 |
peter |
56 |
class Kernel |
749 |
11 Feb 07 |
peter |
57 |
{ |
333 |
02 Jun 05 |
jari |
58 |
|
749 |
11 Feb 07 |
peter |
59 |
public: |
749 |
11 Feb 07 |
peter |
60 |
|
330 |
01 Jun 05 |
peter |
61 |
/// |
749 |
11 Feb 07 |
peter |
/// Constructor taking the @a data matrix and KernelFunction as |
749 |
11 Feb 07 |
peter |
/// input. Each column in the data matrix corresponds to one |
749 |
11 Feb 07 |
peter |
/// sample and the Kernel matrix is built applying the |
749 |
11 Feb 07 |
peter |
/// KernelFunction on each pair of columns in the data matrix. |
749 |
11 Feb 07 |
peter |
/// If @a own is set to true, Kernel is owner of underlying data. |
330 |
01 Jun 05 |
peter |
67 |
/// |
749 |
11 Feb 07 |
peter |
/// @note Can not handle NaNs. To deal with missing values use |
749 |
11 Feb 07 |
peter |
/// constructor taking MatrixLookupWeighted. |
513 |
18 Feb 06 |
peter |
70 |
/// |
4200 |
19 Aug 22 |
peter |
71 |
Kernel(const MatrixLookup& data, const KernelFunction& kf, |
4200 |
19 Aug 22 |
peter |
72 |
const bool own=false); |
545 |
06 Mar 06 |
peter |
73 |
|
566 |
16 Mar 06 |
peter |
74 |
/// |
749 |
11 Feb 07 |
peter |
/// Constructor taking the @a data matrix (with weights) and |
749 |
11 Feb 07 |
peter |
/// KernelFunction as |
749 |
11 Feb 07 |
peter |
/// input. Each column in the data matrix corresponds to one |
749 |
11 Feb 07 |
peter |
/// sample and the Kernel matrix is built applying the |
749 |
11 Feb 07 |
peter |
/// KernelFunction on each pair of columns in the data matrix. |
749 |
11 Feb 07 |
peter |
/// If @a own is set to true, Kernel is owner of underlying data. |
566 |
16 Mar 06 |
peter |
81 |
/// |
4200 |
19 Aug 22 |
peter |
82 |
Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, |
4200 |
19 Aug 22 |
peter |
83 |
const bool own=false); |
628 |
05 Sep 06 |
peter |
84 |
|
545 |
06 Mar 06 |
peter |
85 |
/// |
628 |
05 Sep 06 |
peter |
/// The new kernel is created using selected features @a |
4200 |
19 Aug 22 |
peter |
/// index. Kernel will own its underlying data |
628 |
05 Sep 06 |
peter |
88 |
/// |
545 |
06 Mar 06 |
peter |
89 |
Kernel(const Kernel& kernel, const std::vector<size_t>& index); |
545 |
06 Mar 06 |
peter |
90 |
|
749 |
11 Feb 07 |
peter |
91 |
/// |
749 |
11 Feb 07 |
peter |
/// @brief Destructor |
749 |
11 Feb 07 |
peter |
93 |
/// |
749 |
11 Feb 07 |
peter |
/// If Kernel is owner of underlying data and Kernel is the last |
749 |
11 Feb 07 |
peter |
/// owner, underlying data is deleted. |
749 |
11 Feb 07 |
peter |
96 |
/// |
545 |
06 Mar 06 |
peter |
97 |
virtual ~Kernel(void); |
330 |
01 Jun 05 |
peter |
98 |
|
330 |
01 Jun 05 |
peter |
99 |
/// |
330 |
01 Jun 05 |
peter |
/// @return element at position (\a row, \a column) of the Kernel |
330 |
01 Jun 05 |
peter |
/// matrix |
749 |
11 Feb 07 |
peter |
102 |
/// |
527 |
01 Mar 06 |
peter |
103 |
virtual double operator()(const size_t row, const size_t column) const=0; |
330 |
01 Jun 05 |
peter |
104 |
|
330 |
01 Jun 05 |
peter |
105 |
/// |
628 |
05 Sep 06 |
peter |
/// @return const reference to the underlying data. |
592 |
24 Aug 06 |
peter |
107 |
/// |
4200 |
19 Aug 22 |
peter |
/// \throw if data is weighted |
1165 |
26 Feb 08 |
peter |
109 |
/// |
1165 |
26 Feb 08 |
peter |
110 |
const MatrixLookup& data(void) const; |
545 |
06 Mar 06 |
peter |
111 |
|
536 |
03 Mar 06 |
peter |
112 |
/// |
1165 |
26 Feb 08 |
peter |
/// @return const reference to the underlying data. |
1165 |
26 Feb 08 |
peter |
114 |
/// |
4200 |
19 Aug 22 |
peter |
/// \throw if data is unweighted |
1165 |
26 Feb 08 |
peter |
116 |
/// |
1165 |
26 Feb 08 |
peter |
117 |
const MatrixLookupWeighted& data_weighted(void) const; |
1165 |
26 Feb 08 |
peter |
118 |
|
1165 |
26 Feb 08 |
peter |
119 |
/// |
592 |
24 Aug 06 |
peter |
/// Calculates the scalar product (using the KernelFunction) |
597 |
28 Aug 06 |
markus |
/// between vector @a vec and the \f$ i \f$ th column in the data |
592 |
24 Aug 06 |
peter |
/// matrix. |
4200 |
19 Aug 22 |
peter |
123 |
/// |
628 |
05 Sep 06 |
peter |
124 |
double element(const DataLookup1D& vec, const size_t i) const; |
592 |
24 Aug 06 |
peter |
125 |
|
592 |
24 Aug 06 |
peter |
126 |
/// |
592 |
24 Aug 06 |
peter |
/// Calculates the weighted scalar product (using the |
597 |
28 Aug 06 |
markus |
/// KernelFunction) between vector @a vec and the \f$ i \f$ th column |
592 |
24 Aug 06 |
peter |
/// in the data matrix. Using a weight vector with all elements |
592 |
24 Aug 06 |
peter |
/// equal to unity yields same result as the non-weighted version |
592 |
24 Aug 06 |
peter |
/// above. |
592 |
24 Aug 06 |
peter |
132 |
/// |
628 |
05 Sep 06 |
peter |
133 |
double element(const DataLookupWeighted1D& vec, const size_t i) const; |
523 |
23 Feb 06 |
peter |
134 |
|
545 |
06 Mar 06 |
peter |
135 |
/// |
658 |
25 Sep 06 |
peter |
/// An interface for making new classifier objects. This function |
658 |
25 Sep 06 |
peter |
/// allows for specification at run-time of which kernel to |
658 |
25 Sep 06 |
peter |
/// instatiate (see 'Prototype' in Design Patterns). |
658 |
25 Sep 06 |
peter |
139 |
/// |
1125 |
22 Feb 08 |
peter |
/// @note Returns a dynamically allocated Kernel, which has |
658 |
25 Sep 06 |
peter |
/// to be deleted by the caller to avoid memory leaks. |
658 |
25 Sep 06 |
peter |
142 |
/// |
658 |
25 Sep 06 |
peter |
143 |
virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0; |
658 |
25 Sep 06 |
peter |
144 |
|
658 |
25 Sep 06 |
peter |
145 |
|
658 |
25 Sep 06 |
peter |
146 |
/// |
658 |
25 Sep 06 |
peter |
/// An interface for making new classifier objects. This function |
658 |
25 Sep 06 |
peter |
/// allows for specification at run-time of which kernel to |
658 |
25 Sep 06 |
peter |
/// instatiate (see 'Prototype' in Design Patterns). |
658 |
25 Sep 06 |
peter |
150 |
/// |
1125 |
22 Feb 08 |
peter |
/// @note Returns a dynamically allocated Kernel, which has |
658 |
25 Sep 06 |
peter |
/// to be deleted by the caller to avoid memory leaks. |
658 |
25 Sep 06 |
peter |
153 |
/// |
4200 |
19 Aug 22 |
peter |
154 |
virtual const Kernel* make_kernel(const MatrixLookupWeighted&, |
658 |
25 Sep 06 |
peter |
155 |
const bool own=false) const=0; |
658 |
25 Sep 06 |
peter |
156 |
|
658 |
25 Sep 06 |
peter |
157 |
|
720 |
26 Dec 06 |
jari |
158 |
/** |
4200 |
19 Aug 22 |
peter |
\brief number of samples |
720 |
26 Dec 06 |
jari |
160 |
*/ |
720 |
26 Dec 06 |
jari |
161 |
size_t size(void) const; |
720 |
26 Dec 06 |
jari |
162 |
|
545 |
06 Mar 06 |
peter |
163 |
/// |
545 |
06 Mar 06 |
peter |
/// @return true if kernel is calculated using weights |
545 |
06 Mar 06 |
peter |
165 |
/// |
720 |
26 Dec 06 |
jari |
166 |
bool weighted(void) const; |
545 |
06 Mar 06 |
peter |
167 |
|
749 |
11 Feb 07 |
peter |
168 |
protected: |
659 |
26 Sep 06 |
peter |
/// underlying data |
1163 |
26 Feb 08 |
peter |
170 |
const MatrixLookup* ml_; |
648 |
14 Sep 06 |
peter |
/// same as data_ if weifghted otherwise a NULL pointer |
1163 |
26 Feb 08 |
peter |
172 |
const MatrixLookupWeighted* mlw_; |
648 |
14 Sep 06 |
peter |
/// type of Kernel Function e.g. Gaussian (aka RBF) |
512 |
18 Feb 06 |
peter |
174 |
const KernelFunction* kf_; |
330 |
01 Jun 05 |
peter |
175 |
|
659 |
26 Sep 06 |
peter |
176 |
/// |
749 |
11 Feb 07 |
peter |
/// pointer telling how many owners to underlying data |
659 |
26 Sep 06 |
peter |
/// (data_). NULL if this is not an owner. |
659 |
26 Sep 06 |
peter |
179 |
/// |
1271 |
09 Apr 08 |
peter |
180 |
unsigned int* ref_count_; |
659 |
26 Sep 06 |
peter |
181 |
|
659 |
26 Sep 06 |
peter |
182 |
/// |
749 |
11 Feb 07 |
peter |
/// pointer telling how many owners to underlying weights |
659 |
26 Sep 06 |
peter |
/// (data_w_). NULL if this is not an owner. |
659 |
26 Sep 06 |
peter |
185 |
/// |
1271 |
09 Apr 08 |
peter |
186 |
unsigned int* ref_count_w_; |
659 |
26 Sep 06 |
peter |
187 |
|
513 |
18 Feb 06 |
peter |
188 |
private: |
513 |
18 Feb 06 |
peter |
189 |
/// |
513 |
18 Feb 06 |
peter |
/// Copy constructor (not implemented) |
513 |
18 Feb 06 |
peter |
191 |
/// |
513 |
18 Feb 06 |
peter |
192 |
Kernel(const Kernel&); |
513 |
18 Feb 06 |
peter |
193 |
|
555 |
08 Mar 06 |
peter |
194 |
const Kernel& operator=(const Kernel&); |
513 |
18 Feb 06 |
peter |
195 |
|
749 |
11 Feb 07 |
peter |
196 |
}; // class Kernel |
330 |
01 Jun 05 |
peter |
197 |
|
680 |
11 Oct 06 |
jari |
198 |
}}} // of namespace classifier, yat, and theplu |
330 |
01 Jun 05 |
peter |
199 |
|
330 |
01 Jun 05 |
peter |
200 |
#endif |