UFJF - Machine Learning Toolkit  0.51.8
DualClassifier.hpp
Go to the documentation of this file.
1 
6 #ifndef DUALCLASSIFIER__HPP
7 #define DUALCLASSIFIER__HPP
8 
10 #include "Classifier.hpp"
11 #include <vector>
12 
13 namespace mltk{
14  namespace classifier {
15  template<typename T>
16  class DualClassifier : virtual public Classifier<T> {
17  // Associations
18  // Attributes
19  protected:
21  std::vector<double> alpha;
22  KernelType kernel_type = KernelType::INNER_PRODUCT;
23  double kernel_param = 0;
25  Kernel<T> *kernel = nullptr;
26  public:
27 
28  virtual double evaluate(const Point <T> &p, bool raw_value = false) override {
29  double func, bias = this->solution.bias, fk = 0.0, lambda;
30  size_t size = this->samples->size(), dim = this->samples->dim(), r;
31  auto po = std::make_shared<Point<T> >(p);
32 
33  if (p.X().size() != dim) {
34  std::cerr << "The point must have the same dimension of the feature set!" << std::endl;
35  return 0;
36  }
37 
38  for (func = bias, r = 0; r < size; ++r) {
39  fk = this->kernel->function(po, (*this->samples)[r], dim);
40  func += this->solution.alpha[r] * (*this->samples)[r]->Y() * fk;
41  }
42 
43  return (func >= 0) ? 1 : -1;
44  }
45 
46  inline void recomputeKernel(){ if(this->kernel) this->kernel->recompute(); }
47 
48  /*********************************************
49  * Setters *
50  *********************************************/
51 
56  inline void setKernel(Kernel<T> *K) { this->kernel = K; }
57 
62  inline void setKernelType(KernelType type) {
63  this->kernel_type = type;
64  if (this->kernel) this->kernel->setType(type);
65  }
66 
71  inline void setKernelParam(double param) {
72  this->kernel_param = param;
73  if (this->kernel) kernel->setParam(param);
74  }
75 
76  /*********************************************
77  * Getters *
78  *********************************************/
79 
80  virtual std::string getFormulationString() override { return "Dual"; }
81 
82  inline Kernel<T> *getKernel() { return kernel; }
83 
88  inline double getKernelParam() { return kernel_param; }
89 
94  inline KernelType getKernelType() { return kernel_type; }
95 
100  inline std::vector<double> getAlphaVector() { return alpha; }
101 
107  size_t i, j, dim = this->samples->dim(), size = this->samples->size();
108  std::vector<double> w(dim);
109 
110  for (i = 0; i < dim; i++) {
111  for (j = 0; j < size; j++) {
112  w[i] += (*this->samples)[j]->Alpha() * (*this->samples)[j]->Y() * (*this->samples)[j]->X()[i];
113  }
114  }
115 
116  return w;
117  }
118 
124  int i = 0, j = 0, k = 0;
125  size_t size = this->samples->size(), dim = this->samples->dim();
126  dMatrix *H, *Hk, matrixdif(size, std::vector<double>(size));
127  std::vector<double> alphaaux(size);
128 
129  H = kernel->generateMatrixH(this->samples);
130 
131  this->solution.w.resize(dim);
132 
133  for (k = 0; k < dim; ++k) {
134  Hk = kernel->generateMatrixHwithoutDim(this->samples, k);
135 
136  for (i = 0; i < size; ++i)
137  for (j = 0; j < size; ++j)
138  matrixdif[i][j] = (*H)[i][j] - (*Hk)[i][j];
139 
140  for (i = 0; i < size; ++i)
141  for (alphaaux[i] = 0, j = 0; j < size; ++j)
142  alphaaux[i] += this->samples->point(j)->Alpha() * matrixdif[i][j];
143 
144  for (this->solution.w[k] = 0, i = 0; i < size; ++i)
145  this->solution.w[k] += alphaaux[i] * this->samples->point(i)->Alpha();
146  }
147 
148  return this->solution.w;
149  }
150 
156  int i = 0, j = 0, k = 0;
157  size_t size = this->samples->size(), dim = this->samples->dim();
158  std::vector<double> alphaaux(size);
159  dMatrix H(size, std::vector<double>(size));
160 
161  this->solution.w.resize(dim);
162 
163  for (k = 0; k < dim; ++k) {
164  for (i = 0; i < size; ++i)
165  for (j = 0; j < size; ++j)
166  H[i][j] = (*this->samples)[i]->X()[k] * (*this->samples)[j]->X()[k]
167  * (*this->samples)[i]->Y() * (*this->samples)[j]->Y();
168  if (this->verbose >= 3) std::clog << "\n H matrix without dim generated.\n";
169  for (i = 0; i < size; ++i)
170  for (alphaaux[i] = 0, j = 0; j < size; ++j)
171  alphaaux[i] += this->samples->point(j)->Alpha() * H[i][j];
172 
173  for (this->solution.w[k] = 0, i = 0; i < size; ++i)
174  this->solution.w[k] += alphaaux[i] * this->samples->point(i)->Alpha();
175  }
176 
177  return this->solution.w;
178  }
179  };
180  }
181 }
182 #endif
void setParam(double param)
setParam Set the kernel parameter used in the kernel computations.
Definition: Kernel.hpp:366
double function(std::shared_ptr< Point< T > > one, std::shared_ptr< Point< T > > two, int dim, typename FunctionType< T >::Type f=nullptr) const
function Compute the kernel function between two points.
Definition: Kernel.hpp:218
void setType(int type)
setType Set the kernel type used in the kernel computations.
Definition: Kernel.hpp:360
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
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
std::shared_ptr< Data< T > > samples
Samples used in the model training.
Definition: Learner.hpp:21
int verbose
Verbose level of the output.
Definition: Learner.hpp:42
Rep const & X() const
Returns the attributes representation of the point (std::vector by default).
Definition: Point.hpp:139
double bias
Bias of the solution.
Definition: Solution.hpp:23
mltk::Point< double > w
Weights vector.
Definition: Solution.hpp:17
std::vector< double > alpha
Alpha Vector for Dual methods.
Definition: Solution.hpp:21
Definition: classifier/Classifier.hpp:17
Solution solution
Classifier solution.
Definition: classifier/Classifier.hpp:25
Definition: DualClassifier.hpp:16
std::vector< double > alpha
Alphas vector.
Definition: DualClassifier.hpp:21
virtual double evaluate(const Point< T > &p, bool raw_value=false) override
Returns the class of a feature point based on the trained Learner.
Definition: DualClassifier.hpp:28
std::vector< double > getAlphaVector()
Get the vector of alphas.
Definition: DualClassifier.hpp:100
void setKernelType(KernelType type)
Set the type of the kernel.
Definition: DualClassifier.hpp:62
double getKernelParam()
Get the parameter of the kernel.
Definition: DualClassifier.hpp:88
KernelType getKernelType()
Get the type of the kernel.
Definition: DualClassifier.hpp:94
void setKernelParam(double param)
Set the parameter of the kernel.
Definition: DualClassifier.hpp:71
Point< double > getDualWeightProdInt()
Compute the weights with inner product of the dual classifier.
Definition: DualClassifier.hpp:155
Kernel< T > * kernel
Object for kernel computations.
Definition: DualClassifier.hpp:25
Point< double > getDualWeight()
Compute the weights of the dual classifier (with H matrix).
Definition: DualClassifier.hpp:123
Point< double > getWeight()
getWeight Get the vector of weights when the linear kernel is used.
Definition: DualClassifier.hpp:106
virtual std::string getFormulationString() override
getFormulationString Returns a string that represents the formulation of the learner (Primal or Dual)...
Definition: DualClassifier.hpp:80
void setKernel(Kernel< T > *K)
setKernel Set the kernel used by the dual classifier.
Definition: DualClassifier.hpp:56
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11