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, Markus Ringnér, Peter Johansson |
---|
9 | Copyright (C) 2007, 2008 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 "Kernel.h" |
---|
30 | #include "yat/utility/Container2DIterator.h" |
---|
31 | #include "yat/utility/Index.h" |
---|
32 | #include "yat/utility/iterator_traits.h" |
---|
33 | #include "yat/utility/SmartPtr.h" |
---|
34 | #include "yat/utility/StrideIterator.h" |
---|
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 allow 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 an |
---|
56 | /// other 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 | /// 'Read Only' iterator |
---|
72 | typedef utility::StrideIterator< |
---|
73 | utility::Container2DIterator<const KernelLookup, const double, void, |
---|
74 | const double> > |
---|
75 | const_iterator; |
---|
76 | |
---|
77 | /** |
---|
78 | 'Read only' iterator intended to iterate over a column |
---|
79 | */ |
---|
80 | typedef const_iterator const_column_iterator; |
---|
81 | |
---|
82 | /** |
---|
83 | 'Read only' iterator intended to iterate over a row |
---|
84 | */ |
---|
85 | typedef const_iterator const_row_iterator; |
---|
86 | |
---|
87 | /// |
---|
88 | /// @brief Constructor a Lookup into a Kernel |
---|
89 | /// |
---|
90 | /// Constructs a KernelLookup corresponding to the Kernel @a |
---|
91 | /// kernel. By default @a owner is set to false, which means |
---|
92 | /// KernelLookup does not own the underlying Kernel. |
---|
93 | /// |
---|
94 | /// @note If underlying Kernel goes out of scope or is deleted, the |
---|
95 | /// KernelLookup becomes invalid and the result of further use is |
---|
96 | /// undefined. |
---|
97 | /// |
---|
98 | /// @note Do not construct two KernelLookups from the same @a |
---|
99 | /// kernel with @a owner set to true because that will cause |
---|
100 | /// multiple deletion of @a kernel. |
---|
101 | /// |
---|
102 | KernelLookup(const Kernel& kernel, const bool owner=false); |
---|
103 | |
---|
104 | /// |
---|
105 | /// @brief Constructing a Lookup into a subKernel |
---|
106 | /// |
---|
107 | /// Creating a Lookup into parts of the Kernel. In the created |
---|
108 | /// Lookup the element in the \f$ i \f$ th row in the \f$ j \f$ th |
---|
109 | /// column is identical to the element in row row[i] and columns |
---|
110 | /// column[j] in the underlying @a kernel. If @a owner is set to |
---|
111 | /// true yhe underlying @a kernel is destroyed in the destructor. |
---|
112 | /// |
---|
113 | /// @note If @a kernel goes out of scope or is deleted, the |
---|
114 | /// returned pointer becomes invalid and the result of further use is |
---|
115 | /// undefined. |
---|
116 | /// |
---|
117 | /// @note For training usage row index shall always be equal to |
---|
118 | /// column index. |
---|
119 | /// |
---|
120 | KernelLookup(const Kernel& kernel, const utility::Index& row, |
---|
121 | const utility::Index& column, const bool owner=false); |
---|
122 | |
---|
123 | /// |
---|
124 | /// @brief Copy constructor. |
---|
125 | /// |
---|
126 | /// A Lookup is created looking into the |
---|
127 | /// same underlying Kernel as @a kl is looking into. |
---|
128 | /// |
---|
129 | /// If \a kl is owner of underlying data, constructed |
---|
130 | /// KernelLookup will also be set as owner of underlying data. |
---|
131 | /// |
---|
132 | KernelLookup(const KernelLookup& kl); |
---|
133 | |
---|
134 | |
---|
135 | /// |
---|
136 | /// @brief Contructing a sub-KernelLookup. |
---|
137 | /// |
---|
138 | /// Contructor building a sub-KernelLookup from a KernelLookup |
---|
139 | /// defined by row index vector and column index vector. In the |
---|
140 | /// created Lookup the element in the \f$ i \f$ th row in the |
---|
141 | /// \f$ j \f$ th column is identical to the element in row row[i] and |
---|
142 | /// columns column[j] in the copied @a kl. The resulting |
---|
143 | /// KernelLookup is independent of the old KernelLookup, but is |
---|
144 | /// undefined in case underlying Kernel is destroyed. |
---|
145 | /// |
---|
146 | /// If \a kl is owner of underlying data, constructed |
---|
147 | /// KernelLookup will also be set as owner of underlying data. |
---|
148 | /// |
---|
149 | /// @note For training usage row index shall always be equal to |
---|
150 | /// column index. |
---|
151 | /// |
---|
152 | KernelLookup(const KernelLookup& kl, const utility::Index& row, |
---|
153 | const utility::Index& column); |
---|
154 | |
---|
155 | /// |
---|
156 | /// Constructor taking the column (default) or row index vector as |
---|
157 | /// input. If @a row is false the created KernelLookup will have |
---|
158 | /// equally many rows as @a kernel. |
---|
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 If underlying kernel goes out of scope or is deleted, the |
---|
164 | /// KernelLookup becomes invalid and the result of further use is |
---|
165 | /// undefined. |
---|
166 | /// |
---|
167 | KernelLookup(const KernelLookup& kernel, const utility::Index&, |
---|
168 | const bool row=false); |
---|
169 | |
---|
170 | /// |
---|
171 | /// @brief Destructor |
---|
172 | /// |
---|
173 | /// Deletes underlying Kernel if KernelLookup owns it and there is |
---|
174 | /// no other owner. |
---|
175 | /// |
---|
176 | virtual ~KernelLookup(void); |
---|
177 | |
---|
178 | /** |
---|
179 | Iterator iterates along a row. When end of row is reached it |
---|
180 | jumps to beginning of next row. |
---|
181 | |
---|
182 | \return const_iterator pointing to upper-left element. |
---|
183 | */ |
---|
184 | const_iterator begin(void) const; |
---|
185 | |
---|
186 | /** |
---|
187 | Iterator iterates along a column. |
---|
188 | |
---|
189 | \return iterator pointing to first element of column \a i. |
---|
190 | */ |
---|
191 | const_column_iterator begin_column(size_t) const; |
---|
192 | |
---|
193 | /** |
---|
194 | Iterator iterates along a column. |
---|
195 | |
---|
196 | \return const_iterator pointing to first element of column \a i. |
---|
197 | */ |
---|
198 | const_row_iterator begin_row(size_t) const; |
---|
199 | |
---|
200 | /** |
---|
201 | \return number of columns |
---|
202 | */ |
---|
203 | size_t columns(void) const; |
---|
204 | |
---|
205 | /// |
---|
206 | /// Each column in returned DataLookup corresponds to the column |
---|
207 | /// in KernelLookup. |
---|
208 | /// |
---|
209 | /// \return data that KernelLookup is built upon. |
---|
210 | /// |
---|
211 | /// \throw if KernelLookup is weighted |
---|
212 | /// |
---|
213 | utility::SmartPtr<const MatrixLookup> data(void) const; |
---|
214 | |
---|
215 | /// |
---|
216 | /// Each column in returned DataLookup corresponds to the column |
---|
217 | /// in KernelLookup. |
---|
218 | /// |
---|
219 | /// \return data that KernelLookup is built upon. |
---|
220 | /// |
---|
221 | /// \throw if KernelLookup is unweighted |
---|
222 | /// |
---|
223 | utility::SmartPtr<const MatrixLookupWeighted> data_weighted(void) const; |
---|
224 | |
---|
225 | /** |
---|
226 | Function to calculate a new Kernel element using the underlying |
---|
227 | KernelFunction. The value is calculated between @a vec and the |
---|
228 | data vector of the \a i th sample, in other words, the |
---|
229 | sample corresponding to the \a i th row. |
---|
230 | */ |
---|
231 | double element(const DataLookup1D& vec, size_t i) const; |
---|
232 | |
---|
233 | /** |
---|
234 | Function to calculate a new Kernel element using the underlying |
---|
235 | KernelFunction. The value is calulated between @a vec and the |
---|
236 | data vector of the \f$ i \f$ th sample, in other words, the |
---|
237 | sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th |
---|
238 | column. In case KernelLookup is a sub-Kernel and not symmetric, |
---|
239 | the kernel value is calculated between @a vec and the data |
---|
240 | vector corresponding to \f$ i \f$ th row. |
---|
241 | */ |
---|
242 | double element(const DataLookupWeighted1D& vec, size_t i) const; |
---|
243 | |
---|
244 | /** |
---|
245 | \return const_iterator pointing to end of matrix |
---|
246 | */ |
---|
247 | const_iterator end(void) const; |
---|
248 | |
---|
249 | /** |
---|
250 | \return const_iterator pointing to end of column \a i |
---|
251 | */ |
---|
252 | const_column_iterator end_column(size_t) const; |
---|
253 | |
---|
254 | /** |
---|
255 | \return const_iterator pointing to end of row \a i |
---|
256 | */ |
---|
257 | const_row_iterator end_row(size_t) const; |
---|
258 | |
---|
259 | /** |
---|
260 | \return number of rows |
---|
261 | */ |
---|
262 | size_t rows(void) const; |
---|
263 | |
---|
264 | /** |
---|
265 | Each element in returned KernelLookup is calculated using only |
---|
266 | selected features (defined by @a index). Each element |
---|
267 | corresponds to the same pair of samples as in the original |
---|
268 | KernelLookup. |
---|
269 | |
---|
270 | \note Returns a dynamically allocated KernelLookup, which has |
---|
271 | to be deleted by the caller to avoid memory leaks. |
---|
272 | */ |
---|
273 | const KernelLookup* selected(const utility::Index& index) const; |
---|
274 | |
---|
275 | /** |
---|
276 | This function is useful when predicting on an independent data |
---|
277 | set using a kernel-based classifier. In returned KernelLookup |
---|
278 | column \f$ i \f$ corresponds to column \f$ i \f$ in @a |
---|
279 | data. Row \f$ i \f$ in returned KernelLookup corresponds to |
---|
280 | same sample as row \f$ i \f$ in @a this. In other words, this |
---|
281 | function returns a KernelLookup containing the kernel elements |
---|
282 | between the passed @a data and the internal underlying data @a |
---|
283 | this was built from. |
---|
284 | |
---|
285 | @note Returns a dynamically allocated KernelLookup, which has |
---|
286 | to be deleted by the caller to avoid memory leaks. |
---|
287 | */ |
---|
288 | const KernelLookup* test_kernel(const MatrixLookup& data) const; |
---|
289 | |
---|
290 | /** |
---|
291 | This function is useful when predicting on an independent data |
---|
292 | set using a kernel-based classifier. In returned KernelLookup |
---|
293 | column \f$ i \f$ corresponds to column \f$ i \f$ in @a |
---|
294 | data. Row \f$ i \f$ in returned KernelLookup corresponds to |
---|
295 | same sample as row \f$ i \f$ in @a this. In other words, this |
---|
296 | function returns a KernelLookup containing the kernel elements |
---|
297 | between the passed @a data and the internal underlying data @a |
---|
298 | this was built from. |
---|
299 | |
---|
300 | @note Returns a dynamically allocated DataLookup2D, which has |
---|
301 | to be deleted by the caller to avoid memory leaks. |
---|
302 | */ |
---|
303 | const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const; |
---|
304 | |
---|
305 | /** |
---|
306 | \brief Creates a sub-Kernel identical to the one created using |
---|
307 | KernelLookup(*this, train, train). |
---|
308 | |
---|
309 | \return pointer to dynamically allocated sub-Lookup of the |
---|
310 | KernelLookup |
---|
311 | |
---|
312 | \note Returns a dynamically allocated DataLookup2D, which has |
---|
313 | to be deleted by the caller to avoid memory leaks. |
---|
314 | */ |
---|
315 | const KernelLookup* training_data(const utility::Index& train) const; |
---|
316 | |
---|
317 | /** |
---|
318 | In returned kernel each row corresponds to a training sample |
---|
319 | and each column corresponds to a validation sample. The created |
---|
320 | sub-KernelLookup is equivalent to using KernelLooup(*this, |
---|
321 | train, validation). |
---|
322 | |
---|
323 | \return sub-Lookup of the DataLookup2D |
---|
324 | |
---|
325 | \note Returns a dynamically allocated DataLookup2D, which has |
---|
326 | to be deleted by the caller to avoid memory leaks. |
---|
327 | */ |
---|
328 | const KernelLookup* |
---|
329 | validation_data(const utility::Index& train, |
---|
330 | const utility::Index& validation) const; |
---|
331 | |
---|
332 | /** |
---|
333 | \return true if underlying Kernel is weighted |
---|
334 | */ |
---|
335 | bool weighted(void) const; |
---|
336 | |
---|
337 | /** |
---|
338 | \return element at position (\a row, \a column) in the Kernel |
---|
339 | matrix |
---|
340 | */ |
---|
341 | double operator()(size_t row, size_t column) const; |
---|
342 | |
---|
343 | private: |
---|
344 | const KernelLookup& operator=(const KernelLookup&); |
---|
345 | bool validate(const utility::Index&) const; |
---|
346 | |
---|
347 | utility::Index column_index_; |
---|
348 | utility::SmartPtr<const Kernel> kernel_; |
---|
349 | utility::Index row_index_; |
---|
350 | |
---|
351 | }; // class KernelLookup |
---|
352 | |
---|
353 | }}} // of namespace classifier, yat, and theplu |
---|
354 | |
---|
355 | #endif |
---|