1 | #ifndef _theplu_yat_classifier_kernel_ |
---|
2 | #define _theplu_yat_classifier_kernel_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
9 | Copyright (C) 2007 Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://trac.thep.lu.se/trac/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 2 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "KernelFunction.h" |
---|
30 | |
---|
31 | #include <cctype> |
---|
32 | #include <vector> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace classifier { |
---|
37 | |
---|
38 | class DataLookup2D; |
---|
39 | class MatrixLookup; |
---|
40 | class MatrixLookupWeighted; |
---|
41 | |
---|
42 | /// |
---|
43 | /// @brief Interface Class for Kernels. |
---|
44 | /// |
---|
45 | /// Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$ |
---|
46 | /// is number of samples. Each element in the Kernel corresponds to |
---|
47 | /// the scalar product of the corresponding pair of samples. At the |
---|
48 | /// time being there are two kinds of kernels. Kernel_SEV that is |
---|
49 | /// optimized to be fast and Kernel_MEV that is preferable when |
---|
50 | /// dealing with many samples and memory might be a |
---|
51 | /// bottleneck. A |
---|
52 | /// KernelFunction defines what kind of scalar product the Kernel |
---|
53 | /// represents, e.g. a Polynomial Kernel of degree 1 means we are |
---|
54 | /// dealing with the ordinary linear scalar product. |
---|
55 | /// |
---|
56 | /// @note If the KernelFunction is destroyed, the Kernel is no |
---|
57 | /// longer defined. |
---|
58 | /// |
---|
59 | class Kernel |
---|
60 | { |
---|
61 | |
---|
62 | public: |
---|
63 | |
---|
64 | /// |
---|
65 | /// Constructor taking the @a data matrix and KernelFunction as |
---|
66 | /// input. Each column in the data matrix corresponds to one |
---|
67 | /// sample and the Kernel matrix is built applying the |
---|
68 | /// KernelFunction on each pair of columns in the data matrix. |
---|
69 | /// If @a own is set to true, Kernel is owner of underlying data. |
---|
70 | /// |
---|
71 | /// @note Can not handle NaNs. To deal with missing values use |
---|
72 | /// constructor taking MatrixLookupWeighted. |
---|
73 | /// |
---|
74 | Kernel(const MatrixLookup& data, const KernelFunction& kf, |
---|
75 | const bool own=false); |
---|
76 | |
---|
77 | /// |
---|
78 | /// Constructor taking the @a data matrix (with weights) and |
---|
79 | /// KernelFunction as |
---|
80 | /// input. Each column in the data matrix corresponds to one |
---|
81 | /// sample and the Kernel matrix is built applying the |
---|
82 | /// KernelFunction on each pair of columns in the data matrix. |
---|
83 | /// If @a own is set to true, Kernel is owner of underlying data. |
---|
84 | /// |
---|
85 | Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, |
---|
86 | const bool own=false); |
---|
87 | |
---|
88 | /// |
---|
89 | /// The new kernel is created using selected features @a |
---|
90 | /// index. Kernel will own its underlying data |
---|
91 | /// |
---|
92 | Kernel(const Kernel& kernel, const std::vector<size_t>& index); |
---|
93 | |
---|
94 | /// |
---|
95 | /// @brief Destructor |
---|
96 | /// |
---|
97 | /// If Kernel is owner of underlying data and Kernel is the last |
---|
98 | /// owner, underlying data is deleted. |
---|
99 | /// |
---|
100 | virtual ~Kernel(void); |
---|
101 | |
---|
102 | /// |
---|
103 | /// @return element at position (\a row, \a column) of the Kernel |
---|
104 | /// matrix |
---|
105 | /// |
---|
106 | virtual double operator()(const size_t row, const size_t column) const=0; |
---|
107 | |
---|
108 | /// |
---|
109 | /// @return const reference to the underlying data. |
---|
110 | /// |
---|
111 | const DataLookup2D& data(void) const; |
---|
112 | |
---|
113 | /// |
---|
114 | /// Calculates the scalar product (using the KernelFunction) |
---|
115 | /// between vector @a vec and the \f$ i \f$ th column in the data |
---|
116 | /// matrix. |
---|
117 | /// |
---|
118 | double element(const DataLookup1D& vec, const size_t i) const; |
---|
119 | |
---|
120 | /// |
---|
121 | /// Calculates the weighted scalar product (using the |
---|
122 | /// KernelFunction) between vector @a vec and the \f$ i \f$ th column |
---|
123 | /// in the data matrix. Using a weight vector with all elements |
---|
124 | /// equal to unity yields same result as the non-weighted version |
---|
125 | /// above. |
---|
126 | /// |
---|
127 | double element(const DataLookupWeighted1D& vec, const size_t i) const; |
---|
128 | |
---|
129 | /// |
---|
130 | /// An interface for making new classifier objects. This function |
---|
131 | /// allows for specification at run-time of which kernel to |
---|
132 | /// instatiate (see 'Prototype' in Design Patterns). |
---|
133 | /// |
---|
134 | /// @Note Returns a dynamically allocated Kernel, which has |
---|
135 | /// to be deleted by the caller to avoid memory leaks. |
---|
136 | /// |
---|
137 | virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0; |
---|
138 | |
---|
139 | |
---|
140 | /// |
---|
141 | /// An interface for making new classifier objects. This function |
---|
142 | /// allows for specification at run-time of which kernel to |
---|
143 | /// instatiate (see 'Prototype' in Design Patterns). |
---|
144 | /// |
---|
145 | /// @Note Returns a dynamically allocated Kernel, which has |
---|
146 | /// to be deleted by the caller to avoid memory leaks. |
---|
147 | /// |
---|
148 | virtual const Kernel* make_kernel(const MatrixLookupWeighted&, |
---|
149 | const bool own=false) const=0; |
---|
150 | |
---|
151 | |
---|
152 | /** |
---|
153 | \brief number of samples |
---|
154 | */ |
---|
155 | size_t size(void) const; |
---|
156 | |
---|
157 | /// |
---|
158 | /// @return true if kernel is calculated using weights |
---|
159 | /// |
---|
160 | bool weighted(void) const; |
---|
161 | |
---|
162 | protected: |
---|
163 | /// underlying data |
---|
164 | const DataLookup2D* data_; |
---|
165 | /// same as data_ if weifghted otherwise a NULL pointer |
---|
166 | const MatrixLookupWeighted* data_w_; |
---|
167 | /// type of Kernel Function e.g. Gaussian (aka RBF) |
---|
168 | const KernelFunction* kf_; |
---|
169 | |
---|
170 | /// |
---|
171 | /// pointer telling how many owners to underlying data |
---|
172 | /// (data_). NULL if this is not an owner. |
---|
173 | /// |
---|
174 | u_int* ref_count_; |
---|
175 | |
---|
176 | /// |
---|
177 | /// pointer telling how many owners to underlying weights |
---|
178 | /// (data_w_). NULL if this is not an owner. |
---|
179 | /// |
---|
180 | u_int* ref_count_w_; |
---|
181 | |
---|
182 | private: |
---|
183 | /// |
---|
184 | /// Copy constructor (not implemented) |
---|
185 | /// |
---|
186 | Kernel(const Kernel&); |
---|
187 | |
---|
188 | const Kernel& operator=(const Kernel&); |
---|
189 | |
---|
190 | }; // class Kernel |
---|
191 | |
---|
192 | }}} // of namespace classifier, yat, and theplu |
---|
193 | |
---|
194 | #endif |
---|