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

Last change on this file since 676 was 675, checked in by Jari Häkkinen, 16 years ago

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 10.0 KB
Line 
1#ifndef _theplu_classifier_kernel_lookup_
2#define _theplu_classifier_kernel_lookup_
3
4// $Id$
5
6/*
7  Copyright (C) The authors contributing to this file.
8
9  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "yat/classifier/Kernel.h"
28#include "yat/classifier/DataLookup2D.h"
29#include "yat/classifier/MatrixLookup.h"
30
31#include <vector>
32
33namespace theplu {
34namespace classifier {
35
36  class KernelFunction;
37
38  ///
39  /// @brief Lookup into Kernel
40  ///
41  /// This is the KernelLookup class to be used together with kernel
42  /// methods such as Support Vector Machines (SVM). The class does
43  /// not contain any data or values, but rather is a lookup into a
44  /// Kernel object. Each row and each column corresponds to a row and
45  /// a column in the Kernel, respectively. This design allow for fast
46  /// creation of sub-kernels, which is a common operation in most
47  /// traning/validation procedures.
48  ///
49  /// A KernelLookup can be created directly from a Kernel or from an
50  /// other KernelLookup. In the latter case, the resulting
51  /// KernelLookup is looking directly into the underlying Kernel to
52  /// avoid multiple lookups.
53  ///
54  /// There is a possibility to set the KernelLookup as owner of the
55  /// underlying Kernel. In that case the underlying Kernel will be
56  /// destroyed in the destructor. Consequently, the underlying Kernel
57  /// must have been dynamically allocated and no other KernelLookup
58  /// can own the Kernel.
59  ///
60  class KernelLookup : public DataLookup2D
61  {
62
63  public:
64   
65    ///
66    /// @brief Constructor a Lookup into a Kernel
67    ///
68    /// Constructs a KernelLookup corresponding to the Kernel @a
69    /// kernel. By default @a owner is set to false, which means
70    /// KernelLookup does not own the underlying Kernel. If
71    /// KernelLookup owns the Kernel the Kernel will be deleted
72    /// in the destructor.
73    ///
74    /// @note If underlying Kernel goes out of scope or is deleted, the
75    /// KernelLookup becomes invalid and the result of further use is
76    /// undefined.
77    ///
78    /// @note Do not construct two KernelLookups from the same @a
79    /// kernel with @a owner set to true because that will cause
80    /// multiple deletion of @a kernel.
81    ///
82    KernelLookup(const Kernel& kernel, const bool owner=false);
83
84    ///
85    /// @brief Constructing a Lookup into a subKernel
86    ///
87    /// Creating a Lookup into parts of the Kernel. In the created
88    /// Lookup the element in the \f$ i \f$ th row in the \f$ j \f$ th
89    /// column is identical to the element in row row[i] and columns
90    /// column[j] in the underlying @a kernel. If @a owner is set to
91    /// true yhe underlying @a kernel is destroyed in the destructor.
92    ///
93    /// @note If @a kernel goes out of scope or is deleted, the
94    /// returned pointer becomes invalid and the result of further use is
95    /// undefined.
96    ///
97    /// @note For training usage row index shall always be equal to
98    /// column index.
99    ///
100    KernelLookup(const Kernel& kernel, const std::vector<size_t>& row, 
101                 const std::vector<size_t>& column, const bool owner=false);
102   
103    ///
104    /// @brief Copy constructor.
105    ///
106    /// A Lookup is created looking into the
107    /// same underlying Kernel as @a kl is looking into. The newly
108    /// created KernelLookup does not own the underlying Kernel.
109    ///
110    KernelLookup(const KernelLookup& kl);
111
112
113    ///
114    /// @brief Contructing a sub-KernelLookup.
115    ///
116    /// Contructor building a sub-KernelLookup from a KernelLookup
117    /// defined by row index vector and column index vector. In the
118    /// created Lookup the element in the \f$ i \f$ th row in the
119    /// \f$ j \f$ th column is identical to the element in row row[i] and
120    /// columns column[j] in the copied @a kl. The resulting
121    /// KernelLookup is independent of the old KernelLookup, but is
122    /// undefined in case underlying Kernel is destroyed.
123    ///
124    /// @note For training usage row index shall always be equal to
125    /// column index.
126    ///
127    KernelLookup(const KernelLookup& kl, const std::vector<size_t>& row, 
128                 const std::vector<size_t>& column);
129   
130    ///
131    /// Constructor taking the column (default) or row index vector as
132    /// input. If @a row is false the created KernelLookup will have
133    /// equally many rows as @a kernel.
134    ///
135    /// @note If underlying kernel goes out of scope or is deleted, the
136    /// KernelLookup becomes invalid and the result of further use is
137    /// undefined.
138    ///
139    KernelLookup(const KernelLookup& kernel, const std::vector<size_t>&, 
140                 const bool row=false);
141
142    ///
143    /// @brief Destructor
144    ///
145    /// Deletes underlying Kernel if KernelLookup owns it.
146    ///
147    virtual ~KernelLookup(void);
148
149
150    ///
151    /// Creates a sub-Kernel identical to the one created using
152    /// KernelLookup(*this, train, train).
153    ///
154    /// @return pointer to dynamically allocated sub-Lookup of the KernelLookup
155    ///
156    /// @Note Returns a dynamically allocated DataLookup2D, which has
157    /// to be deleted by the caller to avoid memory leaks.
158    ///
159    const KernelLookup* training_data(const std::vector<size_t>& train) const;
160
161
162    ///
163    /// In returned kernel each row corresponds to a training sample
164    /// and each column corresponds to a validation sample. The
165    /// created sub-KernelLookup is equivalent to using
166    /// KernelLooup(*this, train, validation).
167    ///
168    /// @return sub-Lookup of the DataLookup2D
169    ///
170    /// @Note Returns a dynamically allocated DataLookup2D, which has
171    /// to be deleted by the caller to avoid memory leaks.
172    ///
173    const KernelLookup* 
174    validation_data(const std::vector<size_t>& train, 
175                    const std::vector<size_t>& validation) const;
176
177
178    /**
179       This function is useful when predicting on a independent data
180       set using a kernel-based classifier. In returned KernelLookup
181       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
182       data. Row \f$ i \f$ in returned KernelLookup corresponds to
183       same sample as row \f$ i \f$ in @a this. In other words, this
184       function returns a KernelLookup containing the kernel elements
185       between the passed @a data and the internal underlying data @a
186       this was built from.
187   
188       @Note Returns a dynamically allocated DataLookup2D, which has
189       to be deleted by the caller to avoid memory leaks.
190    */
191    const KernelLookup* test_kernel(const MatrixLookup& data) const;
192
193
194    /**
195       This function is useful when predicting on a independent data
196       set using a kernel-based classifier. In returned KernelLookup
197       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
198       data. Row \f$ i \f$ in returned KernelLookup corresponds to
199       same sample as row \f$ i \f$ in @a this. In other words, this
200       function returns a KernelLookup containing the kernel elements
201       between the passed @a data and the internal underlying data @a
202       this was built from.
203   
204       @Note Returns a dynamically allocated DataLookup2D, which has
205       to be deleted by the caller to avoid memory leaks.
206    */
207    const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const;
208
209
210    ///
211    /// @return element at position (\a row, \a column) in the Kernel
212    /// matrix
213    ///
214    inline double operator()(const size_t row,const size_t column) const
215    { return (*kernel_)(row_index_[row],column_index_[column]); }
216
217    ///
218    /// Each column in returned MatrixLookup corresponds to the column
219    /// in KernelLookup.
220    ///
221    /// @Note Returns a dynamically allocated MatrixLookup, which has
222    /// to be deleted by the caller to avoid memory leaks.
223    ///
224    inline const DataLookup2D* data(void) const
225    { return kernel_->data().training_data(column_index_); }
226
227
228    ///
229    /// Function to calculate a new Kernel element using the
230    /// underlying KernelFunction. The value is calulated between @a
231    /// vec and the data vector of the \f$ i \f$ th sample, in other
232    /// words, the sample corresponding to the \f$ i \f$ th row or
233    /// \f$ i \f$ th column. In case KernelLookup is a sub-Kernel and not
234    /// symmetric, the kernel value is calculated between @a vec and
235    /// the data vector corresponding to \f$ i \f$ th row.
236    ///
237    inline double element(const DataLookup1D& vec, const size_t i) const
238    { return kernel_->element(vec, row_index_[i]); }
239
240    ///
241    /// Function to calculate a new Kernel element using the
242    /// underlying KernelFunction. The value is calulated between @a
243    /// vec and the data vector of the \f$ i \f$ th sample, in other
244    /// words, the sample corresponding to the \f$ i \f$ th row or
245    /// \f$ i \f$ th column. In case KernelLookup is a sub-Kernel and not
246    /// symmetric, the kernel value is calculated between @a vec and
247    /// the data vector corresponding to \f$ i \f$ th row.
248    ///
249    inline double element(const DataLookupWeighted1D& vec, const size_t i) const
250    { return kernel_->element(vec, row_index_[i]); }
251
252    ///
253    /// Each element in returned KernelLookup is calculated using only
254    /// selected features (defined by @a index). Each element
255    /// corresponds to the same pair of samples as in the original
256    /// KernelLookup.
257    ///
258    /// @Note Returns a dynamically allocated KernelLookup, which has
259    /// to be deleted by the caller to avoid memory leaks.
260    ///
261    const KernelLookup* selected(const std::vector<size_t>& index) const;
262   
263    ///
264    /// @return true if underlying Kernel is weighted
265    ///
266    inline bool weighted(void) const { return kernel_->weighted(); }
267
268    inline const Kernel* kernel(void) const { return kernel_; }
269   
270  private:
271    const KernelLookup& operator=(const KernelLookup&);
272
273    const Kernel* kernel_;
274   
275  }; // class KernelLookup
276
277}} // of namespace classifier and namespace theplu
278
279#endif
Note: See TracBrowser for help on using the repository browser.