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

Last change on this file since 1529 was 1529, checked in by Peter, 13 years ago

refs #444 fixing KernelLookup?

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