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

Last change on this file since 1083 was 1083, checked in by Peter, 14 years ago

working on #247

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