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

ParamSelLoocvDual is the sub-class of ParamSelection that implements LOO cross-validation with the dual formulation.

#include <loocvdual.h>

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

List of all members.

Public Member Functions

GurlsOptionsListexecute (const gMat2D< T > &X, const gMat2D< T > &Y, const GurlsOptionsList &opt)
 Performs parameter selection when the dual formulation of RLS is used.

Static Public Member Functions

static ParamSelection< T > * factory (const std::string &id) throw (BadParamSelectionCreation)
 Factory function returning a pointer to the newly created object.

Detailed Description

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

Definition at line 71 of file loocvdual.h.


Member Function Documentation

template<typename T >
GurlsOptionsList * gurls::ParamSelLoocvDual< T >::execute ( const gMat2D< T > &  X,
const gMat2D< T > &  Y,
const GurlsOptionsList opt 
) [virtual]

The leave-one-out approach is used.

Parameters:
Xinput data matrix
Ylabels matrix
optoptions with the following:
  • nlambda (default)
  • hoperf (default)
  • smallnumber (default)
  • kernel (settable with the class Kernel and its subclasses)
Returns:
paramsel, a GurlsOptionList with the following fields:
  • lambdas = array of values of the regularization parameter lambda minimizing the validation error for each class
  • guesses = array of guesses for the regularization parameter lambda
  • acc = matrix of validation accuracies for each lambda guess and for each class

Implements gurls::ParamSelection< T >.

Definition at line 94 of file loocvdual.h.

