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 | |
---|
11 | namespace theplu { |
---|
12 | namespace 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 | /// In returned kernel each row corresponds to a training sample |
---|
142 | /// and each column corresponds to a validation sample. The |
---|
143 | /// created sub-KernelLookup is equivalent to using |
---|
144 | /// KernelLooup(*this, train, validation). |
---|
145 | /// |
---|
146 | /// @return sub-Lookup of the DataLookup2D |
---|
147 | /// |
---|
148 | /// @Note Returns a dynamically allocated DataLookup2D, which has |
---|
149 | /// to be deleted by the caller to avoid memory leaks. |
---|
150 | /// |
---|
151 | const KernelLookup* |
---|
152 | validation_data(const std::vector<size_t>& train, |
---|
153 | const std::vector<size_t>& validation) const; |
---|
154 | |
---|
155 | |
---|
156 | /** |
---|
157 | This function is useful when predicting on a independent data |
---|
158 | set using a kernel-based classifier. In returned KernelLookup |
---|
159 | column \f$ i \f$ corresponds to column \f$ i \f$ in @a |
---|
160 | data. Row \f$ i \f$ in returned KernelLookup corresponds to |
---|
161 | same sample as row \f$ i \f$ in @a this. In other words, this |
---|
162 | function returns a KernelLookup containing the kernel elements |
---|
163 | between the passed @a data and the internal underlying data @a |
---|
164 | this was built from. |
---|
165 | |
---|
166 | @Note Returns a dynamically allocated DataLookup2D, which has |
---|
167 | to be deleted by the caller to avoid memory leaks. |
---|
168 | */ |
---|
169 | const KernelLookup* test_kernel(const MatrixLookup& data) const; |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | This function is useful when predicting on a independent data |
---|
174 | set using a kernel-based classifier. In returned KernelLookup |
---|
175 | column \f$ i \f$ corresponds to column \f$ i \f$ in @a |
---|
176 | data. Row \f$ i \f$ in returned KernelLookup corresponds to |
---|
177 | same sample as row \f$ i \f$ in @a this. In other words, this |
---|
178 | function returns a KernelLookup containing the kernel elements |
---|
179 | between the passed @a data and the internal underlying data @a |
---|
180 | this was built from. |
---|
181 | |
---|
182 | @Note Returns a dynamically allocated DataLookup2D, which has |
---|
183 | to be deleted by the caller to avoid memory leaks. |
---|
184 | */ |
---|
185 | const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const; |
---|
186 | |
---|
187 | |
---|
188 | /// |
---|
189 | /// @return element at position (\a row, \a column) in the Kernel |
---|
190 | /// matrix |
---|
191 | /// |
---|
192 | inline double operator()(const size_t row,const size_t column) const |
---|
193 | { return (*kernel_)(row_index_[row],column_index_[column]); } |
---|
194 | |
---|
195 | /// |
---|
196 | /// Each column in returned MatrixLookup corresponds to the column |
---|
197 | /// in KernelLookup. |
---|
198 | /// |
---|
199 | /// @Note Returns a dynamically allocated MatrixLookup, which has |
---|
200 | /// to be deleted by the caller to avoid memory leaks. |
---|
201 | /// |
---|
202 | inline const DataLookup2D* data(void) const |
---|
203 | { return kernel_->data().training_data(column_index_); } |
---|
204 | |
---|
205 | |
---|
206 | /// |
---|
207 | /// Function to calculate a new Kernel element using the |
---|
208 | /// underlying KernelFunction. The value is calulated between @a |
---|
209 | /// vec and the data vector of the \f$ i \f$ th sample, in other |
---|
210 | /// words, the sample corresponding to the \f$ i \f$ th row or |
---|
211 | /// \f$ i \f$ th column. In case KernelLookup is a sub-Kernel and not |
---|
212 | /// symmetric, the kernel value is calculated between @a vec and |
---|
213 | /// the data vector corresponding to \f$ i \f$ th row. |
---|
214 | /// |
---|
215 | inline double element(const DataLookup1D& vec, const size_t i) const |
---|
216 | { return kernel_->element(vec, row_index_[i]); } |
---|
217 | |
---|
218 | /// |
---|
219 | /// Function to calculate a new Kernel element using the |
---|
220 | /// underlying KernelFunction. The value is calulated between @a |
---|
221 | /// vec and the data vector of the \f$ i \f$ th sample, in other |
---|
222 | /// words, the sample corresponding to the \f$ i \f$ th row or |
---|
223 | /// \f$ i \f$ th column. In case KernelLookup is a sub-Kernel and not |
---|
224 | /// symmetric, the kernel value is calculated between @a vec and |
---|
225 | /// the data vector corresponding to \f$ i \f$ th row. |
---|
226 | /// |
---|
227 | inline double element(const DataLookupWeighted1D& vec, const size_t i) const |
---|
228 | { return kernel_->element(vec, row_index_[i]); } |
---|
229 | |
---|
230 | /// |
---|
231 | /// Each element in returned KernelLookup is calculated using only |
---|
232 | /// selected features (defined by @a index). Each element |
---|
233 | /// corresponds to the same pair of samples as in the original |
---|
234 | /// KernelLookup. |
---|
235 | /// |
---|
236 | /// @Note Returns a dynamically allocated KernelLookup, which has |
---|
237 | /// to be deleted by the caller to avoid memory leaks. |
---|
238 | /// |
---|
239 | const KernelLookup* selected(const std::vector<size_t>& index) const; |
---|
240 | |
---|
241 | /// |
---|
242 | /// @return true if underlying Kernel is weighted |
---|
243 | /// |
---|
244 | inline bool weighted(void) const { return kernel_->weighted(); } |
---|
245 | |
---|
246 | inline const Kernel* kernel(void) const { return kernel_; } |
---|
247 | |
---|
248 | private: |
---|
249 | const KernelLookup& operator=(const KernelLookup&); |
---|
250 | |
---|
251 | const Kernel* kernel_; |
---|
252 | |
---|
253 | }; // class KernelLookup |
---|
254 | |
---|
255 | }} // of namespace classifier and namespace theplu |
---|
256 | |
---|
257 | #endif |
---|