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

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

refs #247, #252, and #267

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