6 #ifndef DUALCLASSIFIER__HPP
7 #define DUALCLASSIFIER__HPP
10 #include "Classifier.hpp"
14 namespace classifier {
22 KernelType kernel_type = KernelType::INNER_PRODUCT;
23 double kernel_param = 0;
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);
33 if (p.
X().size() != dim) {
34 std::cerr <<
"The point must have the same dimension of the feature set!" << std::endl;
38 for (func = bias, r = 0; r < size; ++r) {
43 return (func >= 0) ? 1 : -1;
46 inline void recomputeKernel(){
if(this->kernel) this->kernel->recompute(); }
63 this->kernel_type = type;
64 if (this->kernel) this->kernel->
setType(type);
72 this->kernel_param = param;
107 size_t i, j, dim = this->
samples->dim(), size = this->
samples->size();
108 std::vector<double> w(dim);
110 for (i = 0; i < dim; i++) {
111 for (j = 0; j < size; j++) {
124 int i = 0, j = 0, k = 0;
126 dMatrix *H, *Hk, matrixdif(size, std::vector<double>(size));
127 std::vector<double> alphaaux(size);
133 for (k = 0; k < dim; ++k) {
136 for (i = 0; i < size; ++i)
137 for (j = 0; j < size; ++j)
138 matrixdif[i][j] = (*H)[i][j] - (*Hk)[i][j];
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];
144 for (this->
solution.
w[k] = 0, i = 0; i < size; ++i)
156 int i = 0, j = 0, k = 0;
158 std::vector<double> alphaaux(size);
159 dMatrix H(size, std::vector<double>(size));
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]
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];
173 for (this->
solution.
w[k] = 0, i = 0; i < size; ++i)
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