KernelRLSWrapper is the sub-class of GurlsWrapper that implements Regularized Least Squares with a possibly non-linear model by resorting to kernel methods.
More...
#include <kernelrlswrapper.h>
List of all members.
Public Types |
enum | KernelType { RBF,
LINEAR
} |
enum | ProblemType { CLASSIFICATION,
REGRESSION
} |
Public Member Functions |
gMat2D< T > * | eval (const gMat2D< T > &X) |
| Estimates label for an input matrix.
|
virtual T | eval (const gVec< T > &X, unsigned long *index=NULL) |
| Estimates label for a new input point.
|
virtual const GurlsOptionsList & | getOpt () const |
| Returns a const reference to the options structure.
|
| KernelRLSWrapper (const std::string &name) |
| Constructor.
|
virtual void | loadModel (const std::string &fileName) |
| Loads a computed model from a file.
|
virtual void | saveModel (const std::string &fileName) |
| Saves the computed model to file.
|
virtual void | setKernelType (KernelType value) |
virtual void | setNparams (unsigned long value) |
virtual void | setNSigma (unsigned long value) |
virtual void | setParam (double value) |
virtual void | setProblemType (ProblemType value) |
virtual void | setSigma (double value) |
virtual void | setSplitProportion (double value) |
void | train (const gMat2D< T > &X, const gMat2D< T > &y) |
| Initial parameter selection and training.
|
Protected Member Functions |
virtual bool | trainedModel () |
| Checks if model has already been trained.
|
Protected Attributes |
KernelType | kType |
| Kernel type used in train and eval.
|
std::string | name |
| Name of the options structure.
|
GurlsOptionsList * | opt |
| Options structure where information about initial training is stored.
|
ProblemType | probType |
| Problem type.
|
Detailed Description
template<typename T>
class gurls::KernelRLSWrapper< T >
The regularization parameter (and the kernel parameter) is estimated by the wrapper (default) or explicitely given in input by the user via method train(). The eval() method estimates the output for new data. *
Definition at line 57 of file kernelrlswrapper.h.
Constructor & Destructor Documentation
- Parameters:
-
name | Name of the option's structure that will be initialized |
Definition at line 13 of file kernelrlswrapper.hpp.
: KernelWrapper<T>(name) { }
Member Function Documentation
- Parameters:
-
- Returns:
- Matrix of predicted labels
Implements gurls::GurlsWrapper< T >.
Definition at line 103 of file kernelrlswrapper.hpp.
{
Prediction<T> *pred;
PredKernelTrainTest<T> predkTrainTest;
gMat2D<T> empty;
switch (this->kType)
{
case KernelWrapper<T>::LINEAR:
pred = new PredPrimal<T>();
break;
case KernelWrapper<T>::RBF:
pred = new PredDual<T>();
this->opt->removeOpt("predkernel");
this->opt->addOpt("predkernel", predkTrainTest.execute(X, empty, *(this->opt)));
}
OptMatrix<gMat2D<T> >* result = OptMatrix<gMat2D<T> >::dynacast(pred->execute(X, empty, *(this->opt)));
result->detachValue();
delete pred;
gMat2D<T>* ret = &(result->getValue());
delete result;
return ret;
}
- Parameters:
-
[in] | X | Input point |
[out] | index | Index of the estimated label |
- Returns:
- Estimated label
Definition at line 26 of file wrapper.hpp.
{
if(!trainedModel())
throw gException("Error, Train Model First");
gMat2D<T>X_mat(1, X.getSize());
copy(X_mat.getData(), X.getData(), X.getSize());
gMat2D<T>* pred_mat = eval(X_mat);
const T* pred = pred_mat->getData();
const unsigned long size = pred_mat->getSize();
const T* max = std::max_element(pred, pred+size);
T ret = *max;
if(index != NULL)
*index = max-pred;
delete pred_mat;
return ret;
}
- Parameters:
-
fileName | name of the file containing the data to load |
Definition at line 61 of file wrapper.hpp.
- Parameters:
-
fileName | name of the file where data will be saved |
Definition at line 55 of file wrapper.hpp.
- Parameters:
-
Definition at line 67 of file wrapper.hpp.
{
opt->getOptValue<OptNumber>("nlambda") = value;
if(opt->hasOpt("paramsel.lambdas") && value > 1.0)
{
std::cout << "Warning: ignoring previous values of the regularization parameter" << std::endl;
opt->getOptAs<GurlsOptionsList>("paramsel")->removeOpt("lambdas");
}
}
- Parameters:
-
Definition at line 161 of file wrapper.hpp.
{
this->opt->template getOptValue<OptNumber>("nsigma") = value;
if(this->opt->hasOpt("paramsel.sigma") && value > 1.0)
{
std::cout << "Warning: ignoring previous values of the kernel parameter" << std::endl;
this->opt->template getOptAs<GurlsOptionsList>("paramsel")->removeOpt("sigma");
}
}
- Parameters:
-
Reimplemented in gurls::NystromWrapper< T >.
Definition at line 79 of file wrapper.hpp.
{
if(!opt->hasOpt("paramsel"))
opt->addOpt("paramsel", new GurlsOptionsList("paramsel"));
if(opt->hasOpt("paramsel.lambdas"))
opt->getOptValue<OptMatrix<gMat2D<T> > >("paramsel.lambdas").getData()[0] = (T)value;
else
{
gMat2D<T> * lambdas = new gMat2D<T>(1,1);
lambdas->getData()[0] = (T)value;
opt->getOptAs<GurlsOptionsList>("paramsel")->addOpt("lambdas", new OptMatrix<gMat2D<T> >(*lambdas));
}
setNparams(1);
}
- Parameters:
-
Definition at line 147 of file wrapper.hpp.
{
if(this->opt->hasOpt("paramsel.sigma"))
this->opt->template getOptValue<OptNumber>("paramsel.sigma") = value;
else
{
GurlsOptionsList* paramsel = this->opt->template getOptAs<GurlsOptionsList>("paramsel");
paramsel->addOpt("sigma", new OptNumber(value));
}
setNSigma(1);
}
- Parameters:
-
X | Input data matrix |
Y | Labels matrix |
Implements gurls::KernelWrapper< T >.
Definition at line 16 of file kernelrlswrapper.hpp.
{
this->opt->removeOpt("split");
this->opt->removeOpt("optimizer");
const unsigned long nlambda = static_cast<unsigned long>(this->opt->getOptAsNumber("nlambda"));
const unsigned long nsigma = static_cast<unsigned long>(this->opt->getOptAsNumber("nsigma"));
OptTaskSequence *seq = new OptTaskSequence();
GurlsOptionsList * process = new GurlsOptionsList("processes", false);
OptProcess* process1 = new OptProcess();
process->addOpt("one", process1);
this->opt->addOpt("seq", seq);
this->opt->addOpt("processes", process);
if(this->kType == KernelWrapper<T>::LINEAR)
{
if(nlambda > 1ul)
{
*seq << "split:ho" << "kernel:linear" << "paramsel:hodual";
*process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave;
}
else if(nlambda == 1ul)
{
if(this->opt->hasOpt("paramsel.lambdas"))
{
*seq << "kernel:linear";
*process1 << GURLS::computeNsave;
}
else
throw gException("Please set a valid value for the regularization parameter, calling setParam(value)");
}
else
throw gException("Please set a valid value for NParam, calling setNParam(value)");
}
else if(this->kType == KernelWrapper<T>::RBF)
{
if(nlambda > 1ul)
{
if(nsigma > 1ul)
{
*seq << "split:ho" << "paramsel:siglamho" << "kernel:rbf";
*process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave;
}
else if(nsigma == 1ul)
{
if(this->opt->hasOpt("paramsel.sigma"))
{
*seq << "split:ho" << "kernel:rbf" << "paramsel:hodual";
*process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave;
}
else
throw gException("Please set a valid value for the kernel parameter, calling setSigma(value)");
}
else
throw gException("Please set a valid value for NSigma, calling setNSigma(value)");
}
else if(nlambda == 1ul)
{
if(nsigma == 1ul)
{
if(this->opt->hasOpt("paramsel.sigma") && this->opt->hasOpt("paramsel.lambdas"))
{
*seq << "split:ho" << "kernel:rbf";
*process1 << GURLS::computeNsave << GURLS::computeNsave;
}
else
throw gException("Please set a valid value for kernel and regularization parameters, calling setParam(value) and setSigma(value)");
}
else
throw gException("Please set a valid value for NSigma, calling setNSigma(value)");
}
else
throw gException("Please set a valid value for NParam, calling setNParam(value)");
}
*seq << "optimizer:rlsdual";
*process1 << GURLS::computeNsave;
GURLS G;
G.run(X, y, *(this->opt), "one");
}
The documentation for this class was generated from the following files: