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

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

Fixes #170. Almost all inlines removed, some classes have no cc file.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 9.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) 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    /// Each column in returned MatrixLookup corresponds to the column
152    /// in KernelLookup.
153    ///
154    /// @Note Returns a dynamically allocated MatrixLookup, which has
155    /// to be deleted by the caller to avoid memory leaks.
156    ///
157    const DataLookup2D* data(void) const;
158
159    /**
160       Function to calculate a new Kernel element using the underlying
161       KernelFunction. The value is calulated between @a vec and the
162       data vector of the \f$ i \f$ th sample, in other words, the
163       sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th
164       column. In case KernelLookup is a sub-Kernel and not symmetric,
165       the kernel value is calculated between @a vec and the data
166       vector corresponding to \f$ i \f$ th row.
167    */
168    double element(const DataLookup1D& vec, size_t i) const;
169
170    /**
171       Function to calculate a new Kernel element using the underlying
172       KernelFunction. The value is calulated between @a vec and the
173       data vector of the \f$ i \f$ th sample, in other words, the
174       sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th
175       column. In case KernelLookup is a sub-Kernel and not symmetric,
176       the kernel value is calculated between @a vec and the data
177       vector corresponding to \f$ i \f$ th row.
178    */
179    double element(const DataLookupWeighted1D& vec, size_t i) const;
180
181    const Kernel* kernel(void) const;
182
183    /**
184       Each element in returned KernelLookup is calculated using only
185       selected features (defined by @a index). Each element
186       corresponds to the same pair of samples as in the original
187       KernelLookup.
188
189       \Note Returns a dynamically allocated KernelLookup, which has
190       to be deleted by the caller to avoid memory leaks.
191    */
192    const KernelLookup* selected(const std::vector<size_t>& index) const;
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 MatrixLookup& data) const;
208
209    /**
210       This function is useful when predicting on a independent data
211       set using a kernel-based classifier. In returned KernelLookup
212       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
213       data. Row \f$ i \f$ in returned KernelLookup corresponds to
214       same sample as row \f$ i \f$ in @a this. In other words, this
215       function returns a KernelLookup containing the kernel elements
216       between the passed @a data and the internal underlying data @a
217       this was built from.
218   
219       @Note Returns a dynamically allocated DataLookup2D, which has
220       to be deleted by the caller to avoid memory leaks.
221    */
222    const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const;
223
224    /**
225       \brief Creates a sub-Kernel identical to the one created using
226       KernelLookup(*this, train, train).
227   
228       \return pointer to dynamically allocated sub-Lookup of the
229       KernelLookup
230   
231       \Note Returns a dynamically allocated DataLookup2D, which has
232       to be deleted by the caller to avoid memory leaks.
233    */
234    const KernelLookup* training_data(const std::vector<size_t>& train) const;
235
236    /**
237       In returned kernel each row corresponds to a training sample
238       and each column corresponds to a validation sample. The created
239       sub-KernelLookup is equivalent to using KernelLooup(*this,
240       train, validation).
241   
242       \return sub-Lookup of the DataLookup2D
243   
244       \Note Returns a dynamically allocated DataLookup2D, which has
245       to be deleted by the caller to avoid memory leaks.
246    */
247    const KernelLookup* 
248    validation_data(const std::vector<size_t>& train, 
249                    const std::vector<size_t>& validation) const;
250
251    /**
252       \return true if underlying Kernel is weighted
253    */
254    bool weighted(void) const;
255
256    /**
257       \return element at position (\a row, \a column) in the Kernel
258       matrix
259    */
260    double operator()(size_t row, size_t column) const;
261
262  private:
263    const KernelLookup& operator=(const KernelLookup&);
264
265    const Kernel* kernel_;
266   
267  }; // class KernelLookup
268
269}}} // of namespace classifier, yat, and theplu
270
271#endif
Note: See TracBrowser for help on using the repository browser.