source: trunk/c++_tools/classifier/KernelLookup.h @ 656

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

fixes #145 cleaning up interface of DataLook2D. Those function were introduced only to avoid dynamic_casts in SubsetGenerator? and in the end dynamic_cast was necessary anyway, so these functions are just contaminating the interface.

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