GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
gurls::RLSWrapper< T > Class Template Reference

RLSWrapper is the sub-class of GurlsWrapper that implements Regularized Least Squares with a linear model. More...

#include <rlswrapper.h>

Inheritance diagram for gurls::RLSWrapper< T >:
Collaboration diagram for gurls::RLSWrapper< T >:

List of all members.

Public Types

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 GurlsOptionsListgetOpt () const
 Returns a const reference to the options structure.
virtual void loadModel (const std::string &fileName)
 Loads a computed model from a file.
 RLSWrapper (const std::string &name)
 Constructor.
virtual void saveModel (const std::string &fileName)
 Saves the computed model to file.
virtual void setNparams (unsigned long value)
virtual void setParam (double value)
virtual void setProblemType (ProblemType 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

std::string name
 Name of the options structure.
GurlsOptionsListopt
 Options structure where information about initial training is stored.
ProblemType probType
 Problem type.

Detailed Description

template<typename T>
class gurls::RLSWrapper< T >

The regularization 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 58 of file rlswrapper.h.


Constructor & Destructor Documentation

template<typename T >
gurls::RLSWrapper< T >::RLSWrapper ( const std::string &  name)
Parameters:
nameName of the option's structure that will be initialized

Definition at line 12 of file rlswrapper.hpp.

: GurlsWrapper<T>(name) { }

Member Function Documentation

template<typename T >
gMat2D< T > * gurls::RLSWrapper< T >::eval ( const gMat2D< T > &  X) [virtual]
Parameters:
[in]XInput matrix
Returns:
Matrix of predicted labels

Implements gurls::GurlsWrapper< T >.

Reimplemented in gurls::RandomFeaturesWrapper< T >.

Definition at line 42 of file rlswrapper.hpp.

{
    PredPrimal<T> predTask;
    gMat2D<T> empty;
    OptMatrix<gMat2D<T> >* result = predTask.execute(X, empty, *(this->opt));

    gMat2D<T>* pred = &(result->getValue());

    result->detachValue();
    delete result;

    return pred;
}
template<typename T >
T gurls::GurlsWrapper< T >::eval ( const gVec< T > &  X,
unsigned long *  index = NULL 
) [virtual, inherited]
Parameters:
[in]XInput point
[out]indexIndex 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;
}
template<typename T >
void gurls::GurlsWrapper< T >::loadModel ( const std::string &  fileName) [virtual, inherited]
Parameters:
fileNamename of the file containing the data to load

Definition at line 61 of file wrapper.hpp.

{
    opt->load(fileName);
}
template<typename T >
void gurls::GurlsWrapper< T >::saveModel ( const std::string &  fileName) [virtual, inherited]
Parameters:
fileNamename of the file where data will be saved

Definition at line 55 of file wrapper.hpp.

{
    opt->save(fileName);
}
template<typename T >
void gurls::GurlsWrapper< T >::setNparams ( unsigned long  value) [virtual, inherited]
Parameters:
[in]value

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");
    }
}
template<typename T >
void gurls::GurlsWrapper< T >::setParam ( double  value) [virtual, inherited]
Parameters:
[in]value

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);
}
template<typename T >
void gurls::GurlsWrapper< T >::setProblemType ( typename GurlsWrapper< T >::ProblemType  value) [virtual, inherited]
Parameters:
[in]value

Definition at line 103 of file wrapper.hpp.

{
    probType = value;

    opt->getOptValue<OptString>("hoperf") = (value == CLASSIFICATION)? "macroavg": "rmse";
}
template<typename T >
void gurls::GurlsWrapper< T >::setSplitProportion ( double  value) [virtual, inherited]
Parameters:
[in]value

Definition at line 97 of file wrapper.hpp.

{
    opt->getOptValue<OptNumber>("hoproportion") = value;
}
template<typename T >
void gurls::RLSWrapper< T >::train ( const gMat2D< T > &  X,
const gMat2D< T > &  y 
) [virtual]
Parameters:
[in]XInput data matrix
[in]YLabels matrix

Implements gurls::GurlsWrapper< T >.

Reimplemented in gurls::RandomFeaturesWrapper< T >.

Definition at line 15 of file rlswrapper.hpp.

{
    this->opt->removeOpt("split");
    this->opt->removeOpt("optimizer");

    const unsigned long nlambda = static_cast<unsigned long>(this->opt->getOptAsNumber("nlambda"));
    if(nlambda != 1)
    {
        SplitHo<T> splitTask;
        this->opt->addOpt("split", splitTask.execute(X, y, *(this->opt)));

        ParamSelHoPrimal<T> paramselTask;
        GurlsOption * result = paramselTask.execute(X, y, *(this->opt));
        this->opt->removeOpt("paramsel");
        this->opt->addOpt("paramsel", result);
    }
    else
    {
        if(!this->opt->hasOpt("paramsel.lambdas"))
            throw gException("Please set a valid value for the regularization parameter, calling setParam(value)");
    }

    RLSPrimal<T> optimizerTask;
    this->opt->addOpt("optimizer", optimizerTask.execute(X, y, *(this->opt)));
}
template<typename T >
bool gurls::GurlsWrapper< T >::trainedModel ( ) [protected, virtual, inherited]
Parameters:
value
value

Definition at line 111 of file wrapper.hpp.

{
    return opt->hasOpt("optimizer");
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends