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

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

Iterators for KernelLookup? - refs #267

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