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

Last change on this file since 826 was 826, checked in by Peter, 16 years ago

documentation and some minor fixes Fixes #135

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 9.7 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. This implies that underlying kernel is deleted
57  /// in destructor of MatrixLookup, but only if there is no other
58  /// owner of the underlying kernel. A reference counter is used to
59  /// keep track of number of owners. Ownership is copied in copy
60  /// constructors and assignments.
61  ///
62  class KernelLookup : public DataLookup2D
63  {
64
65  public:
66
67    ///
68    /// @brief Constructor a Lookup into a Kernel
69    ///
70    /// Constructs a KernelLookup corresponding to the Kernel @a
71    /// kernel. By default @a owner is set to false, which means
72    /// KernelLookup does not own the underlying Kernel.
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.
108    ///
109    /// If \a kl is owner of underlying data, constructed
110    /// KernelLookup will also be set as owner of underlying data.
111    ///
112    KernelLookup(const KernelLookup& kl);
113
114
115    ///
116    /// @brief Contructing a sub-KernelLookup.
117    ///
118    /// Contructor building a sub-KernelLookup from a KernelLookup
119    /// defined by row index vector and column index vector. In the
120    /// created Lookup the element in the \f$ i \f$ th row in the
121    /// \f$ j \f$ th column is identical to the element in row row[i] and
122    /// columns column[j] in the copied @a kl. The resulting
123    /// KernelLookup is independent of the old KernelLookup, but is
124    /// undefined in case underlying Kernel is destroyed.
125    ///
126    /// If \a kl is owner of underlying data, constructed
127    /// KernelLookup will also be set as owner of underlying data.
128    ///
129    /// @note For training usage row index shall always be equal to
130    /// column index.
131    ///
132    KernelLookup(const KernelLookup& kl, const std::vector<size_t>& row, 
133                 const std::vector<size_t>& column);
134
135    ///
136    /// Constructor taking the column (default) or row index vector as
137    /// input. If @a row is false the created KernelLookup will have
138    /// equally many rows as @a kernel.
139    ///
140    /// If \a kl is owner of underlying data, constructed
141    /// KernelLookup will also be set as owner of underlying data.
142    ///
143    /// @note If underlying kernel goes out of scope or is deleted, the
144    /// KernelLookup becomes invalid and the result of further use is
145    /// undefined.
146    ///
147    KernelLookup(const KernelLookup& kernel, const std::vector<size_t>&, 
148                 const bool row=false);
149
150    ///
151    /// @brief Destructor
152    ///
153    /// Deletes underlying Kernel if KernelLookup owns it and there is
154    /// no other owner.
155    ///
156    virtual ~KernelLookup(void);
157
158    ///
159    /// Each column in returned DataLookup corresponds to the column
160    /// in KernelLookup.
161    ///
162    /// \return data that KernelLookup is built upon.
163    ///
164    /// @Note Returns a dynamically allocated MatrixLookup, which has
165    /// to be deleted by the caller to avoid memory leaks.
166    ///
167    const DataLookup2D* data(void) const;
168
169    /**
170       Function to calculate a new Kernel element using the underlying
171       KernelFunction. The value is calculated between @a vec and the
172       data vector of the \a i th sample, in other words, the
173       sample corresponding to the \a i th row.
174    */
175    double element(const DataLookup1D& vec, size_t i) const;
176
177    /**
178       Function to calculate a new Kernel element using the underlying
179       KernelFunction. The value is calulated between @a vec and the
180       data vector of the \f$ i \f$ th sample, in other words, the
181       sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th
182       column. In case KernelLookup is a sub-Kernel and not symmetric,
183       the kernel value is calculated between @a vec and the data
184       vector corresponding to \f$ i \f$ th row.
185    */
186    double element(const DataLookupWeighted1D& vec, size_t i) const;
187
188    /**
189       Each element in returned KernelLookup is calculated using only
190       selected features (defined by @a index). Each element
191       corresponds to the same pair of samples as in the original
192       KernelLookup.
193
194       \Note Returns a dynamically allocated KernelLookup, which has
195       to be deleted by the caller to avoid memory leaks.
196    */
197    const KernelLookup* selected(const std::vector<size_t>& index) const;
198   
199    /**
200       This function is useful when predicting on an independent data
201       set using a kernel-based classifier. In returned KernelLookup
202       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
203       data. Row \f$ i \f$ in returned KernelLookup corresponds to
204       same sample as row \f$ i \f$ in @a this. In other words, this
205       function returns a KernelLookup containing the kernel elements
206       between the passed @a data and the internal underlying data @a
207       this was built from.
208   
209       @Note Returns a dynamically allocated DataLookup2D, which has
210       to be deleted by the caller to avoid memory leaks.
211    */
212    const KernelLookup* test_kernel(const MatrixLookup& data) const;
213
214    /**
215       This function is useful when predicting on an independent data
216       set using a kernel-based classifier. In returned KernelLookup
217       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
218       data. Row \f$ i \f$ in returned KernelLookup corresponds to
219       same sample as row \f$ i \f$ in @a this. In other words, this
220       function returns a KernelLookup containing the kernel elements
221       between the passed @a data and the internal underlying data @a
222       this was built from.
223   
224       @Note Returns a dynamically allocated DataLookup2D, which has
225       to be deleted by the caller to avoid memory leaks.
226    */
227    const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const;
228
229    /**
230       \brief Creates a sub-Kernel identical to the one created using
231       KernelLookup(*this, train, train).
232   
233       \return pointer to dynamically allocated sub-Lookup of the
234       KernelLookup
235   
236       \Note Returns a dynamically allocated DataLookup2D, which has
237       to be deleted by the caller to avoid memory leaks.
238    */
239    const KernelLookup* training_data(const std::vector<size_t>& train) const;
240
241    /**
242       In returned kernel each row corresponds to a training sample
243       and each column corresponds to a validation sample. The created
244       sub-KernelLookup is equivalent to using KernelLooup(*this,
245       train, validation).
246   
247       \return sub-Lookup of the DataLookup2D
248   
249       \Note Returns a dynamically allocated DataLookup2D, which has
250       to be deleted by the caller to avoid memory leaks.
251    */
252    const KernelLookup* 
253    validation_data(const std::vector<size_t>& train, 
254                    const std::vector<size_t>& validation) const;
255
256    /**
257       \return true if underlying Kernel is weighted
258    */
259    bool weighted(void) const;
260
261    /**
262       \return element at position (\a row, \a column) in the Kernel
263       matrix
264    */
265    double operator()(size_t row, size_t column) const;
266
267  private:
268    const KernelLookup& operator=(const KernelLookup&);
269
270    const Kernel* kernel_;
271   
272  }; // class KernelLookup
273
274}}} // of namespace classifier, yat, and theplu
275
276#endif
Note: See TracBrowser for help on using the repository browser.