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