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

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

using DataWeight? rather than std::pair<double, double>

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