1 | #ifndef _theplu_yat_classifier_kernel_lookup_ |
---|
2 | #define _theplu_yat_classifier_kernel_lookup_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2010, 2012, 2020 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 3 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "Kernel.h" |
---|
29 | #include "yat/utility/Container2DIterator.h" |
---|
30 | #include "yat/utility/Index.h" |
---|
31 | #include "yat/utility/iterator_traits.h" |
---|
32 | #include "yat/utility/StrideIterator.h" |
---|
33 | |
---|
34 | #include <memory> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace classifier { |
---|
39 | |
---|
40 | class KernelFunction; |
---|
41 | class MatrixLookup; |
---|
42 | class MatrixLookupWeighted; |
---|
43 | |
---|
44 | /// |
---|
45 | /// @brief Lookup into Kernel |
---|
46 | /// |
---|
47 | /// This is the KernelLookup class to be used together with kernel |
---|
48 | /// methods such as Support Vector Machines (SVM). The class does |
---|
49 | /// not contain any data or values, but rather is a lookup into a |
---|
50 | /// Kernel object. Each row and each column corresponds to a row and |
---|
51 | /// a column in the Kernel, respectively. This design allows for fast |
---|
52 | /// creation of sub-kernels, which is a common operation in most |
---|
53 | /// traning/validation procedures. |
---|
54 | /// |
---|
55 | /// A KernelLookup can be created directly from a Kernel or from |
---|
56 | /// another KernelLookup. In the latter case, the resulting |
---|
57 | /// KernelLookup is looking directly into the underlying Kernel to |
---|
58 | /// avoid multiple lookups. |
---|
59 | /// |
---|
60 | /// There is a possibility to set the KernelLookup as owner of the |
---|
61 | /// underlying kernel. This implies that underlying kernel is deleted |
---|
62 | /// in destructor of MatrixLookup, but only if there is no other |
---|
63 | /// owner of the underlying kernel. A reference counter is used to |
---|
64 | /// keep track of number of owners. Ownership is copied in copy |
---|
65 | /// constructors and assignments. |
---|
66 | /// |
---|
67 | class KernelLookup |
---|
68 | { |
---|
69 | |
---|
70 | public: |
---|
71 | /** |
---|
72 | value_type is double |
---|
73 | |
---|
74 | \since New in yat 0.5 |
---|
75 | */ |
---|
76 | typedef double value_type; |
---|
77 | |
---|
78 | /** |
---|
79 | const_reference type is const double |
---|
80 | |
---|
81 | \since New in yat 0.5 |
---|
82 | */ |
---|
83 | typedef const double const_reference; |
---|
84 | |
---|
85 | /// 'Read Only' iterator |
---|
86 | typedef utility::StrideIterator< |
---|
87 | utility::Container2DIterator<const KernelLookup, const double, |
---|
88 | const_reference> > |
---|
89 | const_iterator; |
---|
90 | |
---|
91 | /** |
---|
92 | 'Read only' iterator intended to iterate over a column |
---|
93 | */ |
---|
94 | typedef const_iterator const_column_iterator; |
---|
95 | |
---|
96 | /** |
---|
97 | 'Read only' iterator intended to iterate over a row |
---|
98 | */ |
---|
99 | typedef const_iterator const_row_iterator; |
---|
100 | |
---|
101 | /// |
---|
102 | /// @brief Constructor a Lookup into a Kernel |
---|
103 | /// |
---|
104 | /// Constructs a KernelLookup corresponding to the Kernel @a |
---|
105 | /// kernel. By default @a owner is set to false, which means |
---|
106 | /// KernelLookup does not own the underlying Kernel. |
---|
107 | /// |
---|
108 | /// @note If underlying Kernel goes out of scope or is deleted, the |
---|
109 | /// KernelLookup becomes invalid and the result of further use is |
---|
110 | /// undefined. |
---|
111 | /// |
---|
112 | /// @note Do not construct two KernelLookups from the same @a |
---|
113 | /// kernel with @a owner set to true because that will cause |
---|
114 | /// multiple deletion of @a kernel. |
---|
115 | /// |
---|
116 | KernelLookup(const Kernel& kernel, const bool owner=false); |
---|
117 | |
---|
118 | /// |
---|
119 | /// @brief Constructing a Lookup into a subKernel |
---|
120 | /// |
---|
121 | /// Creating a Lookup into parts of the Kernel. In the created |
---|
122 | /// Lookup the element in the \f$ i \f$ th row in the \f$ j \f$ th |
---|
123 | /// column is identical to the element in row row[i] and columns |
---|
124 | /// column[j] in the underlying @a kernel. If @a owner is set to |
---|
125 | /// true yhe underlying @a kernel is destroyed in the destructor. |
---|
126 | /// |
---|
127 | /// @note If @a kernel goes out of scope or is deleted, the |
---|
128 | /// returned pointer becomes invalid and the result of further use is |
---|
129 | /// undefined. |
---|
130 | /// |
---|
131 | /// @note For training usage row index shall always be equal to |
---|
132 | /// column index. |
---|
133 | /// |
---|
134 | KernelLookup(const Kernel& kernel, const utility::Index& row, |
---|
135 | const utility::Index& column, const bool owner=false); |
---|
136 | |
---|
137 | /// |
---|
138 | /// @brief Copy constructor. |
---|
139 | /// |
---|
140 | /// A Lookup is created looking into the |
---|
141 | /// same underlying Kernel as @a kl is looking into. |
---|
142 | /// |
---|
143 | /// If \a kl is owner of underlying data, constructed |
---|
144 | /// KernelLookup will also be set as owner of underlying data. |
---|
145 | /// |
---|
146 | KernelLookup(const KernelLookup& kl); |
---|
147 | |
---|
148 | |
---|
149 | /// |
---|
150 | /// @brief Contructing a sub-KernelLookup. |
---|
151 | /// |
---|
152 | /// Contructor building a sub-KernelLookup from a KernelLookup |
---|
153 | /// defined by row index vector and column index vector. In the |
---|
154 | /// created Lookup the element in the \f$ i \f$ th row in the |
---|
155 | /// \f$ j \f$ th column is identical to the element in row row[i] and |
---|
156 | /// columns column[j] in the copied @a kl. The resulting |
---|
157 | /// KernelLookup is independent of the old KernelLookup, but is |
---|
158 | /// undefined in case underlying Kernel is destroyed. |
---|
159 | /// |
---|
160 | /// If \a kl is owner of underlying data, constructed |
---|
161 | /// KernelLookup will also be set as owner of underlying data. |
---|
162 | /// |
---|
163 | /// @note For training usage row index shall always be equal to |
---|
164 | /// column index. |
---|
165 | /// |
---|
166 | KernelLookup(const KernelLookup& kl, const utility::Index& row, |
---|
167 | const utility::Index& column); |
---|
168 | |
---|
169 | /// |
---|
170 | /// Constructor taking the column (default) or row index as |
---|
171 | /// input. If @a row is false the created KernelLookup will have |
---|
172 | /// equally many rows as @a kernel. |
---|
173 | /// |
---|
174 | /// If \a kl is owner of underlying data, constructed |
---|
175 | /// KernelLookup will also be set as owner of underlying data. |
---|
176 | /// |
---|
177 | /// @note If underlying kernel goes out of scope or is deleted, the |
---|
178 | /// KernelLookup becomes invalid and the result of further use is |
---|
179 | /// undefined. |
---|
180 | /// |
---|
181 | KernelLookup(const KernelLookup& kl, const utility::Index&, |
---|
182 | const bool row=false); |
---|
183 | |
---|
184 | /// |
---|
185 | /// @brief Destructor |
---|
186 | /// |
---|
187 | /// Deletes underlying Kernel if KernelLookup owns it and there is |
---|
188 | /// no other owner. |
---|
189 | /// |
---|
190 | virtual ~KernelLookup(void); |
---|
191 | |
---|
192 | /** |
---|
193 | Iterator iterates along a row. When end of row is reached it |
---|
194 | jumps to beginning of next row. |
---|
195 | |
---|
196 | \return const_iterator pointing to upper-left element. |
---|
197 | */ |
---|
198 | const_iterator begin(void) const; |
---|
199 | |
---|
200 | /** |
---|
201 | Iterator iterates along a column. |
---|
202 | |
---|
203 | \return iterator pointing to first element of column \a i. |
---|
204 | */ |
---|
205 | const_column_iterator begin_column(size_t) const; |
---|
206 | |
---|
207 | /** |
---|
208 | Iterator iterates along a column. |
---|
209 | |
---|
210 | \return const_iterator pointing to first element of column \a i. |
---|
211 | */ |
---|
212 | const_row_iterator begin_row(size_t) const; |
---|
213 | |
---|
214 | /** |
---|
215 | \return number of columns |
---|
216 | */ |
---|
217 | size_t columns(void) const; |
---|
218 | |
---|
219 | /// |
---|
220 | /// Each column in returned DataLookup corresponds to the column |
---|
221 | /// in KernelLookup. |
---|
222 | /// |
---|
223 | /// \return data that KernelLookup is built upon. |
---|
224 | /// |
---|
225 | /// \throw if KernelLookup is weighted |
---|
226 | /// |
---|
227 | MatrixLookup data(void) const; |
---|
228 | |
---|
229 | /// |
---|
230 | /// Each column in returned DataLookup corresponds to the column |
---|
231 | /// in KernelLookup. |
---|
232 | /// |
---|
233 | /// \return data that KernelLookup is built upon. |
---|
234 | /// |
---|
235 | /// \throw if KernelLookup is unweighted |
---|
236 | /// |
---|
237 | MatrixLookupWeighted data_weighted(void) const; |
---|
238 | |
---|
239 | /** |
---|
240 | Function to calculate a new Kernel element using the underlying |
---|
241 | KernelFunction. The value is calculated between @a vec and the |
---|
242 | data vector of the \a i th sample, in other words, the |
---|
243 | sample corresponding to the \a i th row. |
---|
244 | */ |
---|
245 | double element(const DataLookup1D& vec, size_t i) const; |
---|
246 | |
---|
247 | /** |
---|
248 | Function to calculate a new Kernel element using the underlying |
---|
249 | KernelFunction. The value is calulated between @a vec and the |
---|
250 | data vector of the \f$ i \f$ th sample, in other words, the |
---|
251 | sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th |
---|
252 | column. In case KernelLookup is a sub-Kernel and not symmetric, |
---|
253 | the kernel value is calculated between @a vec and the data |
---|
254 | vector corresponding to \f$ i \f$ th row. |
---|
255 | */ |
---|
256 | double element(const DataLookupWeighted1D& vec, size_t i) const; |
---|
257 | |
---|
258 | /** |
---|
259 | \return const_iterator pointing to end of matrix |
---|
260 | */ |
---|
261 | const_iterator end(void) const; |
---|
262 | |
---|
263 | /** |
---|
264 | \return const_iterator pointing to end of column \a i |
---|
265 | */ |
---|
266 | const_column_iterator end_column(size_t) const; |
---|
267 | |
---|
268 | /** |
---|
269 | \return const_iterator pointing to end of row \a i |
---|
270 | */ |
---|
271 | const_row_iterator end_row(size_t) const; |
---|
272 | |
---|
273 | /** |
---|
274 | \return number of rows |
---|
275 | */ |
---|
276 | size_t rows(void) const; |
---|
277 | |
---|
278 | /** |
---|
279 | Each element in returned KernelLookup is calculated using only |
---|
280 | selected features (defined by @a index). Each element |
---|
281 | corresponds to the same pair of samples as in the original |
---|
282 | KernelLookup. |
---|
283 | */ |
---|
284 | KernelLookup selected(const utility::Index& index) const; |
---|
285 | |
---|
286 | /** |
---|
287 | This function is useful when predicting on an independent data |
---|
288 | set using a kernel-based classifier. In returned KernelLookup |
---|
289 | column \f$ i \f$ corresponds to column \f$ i \f$ in @a |
---|
290 | data. Row \f$ i \f$ in returned KernelLookup corresponds to |
---|
291 | same sample as row \f$ i \f$ in @a this. In other words, this |
---|
292 | function returns a KernelLookup containing the kernel elements |
---|
293 | between the passed @a data and the internal underlying data @a |
---|
294 | this was built from. |
---|
295 | */ |
---|
296 | KernelLookup test_kernel(const MatrixLookup& data) const; |
---|
297 | |
---|
298 | /** |
---|
299 | This function is useful when predicting on an independent data |
---|
300 | set using a kernel-based classifier. In returned KernelLookup |
---|
301 | column \f$ i \f$ corresponds to column \f$ i \f$ in @a |
---|
302 | data. Row \f$ i \f$ in returned KernelLookup corresponds to |
---|
303 | same sample as row \f$ i \f$ in @a this. In other words, this |
---|
304 | function returns a KernelLookup containing the kernel elements |
---|
305 | between the passed @a data and the internal underlying data @a |
---|
306 | this was built from. |
---|
307 | */ |
---|
308 | KernelLookup test_kernel(const MatrixLookupWeighted& data) const; |
---|
309 | |
---|
310 | /** |
---|
311 | \return true if underlying Kernel is weighted |
---|
312 | */ |
---|
313 | bool weighted(void) const; |
---|
314 | |
---|
315 | /** |
---|
316 | \return element at position (\a row, \a column) in the Kernel |
---|
317 | matrix |
---|
318 | */ |
---|
319 | const_reference operator()(size_t row, size_t column) const; |
---|
320 | |
---|
321 | private: |
---|
322 | const KernelLookup& operator=(const KernelLookup&); |
---|
323 | bool validate(const utility::Index&) const; |
---|
324 | |
---|
325 | utility::Index column_index_; |
---|
326 | std::shared_ptr<const Kernel> kernel_; |
---|
327 | utility::Index row_index_; |
---|
328 | }; // class KernelLookup |
---|
329 | |
---|
330 | }}} // of namespace classifier, yat, and theplu |
---|
331 | |
---|
332 | #endif |
---|