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

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

fixes #116 and #90 removed classes KernelWeighted_SEV and KernelWeighted_MEV and they're now absorbed into Kernel_SEV and Kernel_MEV, respectively.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 8.7 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 then 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    KernelLookup(const Kernel& kernel, const bool owner=false);
57
58    ///
59    /// @brief Constructing a Lookup into a subKernel
60    ///
61    /// Creating a Lookup into parts of the Kernel. In the created
62    /// Lookup the element in the \f$ i \f$ th row in the \f$ j \f$ th
63    /// column is identical to the element in row row[i] and columns
64    /// column[j] in the underlying @a kernel. If @a owner is set to
65    /// true yhe underlying @a kernel is destroyed in the destructor.
66    ///
67    /// @note If @a kernel goes out of scope or is deleted, the
68    /// returned pointer becomes invalid and the result of further use is
69    /// undefined.
70    ///
71    /// @note For training usage row index shall always be equal to
72    /// column index.
73    ///
74    KernelLookup(const Kernel& kernel, const std::vector<size_t>& row, 
75                 const std::vector<size_t>& column, const bool owner=false);
76   
77    ///
78    /// @brief Copy constructor.
79    ///
80    /// A Lookup is created looking into the
81    /// same underlying Kernel as @a kl is looking into. The newly
82    /// created KernelLookup does not own the underlying Kernel.
83    ///
84    KernelLookup(const KernelLookup& kl);
85
86
87    ///
88    /// @brief Contructing a sub-KernelLookup.
89    ///
90    /// Contructor building a sub-KernelLookup from a KernelLookup
91    /// defined by row index vector and column index vector. In the
92    /// created Lookup the element in the \f$ i \f$ th row in the
93    /// \f$ j \f$ th column is identical to the element in row row[i] and
94    /// columns column[j] in the copied @a kl. The resulting
95    /// KernelLookup is independent of the old KernelLookup, but is
96    /// undefined in case underlying Kernel is destroyed.
97    ///
98    /// @note For training usage row index shall always be equal to
99    /// column index.
100    ///
101    KernelLookup(const KernelLookup& kl, const std::vector<size_t>& row, 
102                 const std::vector<size_t>& column);
103   
104    ///
105    /// Constructor taking the column (default) or row index vector as
106    /// input. If @a row is false the created KernelLookup will have
107    /// equally many rows as @a kernel.
108    ///
109    /// @note If underlying kernel goes out of scope or is deleted, the
110    /// KernelLookup becomes invalid and the result of further use is
111    /// undefined.
112    ///
113    KernelLookup(const KernelLookup& kernel, const std::vector<size_t>&, 
114                 const bool row=false);
115
116    ///
117    /// @brief Destructor
118    ///
119    /// Deletes underlying Kernel if KernelLookup owns it.
120    ///
121    virtual ~KernelLookup(void);
122
123
124    ///
125    /// Creates a sub-Kernel identical to the one created using
126    /// KernelLookup(*this, train, train).
127    ///
128    /// @return pointer to dynamically allocated sub-Lookup of the KernelLookup
129    ///
130    /// @Note Returns a dynamically allocated DataLookup2D, which has
131    /// to be deleted by the caller to avoid memory leaks.
132    ///
133    const KernelLookup* training_data(const std::vector<size_t>& train) const;
134
135
136    ///
137    /// @retun a sub-kernel of kernel calculated using data defined by
138    /// @a features. Each row and each columns corresponds to a traing
139    /// sample defined by @a train.
140    ///
141    /// @return pointer to dynamically allocated sub-Lookup of the KernelLookup
142    ///
143    /// @Note Returns a dynamically allocated DataLookup2D, which has
144    /// to be deleted by the caller to avoid memory leaks.
145    ///
146    const KernelLookup* training_data(const std::vector<size_t>& features,
147                                      const std::vector<size_t>& train) const;
148
149
150    ///
151    /// In returned kernel each row corresponds to a training sample
152    /// and each column corresponds to a validation sample. The
153    /// created sub-KernelLookup is equivalent to using
154    /// KernelLooup(*this, train, validation).
155    ///
156    /// @return sub-Lookup of the DataLookup2D
157    ///
158    /// @Note Returns a dynamically allocated DataLookup2D, which has
159    /// to be deleted by the caller to avoid memory leaks.
160    ///
161    const KernelLookup* 
162    validation_data(const std::vector<size_t>& train, 
163                    const std::vector<size_t>& validation) const;
164
165
166    ///
167    /// In returned kernel each row corresponds to a training sample
168    /// and each column corresponds to a validation sample. The kernel
169    /// is based on the features defined by @a features.
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>& features, 
176                    const std::vector<size_t>& train, 
177                    const std::vector<size_t>& validation) const;
178
179
180    ///
181    /// @return element at position (\a row, \a column) in the Kernel
182    /// matrix
183    ///
184    inline double operator()(const size_t row,const size_t column) const
185    { return (*kernel_)(row_index_[row],column_index_[column]); }
186
187    ///
188    /// Each column in returned MatrixLookup corresponds to the column
189    /// in KernelLookup.
190    ///
191    /// @Note Returns a dynamically allocated MatrixLookup, which has
192    /// to be deleted by the caller to avoid memory leaks.
193    ///
194    inline const DataLookup2D* data(void) const
195    { return kernel_->data().training_data(column_index_); }
196
197
198    ///
199    /// Function to calculate a new Kernel element using the
200    /// underlying KernelFunction. The value is calulated between @a
201    /// vec and the data vector of the \f$ i \f$ th sample, in other
202    /// words, the sample corresponding to the \f$ i \f$ th row or
203    /// \f$ i \f$ th column. In case KernelLookup is a sub-Kernel and not
204    /// symmetric, the kernel value is calculated between @a vec and
205    /// the data vector corresponding to \f$ i \f$ th row.
206    ///
207    inline double element(const DataLookup1D& vec, const size_t i) const
208    { return kernel_->element(vec, row_index_[i]); }
209
210    ///
211    /// Weighted version of element function. Using weights @a w all
212    /// identical to unity results in same as using the unweighted
213    /// version above.
214    ///
215    inline double element(const DataLookupWeighted1D& vec, const size_t i) const
216    { return kernel_->element(vec, row_index_[i]); }
217
218    ///
219    /// @todo remove
220    /// @note This function will probably be removed
221    ///
222    /// @Note Returns a dynamically allocated KernelLookup, which has
223    /// to be deleted by the caller to avoid memory leaks.
224    ///
225    const KernelLookup* selected(const std::vector<size_t>&) const;
226   
227    ///
228    /// @return true if underlying Kernel is weighted
229    ///
230    inline bool weighted(void) const { return kernel_->weighted(); }
231
232    ///
233    /// Each column in returned MatrixLook corresponds to the column
234    /// in KernelLookup. If no weights were used in construction of
235    /// Kernel, each element in returned MatrixLookup is set to unity.
236    ///
237    /// @Note Returns a dynamically allocated MatrixLookup2D, which has
238    /// to be deleted by the caller to avoid memory leaks.
239    ///
240    // Peter remove this
241    //inline const MatrixLookup* weights(void) const
242    //{ return new MatrixLookup(kernel_->weights(),column_index_, false); }
243
244
245  private:
246    const KernelLookup& operator=(const KernelLookup&);
247
248    const Kernel* kernel_;
249   
250  }; // class KernelLookup
251
252}} // of namespace classifier and namespace theplu
253
254#endif
Note: See TracBrowser for help on using the repository browser.