16 enum KernelType {INVALID_TYPE = -1, INNER_PRODUCT, POLYNOMIAL, GAUSSIAN, CUSTOM};
21 template<
typename T =
double>
31 typename FunctionType<T>::Type func{
nullptr};
32 bool computed =
false;
42 mltk::dMatrix HwithoutDim;
47 Kernel(
int type = 0,
double param = 0);
88 void recompute(){ this->computed =
false;}
94 typename FunctionType<T>::Type f =
nullptr);
115 double function(std::shared_ptr<Point< T > > one, std::shared_ptr<Point< T > > two,
int dim,
116 typename FunctionType<T>::Type f =
nullptr)
const;
139 size_t size()
const {
return K.size(); }
143 return function(make_point<T>(a), make_point<T>(b), a.
size());
146 void operator()(
const Data<T>& samples,
typename FunctionType<T>::Type f =
nullptr){
147 if(this->type == mltk::CUSTOM){
148 assert(f &&
"A function must be provided for a custom kernel.");
150 compute(make_data<T>(samples), f);
153 std::vector<double> operator[](
const size_t& idx)
const {
154 assert(idx < K.size());
159 template <
typename T >
161 size_t i, j, size = samples->size(), dim = samples->dim();
164 if(this->type == mltk::CUSTOM) this->func = f;
165 K.assign(size, std::vector<double>(size, 0.0));
168 for(i = 0; i < size; ++i){
169 for(j = i; j < size; ++j){
170 K[i][j] =
function((*samples)[i], (*samples)[j], dim, f);
177 template <
typename T >
180 size_t size = samples->size(), dim = samples->dim();
182 H.resize(size, std::vector<double>(size));
185 for(i = 0; i < size; ++i) {
186 for (j = i; j < size; ++j) {
187 H[i][j] =
function(samples->point(i), samples->point(j), dim) * samples->point(i)->Y() *
188 samples->point(j)->Y();
195 template <
typename T >
198 size_t size = samples->size();
200 for(
auto& row: HwithoutDim){
204 HwithoutDim = mltk::dMatrix(size, std::vector<double>(size, 0.0));
207 for(i = 0; i < size; ++i) {
208 for (j = i; j < size; ++j) {
209 HwithoutDim[i][j] = functionWithoutDim(samples->point(i), samples->point(j), dim, samples->dim());
210 HwithoutDim[j][i] = HwithoutDim[i][j]*samples->point(i)->Y() * samples->point(j)->Y();
217 template <
typename T >
219 typename FunctionType<T>::Type f)
const{
222 std::vector< T > a = one->
X(), b = two->
X();
229 case mltk::INNER_PRODUCT:
230 for(i = 0; i < dim; ++i)
233 case mltk::POLYNOMIAL:
234 for(i = 0; i < dim; ++i)
237 sum = (param > 1) ? std::pow(sum, param) : sum;
241 for(i = 0; i < dim; ++i)
242 { t = a[i] - b[i]; sum += t * t; }
243 sum = std::exp(-1 * sum * param);
247 if(f) sum = f(*one, *two, param);
248 else sum = this->func(*one, *two, param);
258 template <
typename T >
265 case mltk::INNER_PRODUCT:
266 for(i = 0; i < dim; ++i)
268 sum += (*one)[i] * (*two)[i];
271 case mltk::POLYNOMIAL:
272 for(i = 0; i < dim; ++i)
274 sum += (*one)[i] * (*two)[i];
275 sum = (param > 1) ? std::pow(sum+1, param) : sum;
279 for(i = 0; i < dim; ++i) {
281 t = (*one)[i] - (*two)[i];
285 sum = std::exp(-1 * sum * param);
288 sum = this->func(*one, *two, param);
296 template <
typename T >
298 size_t i, j, size = data.
size();
300 auto points = data.
points();
304 for(i = 0; i < size; ++i){
305 for(j = 0; j < size; j++){
306 sum1 += points[j]->Alpha() * points[j]->Y() * K[i][j];
307 sum += points[i]->Y() * points[i]->Alpha() * sum1;
314 template <
typename T >
316 size_t i = 0, j = 0, size = data->size();
320 for(i = 0; i < size; ++i)
322 if((*data)[i]->Alpha() > 0)
325 for(j = 0; j < size; ++j)
327 if((*data)[j]->Alpha() > 0)
328 sum1 += (*data)[j]->Y() * (*data)[j]->Alpha() * K[j][i];
330 sum += (*data)[i]->Alpha() * (*data)[i]->Y() * sum1;
338 template <
typename T >
344 template <
typename T >
346 this->K = std::move(kernel_matrix);
349 template <
typename T >
354 template <
typename T >
359 template <
typename T >
362 this->computed =
false;
365 template <
typename T >
367 this->param = _param;
368 this->computed =
false;
371 template <
typename T >
373 this->K = std::move(_K);
376 template <
typename T >
381 template <
typename T >
size_t size() const
Returns the size of the dataset.
Definition: Data.hpp:208
std::vector< std::shared_ptr< Point< T > > > points()
Returns a shared pointer to the vector of Points of the sample.
Definition: Data.hpp:1685
Class for the kernel computations.
Definition: Kernel.hpp:22
void setParam(double param)
setParam Set the kernel parameter used in the kernel computations.
Definition: Kernel.hpp:366
mltk::dMatrix * getKernelMatrixPointer()
getKernelMatrixPointer Returns a reference to the kernel matrix.
Definition: Kernel.hpp:382
mltk::dMatrix getKernelMatrix()
getKernelMatrix Get the kernel matrix.
Definition: Kernel.hpp:377
Kernel(mltk::dMatrix kernel_matrix)
Class constructor.
Definition: Kernel.hpp:345
void setType(int type)
setType Set the kernel type used in the kernel computations.
Definition: Kernel.hpp:360
int getType()
getType Returns the kernel type used in the kernel computations.
Definition: Kernel.hpp:350
double getParam()
getParam Returns the kernel parameter used in the kernel computations.
Definition: Kernel.hpp:355
void setKernelMatrix(mltk::dMatrix K)
setKernelMatrix Set a pre computed kernel matrix.
Definition: Kernel.hpp:372
double functionWithoutDim(std::shared_ptr< Point< T > > one, std::shared_ptr< Point< T > > two, int j, int dim)
function Compute the kernel function between two points without a dimension.
Definition: Kernel.hpp:259
void compute(std::shared_ptr< Data< T > > samples, typename FunctionType< T >::Type f=nullptr)
compute Compute the kernel matrix with the given type and parameter.
Definition: Kernel.hpp:160
double featureSpaceNorm(std::shared_ptr< Data< T > > data)
featureSpaceNorm Computes the norm in the feature space (Dual).
Definition: Kernel.hpp:315
mltk::dMatrix * generateMatrixHwithoutDim(std::shared_ptr< Data< T > > samples, int dim)
compute Compute the H matrix without a dimension, with the computed kernel matrix and given samples.
Definition: Kernel.hpp:196
double norm(Data< T > data)
norm Computes norm in dual variables.
Definition: Kernel.hpp:297
Kernel(int type=0, double param=0)
Class constructor.
Definition: Kernel.hpp:339
mltk::dMatrix * generateMatrixH(std::shared_ptr< Data< T > > samples)
compute Compute the H matrix with the computed kernel matrix and given samples.
Definition: Kernel.hpp:178
Wrapper for the point data.
Definition: Point.hpp:42
Rep const & X() const
Returns the attributes representation of the point (std::vector by default).
Definition: Point.hpp:139
std::size_t size() const
Returns the dimension of the point.
Definition: Point.hpp:133
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11