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

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

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

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