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

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

removed Policy from Container2DIterator - refs #398

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