source: trunk/yat/classifier/KernelLookup.h @ 1125

Last change on this file since 1125 was 1125, checked in by Peter, 16 years ago

fixing Doxygen parsing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 11.2 KB
Line 
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 "DataLookup2D.h"
31#include "MatrixLookup.h"
32#include "yat/utility/Container2DIterator.h"
33#include "yat/utility/iterator_traits.h"
34#include "yat/utility/StrideIterator.h"
35
36
37#include <vector>
38
39namespace theplu {
40namespace yat {
41namespace classifier {
42
43  class KernelFunction;
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 : public DataLookup2D
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 std::vector<size_t>& row, 
122                 const std::vector<size_t>& 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 std::vector<size_t>& row, 
154                 const std::vector<size_t>& 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 std::vector<size_t>&, 
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    /// Each column in returned DataLookup corresponds to the column
203    /// in KernelLookup.
204    ///
205    /// \return data that KernelLookup is built upon.
206    ///
207    /// @note Returns a dynamically allocated MatrixLookup, which has
208    /// to be deleted by the caller to avoid memory leaks.
209    ///
210    const DataLookup2D* data(void) const;
211
212    /**
213       Function to calculate a new Kernel element using the underlying
214       KernelFunction. The value is calculated between @a vec and the
215       data vector of the \a i th sample, in other words, the
216       sample corresponding to the \a i th row.
217    */
218    double element(const DataLookup1D& vec, size_t i) const;
219
220    /**
221       Function to calculate a new Kernel element using the underlying
222       KernelFunction. The value is calulated between @a vec and the
223       data vector of the \f$ i \f$ th sample, in other words, the
224       sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th
225       column. In case KernelLookup is a sub-Kernel and not symmetric,
226       the kernel value is calculated between @a vec and the data
227       vector corresponding to \f$ i \f$ th row.
228    */
229    double element(const DataLookupWeighted1D& vec, size_t i) const;
230
231    /**
232       \return const_iterator pointing to end of matrix
233     */
234    const_iterator end(void) const;
235
236    /**
237       \return const_iterator pointing to end of column \a i
238     */
239    const_column_iterator end_column(size_t) const;
240
241    /**
242       \return const_iterator pointing to end of row \a i
243     */
244    const_row_iterator end_row(size_t) const;
245
246    /**
247       Each element in returned KernelLookup is calculated using only
248       selected features (defined by @a index). Each element
249       corresponds to the same pair of samples as in the original
250       KernelLookup.
251
252       \note Returns a dynamically allocated KernelLookup, which has
253       to be deleted by the caller to avoid memory leaks.
254    */
255    const KernelLookup* selected(const std::vector<size_t>& index) const;
256   
257    /**
258       This function is useful when predicting on an independent data
259       set using a kernel-based classifier. In returned KernelLookup
260       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
261       data. Row \f$ i \f$ in returned KernelLookup corresponds to
262       same sample as row \f$ i \f$ in @a this. In other words, this
263       function returns a KernelLookup containing the kernel elements
264       between the passed @a data and the internal underlying data @a
265       this was built from.
266   
267       @note Returns a dynamically allocated DataLookup2D, which has
268       to be deleted by the caller to avoid memory leaks.
269    */
270    const KernelLookup* test_kernel(const MatrixLookup& data) const;
271
272    /**
273       This function is useful when predicting on an independent data
274       set using a kernel-based classifier. In returned KernelLookup
275       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
276       data. Row \f$ i \f$ in returned KernelLookup corresponds to
277       same sample as row \f$ i \f$ in @a this. In other words, this
278       function returns a KernelLookup containing the kernel elements
279       between the passed @a data and the internal underlying data @a
280       this was built from.
281   
282       @note Returns a dynamically allocated DataLookup2D, which has
283       to be deleted by the caller to avoid memory leaks.
284    */
285    const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const;
286
287    /**
288       \brief Creates a sub-Kernel identical to the one created using
289       KernelLookup(*this, train, train).
290   
291       \return pointer to dynamically allocated sub-Lookup of the
292       KernelLookup
293   
294       \note Returns a dynamically allocated DataLookup2D, which has
295       to be deleted by the caller to avoid memory leaks.
296    */
297    const KernelLookup* training_data(const std::vector<size_t>& train) const;
298
299    /**
300       In returned kernel each row corresponds to a training sample
301       and each column corresponds to a validation sample. The created
302       sub-KernelLookup is equivalent to using KernelLooup(*this,
303       train, validation).
304   
305       \return sub-Lookup of the DataLookup2D
306   
307       \note Returns a dynamically allocated DataLookup2D, which has
308       to be deleted by the caller to avoid memory leaks.
309    */
310    const KernelLookup* 
311    validation_data(const std::vector<size_t>& train, 
312                    const std::vector<size_t>& validation) const;
313
314    /**
315       \return true if underlying Kernel is weighted
316    */
317    bool weighted(void) const;
318
319    /**
320       \return element at position (\a row, \a column) in the Kernel
321       matrix
322    */
323    double operator()(size_t row, size_t column) const;
324
325  private:
326    const KernelLookup& operator=(const KernelLookup&);
327
328    const Kernel* kernel_;
329   
330  }; // class KernelLookup
331
332}}} // of namespace classifier, yat, and theplu
333
334#endif
Note: See TracBrowser for help on using the repository browser.