{
//    [n,T]  = size(y);
    const unsigned long n = Y.rows();
    const unsigned long t = Y.cols();

    const unsigned long d = X.cols();


//    tot = opt.nlambda;
    int tot = static_cast<int>(std::ceil( opt.getOptAsNumber("nlambda")));


//    [Q,L] = eig(opt.kernel.K);
//    Q = double(Q);
//    L = double(diag(L));
    const GurlsOptionsList* kernel = opt.getOptAs<GurlsOptionsList>("kernel");

    const gMat2D<T> &K_mat = kernel->getOptValue<OptMatrix<gMat2D<T> > >("K");

    gMat2D<T> K(K_mat.rows(), K_mat.cols());
    copy(K.getData(), K_mat.getData(), K_mat.getSize());

    const unsigned long qrows = K.rows();
    const unsigned long qcols = K.cols();
    const unsigned long l_length = qrows;

    T *Q = K.getData();
    T *L = new T[l_length];

    eig_sm(Q, L, qrows); // qrows == qcols

    int r = n;
    if(kernel->getOptAsString("type") == "linear")
    {
        set(L, (T) 1.0e-12, l_length-d);
        r = std::min(n,d);
    }


//    Qty = Q'*y;
//    T* Qty = new T[qrows*qcols];
    T* Qty = new T[qcols*t];
    dot(Q, Y.getData(), Qty, qrows, qcols, n, t, qcols, t, CblasTrans, CblasNoTrans, CblasColMajor);

    T* guesses = lambdaguesses(L, n, r, n, tot, (T)(opt.getOptAsNumber("smallnumber")));



    GurlsOptionsList* nestedOpt = new GurlsOptionsList("nested");

    gMat2D<T>* pred = new gMat2D<T>(n, t);
    OptMatrix<gMat2D<T> >* pred_opt = new OptMatrix<gMat2D<T> >(*pred);
    nestedOpt->addOpt("pred", pred_opt);


    Performance<T>* perfClass = Performance<T>::factory(opt.getOptAsString("hoperf"));

    gMat2D<T>* perf = new gMat2D<T>(tot, t);
    T* ap = perf->getData();

    T* C_div_Z = new T[qrows];
    T* C = new T[qrows*qcols];
    T* Z = new T[qrows];
    T* work = new T[std::max((qrows+1)*l_length, (qrows*qcols)+l_length)];

    for(int i = 0; i < tot; ++i)
    {
        rls_eigen(Q, L, Qty, C, guesses[i], n, qrows, qcols, l_length, qcols, t, work);
        GInverseDiagonal(Q, L, guesses+i, Z, qrows, qcols, l_length, 1, work);

        for(unsigned long j = 0; j< t; ++j)
        {
            rdivide(C + (qrows*j), Z, C_div_Z, qrows);

//            opt.pred(:,t) = y(:,t) - (C(:,t)./Z);
            copy(pred->getData()+(n*j), Y.getData() + (n*j), n);
            axpy(n, (T)-1.0, C_div_Z, 1, pred->getData() + (n*j), 1);
        }

//        opt.perf = opt.hoperf([],y,opt);
        const gMat2D<T> dummy;
        GurlsOptionsList* perf_opt = perfClass->execute(dummy, Y, *nestedOpt);

        gMat2D<T> &forho_vec = perf_opt->getOptValue<OptMatrix<gMat2D<T> > >("forho");

        copy(ap+i, forho_vec.getData(), t, tot, 1);

        delete perf_opt;
    }

    delete nestedOpt;
    delete[] work;
    delete [] C;
    delete [] Z;
    delete [] C_div_Z;
    delete perfClass;
    delete [] Qty;

    delete[] L;
    //delete[] Q;

    unsigned long* idx = new unsigned long[t];
    work = NULL;
    indicesOfMax(ap, tot, t, idx, work, 1);


    gMat2D<T> *LAMBDA = new gMat2D<T>(1, t);
    copyLocations(idx, guesses, t, tot, LAMBDA->getData());

    delete[] idx;


    GurlsOptionsList* paramsel;

    if(opt.hasOpt("paramsel"))
    {
        GurlsOptionsList* tmp_opt = new GurlsOptionsList("tmp");
        tmp_opt->copyOpt("paramsel", opt);

        paramsel = GurlsOptionsList::dynacast(tmp_opt->getOpt("paramsel"));
        tmp_opt->removeOpt("paramsel", false);
        delete tmp_opt;

        paramsel->removeOpt("guesses");
        paramsel->removeOpt("perf");
        paramsel->removeOpt("lambdas");
    }
    else
        paramsel = new GurlsOptionsList("paramsel");


//     opt.addOpt("lambdas", LAMBDA);
    paramsel->addOpt("lambdas", new OptMatrix<gMat2D<T> >(*LAMBDA));

    //vout.perf =   ap;
    paramsel->addOpt("perf", new OptMatrix<gMat2D<T> >(*perf));

    //vout.guesses =    guesses;
    gMat2D<T> *guesses_mat = new gMat2D<T>(guesses, 1, tot, true);
    paramsel->addOpt("guesses", new OptMatrix<gMat2D<T> >(*guesses_mat));

    delete[] guesses;

    return paramsel;

}
template<typename T>
static ParamSelection<T>* gurls::ParamSelection< T >::factory ( const std::string &  id) throw (BadParamSelectionCreation) [inline, static, inherited]
Warning:
The returned pointer is a plain, un-managed pointer. The calling function is responsible of deallocating the object.

Definition at line 146 of file paramsel.h.

    {
        if(id == "loocvprimal")
            return new ParamSelLoocvPrimal<T>;
        if(id == "loocvdual")
            return new ParamSelLoocvDual<T>;
        if(id == "fixlambda")
            return new ParamSelFixLambda<T>;
        if(id == "calibratesgd")
            return new ParamSelCalibrateSGD<T>;
        if(id == "siglam")
            return new ParamSelSiglam<T>;
        if(id == "siglamho")
            return new ParamSelSiglamHo<T>;
        if(id == "hodual")
            return new ParamSelHoDual<T>;
        if(id == "hodualr")
            return new ParamSelHoDualr<T>;
        if(id == "hoprimal")
            return new ParamSelHoPrimal<T>;
        if(id == "hoprimalr")
            return new ParamSelHoPrimalr<T>;
        if(id == "fixsiglam")
            return new ParamSelFixSigLam<T>;
        if(id == "loogpregr")
            return new ParamSelLooGPRegr<T>;
        if(id == "hogpregr")
            return new ParamSelHoGPRegr<T>;
        if(id == "siglamloogpregr")
            return new ParamSelSiglamLooGPRegr<T>;
        if(id == "siglamhogpregr")
            return new ParamSelSiglamHoGPRegr<T>;

        throw BadParamSelectionCreation(id);
    }

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