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

Last change on this file since 1487 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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