1 | #ifndef _theplu_yat_classifier_kernel_ |
---|
2 | #define _theplu_yat_classifier_kernel_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
9 | Copyright (C) 2007 Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 2 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "KernelFunction.h" |
---|
30 | |
---|
31 | #include <cctype> |
---|
32 | #include <vector> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace classifier { |
---|
37 | |
---|
38 | class MatrixLookup; |
---|
39 | class MatrixLookupWeighted; |
---|
40 | |
---|
41 | /// |
---|
42 | /// @brief Interface Class for Kernels. |
---|
43 | /// |
---|
44 | /// Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$ |
---|
45 | /// is number of samples. Each element in the Kernel corresponds to |
---|
46 | /// the scalar product of the corresponding pair of samples. At the |
---|
47 | /// time being there are two kinds of kernels. Kernel_SEV that is |
---|
48 | /// optimized to be fast and Kernel_MEV that is preferable when |
---|
49 | /// dealing with many samples and memory might be a |
---|
50 | /// bottleneck. A |
---|
51 | /// KernelFunction defines what kind of scalar product the Kernel |
---|
52 | /// represents, e.g. a Polynomial Kernel of degree 1 means we are |
---|
53 | /// dealing with the ordinary linear scalar product. |
---|
54 | /// |
---|
55 | /// @note If the KernelFunction is destroyed, the Kernel is no |
---|
56 | /// longer defined. |
---|
57 | /// |
---|
58 | class Kernel |
---|
59 | { |
---|
60 | |
---|
61 | public: |
---|
62 | |
---|
63 | /// |
---|
64 | /// Constructor taking the @a data matrix and KernelFunction as |
---|
65 | /// input. Each column in the data matrix corresponds to one |
---|
66 | /// sample and the Kernel matrix is built applying the |
---|
67 | /// KernelFunction on each pair of columns in the data matrix. |
---|
68 | /// If @a own is set to true, Kernel is owner of underlying data. |
---|
69 | /// |
---|
70 | /// @note Can not handle NaNs. To deal with missing values use |
---|
71 | /// constructor taking MatrixLookupWeighted. |
---|
72 | /// |
---|
73 | Kernel(const MatrixLookup& data, const KernelFunction& kf, |
---|
74 | const bool own=false); |
---|
75 | |
---|
76 | /// |
---|
77 | /// Constructor taking the @a data matrix (with weights) and |
---|
78 | /// KernelFunction as |
---|
79 | /// input. Each column in the data matrix corresponds to one |
---|
80 | /// sample and the Kernel matrix is built applying the |
---|
81 | /// KernelFunction on each pair of columns in the data matrix. |
---|
82 | /// If @a own is set to true, Kernel is owner of underlying data. |
---|
83 | /// |
---|
84 | Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, |
---|
85 | const bool own=false); |
---|
86 | |
---|
87 | /// |
---|
88 | /// The new kernel is created using selected features @a |
---|
89 | /// index. Kernel will own its underlying data |
---|
90 | /// |
---|
91 | Kernel(const Kernel& kernel, const std::vector<size_t>& index); |
---|
92 | |
---|
93 | /// |
---|
94 | /// @brief Destructor |
---|
95 | /// |
---|
96 | /// If Kernel is owner of underlying data and Kernel is the last |
---|
97 | /// owner, underlying data is deleted. |
---|
98 | /// |
---|
99 | virtual ~Kernel(void); |
---|
100 | |
---|
101 | /// |
---|
102 | /// @return element at position (\a row, \a column) of the Kernel |
---|
103 | /// matrix |
---|
104 | /// |
---|
105 | virtual double operator()(const size_t row, const size_t column) const=0; |
---|
106 | |
---|
107 | /// |
---|
108 | /// @return const reference to the underlying data. |
---|
109 | /// |
---|
110 | /// \throw if data is weighted |
---|
111 | /// |
---|
112 | const MatrixLookup& data(void) const; |
---|
113 | |
---|
114 | /// |
---|
115 | /// @return const reference to the underlying data. |
---|
116 | /// |
---|
117 | /// \throw if data is unweighted |
---|
118 | /// |
---|
119 | const MatrixLookupWeighted& data_weighted(void) const; |
---|
120 | |
---|
121 | /// |
---|
122 | /// Calculates the scalar product (using the KernelFunction) |
---|
123 | /// between vector @a vec and the \f$ i \f$ th column in the data |
---|
124 | /// matrix. |
---|
125 | /// |
---|
126 | double element(const DataLookup1D& vec, const size_t i) const; |
---|
127 | |
---|
128 | /// |
---|
129 | /// Calculates the weighted scalar product (using the |
---|
130 | /// KernelFunction) between vector @a vec and the \f$ i \f$ th column |
---|
131 | /// in the data matrix. Using a weight vector with all elements |
---|
132 | /// equal to unity yields same result as the non-weighted version |
---|
133 | /// above. |
---|
134 | /// |
---|
135 | double element(const DataLookupWeighted1D& vec, const size_t i) const; |
---|
136 | |
---|
137 | /// |
---|
138 | /// An interface for making new classifier objects. This function |
---|
139 | /// allows for specification at run-time of which kernel to |
---|
140 | /// instatiate (see 'Prototype' in Design Patterns). |
---|
141 | /// |
---|
142 | /// @note Returns a dynamically allocated Kernel, which has |
---|
143 | /// to be deleted by the caller to avoid memory leaks. |
---|
144 | /// |
---|
145 | virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0; |
---|
146 | |
---|
147 | |
---|
148 | /// |
---|
149 | /// An interface for making new classifier objects. This function |
---|
150 | /// allows for specification at run-time of which kernel to |
---|
151 | /// instatiate (see 'Prototype' in Design Patterns). |
---|
152 | /// |
---|
153 | /// @note Returns a dynamically allocated Kernel, which has |
---|
154 | /// to be deleted by the caller to avoid memory leaks. |
---|
155 | /// |
---|
156 | virtual const Kernel* make_kernel(const MatrixLookupWeighted&, |
---|
157 | const bool own=false) const=0; |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | \brief number of samples |
---|
162 | */ |
---|
163 | size_t size(void) const; |
---|
164 | |
---|
165 | /// |
---|
166 | /// @return true if kernel is calculated using weights |
---|
167 | /// |
---|
168 | bool weighted(void) const; |
---|
169 | |
---|
170 | protected: |
---|
171 | /// underlying data |
---|
172 | const MatrixLookup* ml_; |
---|
173 | /// same as data_ if weifghted otherwise a NULL pointer |
---|
174 | const MatrixLookupWeighted* mlw_; |
---|
175 | /// type of Kernel Function e.g. Gaussian (aka RBF) |
---|
176 | const KernelFunction* kf_; |
---|
177 | |
---|
178 | /// |
---|
179 | /// pointer telling how many owners to underlying data |
---|
180 | /// (data_). NULL if this is not an owner. |
---|
181 | /// |
---|
182 | u_int* ref_count_; |
---|
183 | |
---|
184 | /// |
---|
185 | /// pointer telling how many owners to underlying weights |
---|
186 | /// (data_w_). NULL if this is not an owner. |
---|
187 | /// |
---|
188 | u_int* ref_count_w_; |
---|
189 | |
---|
190 | private: |
---|
191 | /// |
---|
192 | /// Copy constructor (not implemented) |
---|
193 | /// |
---|
194 | Kernel(const Kernel&); |
---|
195 | |
---|
196 | const Kernel& operator=(const Kernel&); |
---|
197 | |
---|
198 | }; // class Kernel |
---|
199 | |
---|
200 | }}} // of namespace classifier, yat, and theplu |
---|
201 | |
---|
202 | #endif |
---|