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

ParamSelLoocvPrimal is the sub-class of ParamSelection that implements LOO cross-validation with the primal formulation.

#include <loocvprimal.h>

Inheritance diagram for gurls::ParamSelLoocvPrimal< T >:
Collaboration diagram for gurls::ParamSelLoocvPrimal< 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 primal 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::ParamSelLoocvPrimal< T >

Definition at line 76 of file loocvprimal.h.


Member Function Documentation

template<typename T >
GurlsOptionsList * gurls::ParamSelLoocvPrimal< 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 default fields:
  • nlambda (default)
  • smallnumber
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 97 of file loocvprimal.h.

{
    typename std::set<T*> garbage;

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


        //  K = X'*X;
        const unsigned long xc = X.cols();
        const unsigned long xr = X.rows();

        if(xr != n)
            throw gException(Exception_Inconsistent_Size);

        T* K = new T[xc*xc];
        garbage.insert(K);
        dot(X.getData(), X.getData(), K, xr, xc, xr, xc, xc, xc, CblasTrans, CblasNoTrans, CblasColMajor);


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

        //  [Q,L] = eig(K);
        //  L = diag(L);

//        T* Q, *L;

//        eig(K, Q, L, xc, xc);
//        garbage.insert(Q);
//        garbage.insert(L);

        T* Q = K;
        T* L = new T[xc];
        garbage.insert(L);

        eig_sm(Q, L, xc);


        T* filtered = L;
        T* guesses = lambdaguesses(filtered, xc, std::min(xc,xr), xr, tot, (T)(opt.getOptAsNumber("smallnumber")));
        garbage.insert(guesses);
//        set(guesses, 0.f, tot);

        T* LOOSQE = new T[tot*t];
        garbage.insert(LOOSQE);
        set(LOOSQE, (T)0.0, tot*t);

        //  LEFT = X*Q;
        //  RIGHT = Q'*X'*y;
        T* LEFT = new T[xr*xc];
        garbage.insert(LEFT);
        dot(X.getData(), Q, LEFT, xr, xc, xc, xc, xr, xc, CblasNoTrans, CblasNoTrans, CblasColMajor);

        T* tmp = new T[xc*t];
        garbage.insert(tmp);
        dot(X.getData(), Y.getData(), tmp, xr, xc, n, t, xc, t, CblasTrans, CblasNoTrans, CblasColMajor);

        T* RIGHT = new T[xc*t];
        garbage.insert(RIGHT);
        dot(Q, tmp, RIGHT, xc, xc, xc, t, xc, t, CblasTrans, CblasNoTrans, CblasColMajor);

        delete[] tmp;
        garbage.erase(tmp);

        //  right = Q'*X';
        T* right = new T[xc*xr];
        garbage.insert(right);
        dot(Q, X.getData(), right, xc, xc, xr, xc, xc, xr, CblasTrans, CblasTrans, CblasColMajor);

        delete[] Q;
        garbage.erase(Q);

        T* den = new T[n];
//        T* Le = new T[n*t];
        garbage.insert(den);
//        garbage.insert(Le);

        T* tmpvec = new T[xc];
        garbage.insert(tmpvec);
        T* tmp1 = new T[xr*t];
        garbage.insert(tmp1);
        T* LL = new T[xc*xc];
        garbage.insert(LL);
        tmp = new T[xc*t];
        garbage.insert(tmp);
        T* num = new T[xr*t];
        garbage.insert(num);
        T* row = new T[xc];
        garbage.insert(row);
        T* num_div_den = new T[n];
        garbage.insert(num_div_den);

        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);

//        const int pred_size = pred->getSize();

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

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


        //  for i = 1:tot
        for(int s = 0; s < tot; ++s)
        {

            //      LL = L + (n*guesses(i));
            set(tmpvec, n*guesses[s] , xc);
            axpy(xc, (T)1.0, L, 1, tmpvec, 1);

            //      LL = LL.^(-1)
            setReciprocal(tmpvec, xc);
            //      LL = diag(LL);
            diag(tmpvec, xc, LL);


            //      num = y - LEFT*LL*RIGHT;

            dot(LL, RIGHT, tmp, xc, xc, xc, t, xc, t, CblasNoTrans, CblasNoTrans, CblasColMajor);
            dot(LEFT, tmp, tmp1, xr, xc, xc, t, xr, t, CblasNoTrans, CblasNoTrans, CblasColMajor);


            copy(num, Y.getData(), xr*t);
            axpy(xr*t, (T)-1.0, tmp1, 1, num, 1);


            // den = zeros(n,1);
            set(den, (T)0.0, n);

            //      for j = 1:n
            //          den(j) = 1-LEFT(j,:)*LL*right(:,j);
            //      end


            for (unsigned long j = 0; j < n; ++j)
            {
                dot(LL, right +(xc*j), tmp, xc, xc, xc, 1, xc, 1, CblasNoTrans, CblasNoTrans, CblasColMajor);

                //extract j-th row from LEFT
                copy(row, LEFT + j, xc, 1, xr);

                den[j] =  ((T) 1.0) - dot (xc, row, 1, tmp, 1);
            }


    //        opt.pred = zeros(n,T);
//            set(pred->getData(), (T)0.0, pred_size);



    //        for t = 1:T
            for(unsigned long j = 0; j< t; ++j)
            {
                rdivide(num + (n*j), den, num_div_den, n);

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

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

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

    //        for t = 1:T
            copy(ap+s, forho_vec.getData(), forho_vec.getSize(), tot, 1);

            delete perf;
        }


        delete perfClass;

        delete[] row;
        garbage.erase(row);
        delete[] num;
        garbage.erase(num);
        delete[] tmp;
        garbage.erase(tmp);
        delete[] tmp1;
        garbage.erase(tmp1);
        delete[] tmpvec;
        garbage.erase(tmpvec);
        delete[] LL;
        garbage.erase(LL);
        delete [] num_div_den;
        garbage.erase(num_div_den);

        delete nestedOpt;
        delete[] L;
        garbage.erase(L);
        delete[] LEFT;
        garbage.erase(LEFT);
        delete[] RIGHT;
        garbage.erase(RIGHT);
        delete[] right;
        garbage.erase(right);
        delete[] den;
        garbage.erase(den);
        delete [] LOOSQE;
        garbage.erase(LOOSQE);
//        delete[] Le;
//        garbage.erase(Le);

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

        //vout.lambdas =    guesses(idx);
        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");



        paramsel->addOpt("lambdas", new OptMatrix<gMat2D<T> >(*LAMBDA));
        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;
    }
    catch( gException& e)
    {

        for(typename std::set<T*>::iterator it = garbage.begin(); it != garbage.end(); ++it)
            delete[] (*it);

        throw e;
    }
}
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