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

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

adding some typedefs in Container2Ds - refs #448

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