source: branches/0.4-stable/yat/classifier/KernelLookup.h

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

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 10.3 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 2 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 this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
30#include "Kernel.h"
31#include "yat/utility/Container2DIterator.h"
32#include "yat/utility/Index.h"
33#include "yat/utility/iterator_traits.h"
34#include "yat/utility/SmartPtr.h"
35#include "yat/utility/StrideIterator.h"
36
37namespace theplu {
38namespace yat {
39namespace classifier {
40
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 allows 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
57  /// another 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 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& kl, 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    /// \throw if KernelLookup is weighted
213    ///
214    MatrixLookup data(void) const;
215
216    ///
217    /// Each column in returned DataLookup corresponds to the column
218    /// in KernelLookup.
219    ///
220    /// \return data that KernelLookup is built upon.
221    ///
222    /// \throw if KernelLookup is unweighted
223    ///
224    MatrixLookupWeighted data_weighted(void) const;
225
226    /**
227       Function to calculate a new Kernel element using the underlying
228       KernelFunction. The value is calculated between @a vec and the
229       data vector of the \a i th sample, in other words, the
230       sample corresponding to the \a i th row.
231    */
232    double element(const DataLookup1D& vec, size_t i) const;
233
234    /**
235       Function to calculate a new Kernel element using the underlying
236       KernelFunction. The value is calulated between @a vec and the
237       data vector of the \f$ i \f$ th sample, in other words, the
238       sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th
239       column. In case KernelLookup is a sub-Kernel and not symmetric,
240       the kernel value is calculated between @a vec and the data
241       vector corresponding to \f$ i \f$ th row.
242    */
243    double element(const DataLookupWeighted1D& vec, size_t i) const;
244
245    /**
246       \return const_iterator pointing to end of matrix
247     */
248    const_iterator end(void) const;
249
250    /**
251       \return const_iterator pointing to end of column \a i
252     */
253    const_column_iterator end_column(size_t) const;
254
255    /**
256       \return const_iterator pointing to end of row \a i
257     */
258    const_row_iterator end_row(size_t) const;
259
260    /**
261       \return number of rows
262    */
263    size_t rows(void) const;
264
265    /**
266       Each element in returned KernelLookup is calculated using only
267       selected features (defined by @a index). Each element
268       corresponds to the same pair of samples as in the original
269       KernelLookup.
270    */
271    KernelLookup selected(const utility::Index& index) const;
272   
273    /**
274       This function is useful when predicting on an independent data
275       set using a kernel-based classifier. In returned KernelLookup
276       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
277       data. Row \f$ i \f$ in returned KernelLookup corresponds to
278       same sample as row \f$ i \f$ in @a this. In other words, this
279       function returns a KernelLookup containing the kernel elements
280       between the passed @a data and the internal underlying data @a
281       this was built from.
282    */
283    KernelLookup test_kernel(const MatrixLookup& data) const;
284
285    /**
286       This function is useful when predicting on an independent data
287       set using a kernel-based classifier. In returned KernelLookup
288       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
289       data. Row \f$ i \f$ in returned KernelLookup corresponds to
290       same sample as row \f$ i \f$ in @a this. In other words, this
291       function returns a KernelLookup containing the kernel elements
292       between the passed @a data and the internal underlying data @a
293       this was built from.
294    */
295    KernelLookup test_kernel(const MatrixLookupWeighted& data) const;
296
297    /**
298       \return true if underlying Kernel is weighted
299    */
300    bool weighted(void) const;
301
302    /**
303       \return element at position (\a row, \a column) in the Kernel
304       matrix
305    */
306    double operator()(size_t row, size_t column) const;
307
308  private:
309    const KernelLookup& operator=(const KernelLookup&);
310    bool validate(const utility::Index&) const;
311
312    utility::Index column_index_;
313    utility::SmartPtr<const Kernel> kernel_;
314    utility::Index row_index_;
315   
316  }; // class KernelLookup
317
318}}} // of namespace classifier, yat, and theplu
319
320#endif
Note: See TracBrowser for help on using the repository browser.