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

RecursiveRLSWrapper is the sub-class of GurlsWrapper that implements recursive update of the RLS estimator with retraining capability. More...

#include <recrlswrapper.h>

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

List of all members.

Public Types

enum  ProblemType { CLASSIFICATION, REGRESSION }

Public Member Functions

virtual T eval (const gVec< T > &X, unsigned long *index=NULL)
 Estimates label for a new input point.
gMat2D< T > * eval (const gMat2D< T > &X)
 Estimates label for an input matrix.
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.
 RecursiveRLSWrapper (const std::string &name)
 Constructor.
void retrain ()
 Selection of the new regularization parameter.
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.
void update (const gVec< T > &X, const gVec< T > &y)
 Estimator update.

Protected Member Functions

virtual bool trainedModel ()
 Checks if model has already been trained.

Protected Attributes

std::string name
 Name of the options structure.
unsigned long nTot
 Total number of samples used for training.
GurlsOptionsListopt
 Options structure where information about initial training is stored.
ProblemType probType
 Problem type.

Detailed Description

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

Initial parameter selection and training are carried out on a initial set of samples via method train() which stores all information necessary for efficient recursive update in the options structure. Once the information about initial training is stored, given a new input-output pair, the RLS estimator can be efficiently updated via the method update(). Every time a new input-output pair is available, method update() can be invoked again. Parameter selection and RLS estimation ( method retrain()) can be repeated after any number of online updates. Finally, the eval() method estimates the output for new data.

Definition at line 63 of file recrlswrapper.h.


Constructor & Destructor Documentation

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

Definition at line 9 of file recrlswrapper.hpp.

                                                                : GurlsWrapper<T>(name)
{
    this->opt->template getOptValue<OptNumber>("nholdouts") = 1.0;
}

Member Function Documentation

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 >
gMat2D< T > * gurls::RecursiveRLSWrapper< T >::eval ( const gMat2D< T > &  X) [virtual]
Parameters:
[in]XInput matrix
Returns:
Matrix of predicted labels

Implements gurls::GurlsWrapper< T >.

Definition at line 156 of file recrlswrapper.hpp.

{
    if(!this->trainedModel())
        throw gException("Error, Train Model First");

    gurls::PredPrimal<T> predTask;
    gMat2D<T> y;

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

    gMat2D<T>& pred_mat = result->getValue();
    result->detachValue();
    delete result;

    return &pred_mat;
}
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::RecursiveRLSWrapper< T >::retrain ( )

Selection is performed via hold-out validation using the subset Xva,yva of the total training set as validation set.

Definition at line 174 of file recrlswrapper.hpp.

{
    GurlsOptionsList* kernel = this->opt->template getOptAs<GurlsOptionsList>("kernel");
    const gMat2D<T> &Xva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xva");
    const gMat2D<T> &yva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("yva");

    const unsigned long nva = Xva.rows();

    this->opt->removeOpt("paramsel");
    this->opt->removeOpt("optimizer");

    GurlsOptionsList* split = this->opt->template getOptAs<GurlsOptionsList>("split");
    split->removeOpt("indices");
    split->removeOpt("lasts");


    gMat2D<unsigned long>* indices = new gMat2D<unsigned long>(1, nTot);
    gMat2D<unsigned long>* lasts = new gMat2D<unsigned long>(1, 1);

    set(indices->getData(), 0ul, nTot-nva);
    unsigned long * it = indices->getData() + (nTot-nva);
    for(unsigned long i=0; i<nva; ++i, ++it)
        *it = i;

    lasts->getData()[0] = (nTot-nva);

    split->addOpt("indices", new OptMatrix<gMat2D<unsigned long> >(*indices));
    split->addOpt("lasts", new OptMatrix<gMat2D<unsigned long> >(*lasts));


    ParamSelHoPrimal<T> paramselTask;
    this->opt->addOpt("paramsel", paramselTask.execute(Xva, yva, *(this->opt)));


    RLSPrimalRecInit<T> optimizerTask;
    gMat2D<T> emptyMat;
    this->opt->addOpt("nTot", new OptNumber(nTot));
    this->opt->addOpt("optimizer", optimizerTask.execute(emptyMat, emptyMat, *(this->opt)));
    this->opt->removeOpt("nTot");

}
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::RecursiveRLSWrapper< T >::train ( const gMat2D< T > &  X,
const gMat2D< T > &  y 
) [virtual]
Parameters:
[in]XInput data matrix
[in]YLabels matrix

Implements gurls::GurlsWrapper< T >.

Definition at line 15 of file recrlswrapper.hpp.

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


    SplitHo<T> splitTask;
    GurlsOptionsList* split = splitTask.execute(X, y, *(this->opt));
    this->opt->addOpt("split", split);


    const gMat2D<unsigned long>& split_indices = split->getOptValue<OptMatrix<gMat2D<unsigned long> > >("indices");
    const gMat2D<unsigned long>& split_lasts = split->getOptValue<OptMatrix<gMat2D<unsigned long> > >("lasts");

    const unsigned long n = X.rows();
    const unsigned long d = X.cols();
    const unsigned long t = y.cols();

    const unsigned long last = split_lasts.getData()[0];
    const unsigned long nva = n-last;

    unsigned long* va = new unsigned long[nva];
    copy(va, split_indices.getData()+last, nva);

    gMat2D<T>* Xva = new gMat2D<T>(nva, d);
    gMat2D<T>* yva = new gMat2D<T>(nva, t);

    subMatrixFromRows(X.getData(), n, d, va, nva, Xva->getData());
    subMatrixFromRows(y.getData(), n, t, va, nva, yva->getData());

    gMat2D<T>* XtX = new gMat2D<T>(d, d);
    gMat2D<T>* Xty = new gMat2D<T>(d, t);


    dot(X.getData(), X.getData(), XtX->getData(), n, d, n, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor);
    dot(X.getData(), y.getData(), Xty->getData(), n, d, n, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);


    GurlsOptionsList* kernel = new GurlsOptionsList("kernel");
    kernel->addOpt("XtX", new OptMatrix<gMat2D<T> >(*XtX));
    kernel->addOpt("Xty", new OptMatrix<gMat2D<T> >(*Xty));

    kernel->addOpt("Xva", new OptMatrix<gMat2D<T> >(*Xva));
    kernel->addOpt("yva", new OptMatrix<gMat2D<T> >(*yva));

    nTot = n;
    this->opt->addOpt("kernel", kernel);

    ParamSelHoPrimal<T> paramselTask;
    this->opt->addOpt("paramsel", paramselTask.execute(X, y, *(this->opt)));


    RLSPrimalRecInit<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");
}
template<typename T >
void gurls::RecursiveRLSWrapper< T >::update ( const gVec< T > &  X,
const gVec< T > &  y 
)
Parameters:
[in]XInput data vector
[in]YLabels vector

Definition at line 74 of file recrlswrapper.hpp.

{
    if(!this->trainedModel())
        throw gException("Error, Train Model First");

    RLSPrimalRecUpdate<T> optimizer;

    const unsigned long d = X.getSize();
    const unsigned long t = y.getSize();

    gMat2D<T>X_mat(1, d);
    copy(X_mat.getData(), X.getData(), d);
    gMat2D<T>y_mat(1, t);
    copy(y_mat.getData(), y.getData(), t);

    GurlsOptionsList* ret = optimizer.execute(X_mat, y_mat, *(this->opt));
    this->opt->removeOpt("optimizer");
    this->opt->addOpt("optimizer", ret);

    ++nTot;

    gMat2D<T>* xtx = new gMat2D<T>(d,d);
    gMat2D<T>* xty = new gMat2D<T>(d,t);

    dot(X.getData(), X.getData(), xtx->getData(), 1, d, 1, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor);
    dot(X.getData(), y.getData(), xty->getData(), 1, d, 1, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);

    GurlsOptionsList* kernel = this->opt->template getOptAs<GurlsOptionsList>("kernel");

    const gMat2D<T>& XtX = kernel->getOptValue<OptMatrix<gMat2D<T> > >("XtX");
    const gMat2D<T>& Xty = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xty");

    axpy(d*d, (T)1.0, XtX.getData(), 1, xtx->getData(), 1);
    axpy(d*t, (T)1.0, Xty.getData(), 1, xty->getData(), 1);

    kernel->removeOpt("XtX");
    kernel->addOpt("XtX", new OptMatrix<gMat2D<T> >(*xtx));

    kernel->removeOpt("Xty");
    kernel->addOpt("Xty", new OptMatrix<gMat2D<T> >(*xty));


    unsigned long proportion = static_cast<unsigned long>(gurls::round(1.0/this->opt->getOptAsNumber("hoproportion")));

    if(nTot % proportion == 0)
    {
        const gMat2D<T>& Xva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xva");
        const gMat2D<T>& yva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("yva");

        const unsigned long nva = Xva.rows();
        const unsigned long nva_new = nva+1;


        gMat2D<T>* Xva_new = new gMat2D<T>(nva_new, d);

        const T* old_it = Xva.getData();
        T* new_it = Xva_new->getData();
        for(const T* end = new_it+(nva_new*d); new_it< end; old_it+=nva, new_it +=nva_new)
            copy(new_it, old_it, nva);

        copy(Xva_new->getData()+nva, X.getData(), d, nva_new, 1);

        kernel->removeOpt("Xva");
        kernel->addOpt("Xva", new OptMatrix<gMat2D<T> >(*Xva_new));


        gMat2D<T>* yva_new = new gMat2D<T>(nva_new, t);

        old_it = yva.getData();
        new_it = yva_new->getData();
        for(const T* end = new_it+(nva_new*t); new_it< end; old_it+=nva, new_it +=nva_new)
            copy(new_it, old_it, nva);

        copy(yva_new->getData()+nva, y.getData(), t, nva_new, 1);

        kernel->removeOpt("yva");
        kernel->addOpt("yva", new OptMatrix<gMat2D<T> >(*yva_new));

    }
}

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