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