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

ParamSelSiglam is the sub-class of ParamSelection that implements hold-out cross validation with the dual formulation for a rbf kernel.

#include <siglamho.h>

Inheritance diagram for gurls::ParamSelSiglamHo< T >:
Collaboration diagram for gurls::ParamSelSiglamHo< 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 with rbf kernel.

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::ParamSelSiglamHo< T >

Definition at line 72 of file siglamho.h.


Member Function Documentation

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

The hold-out approach is used oer a 2-dimensional grid of values for the parameters sigma (kernel) and lambda (regularization) The performance measure specified by opt.hoperf is maximized.

Parameters:
Xinput data matrix
Ylabels matrix
optoptions with the following:
  • nlambda (default)
  • nsigma (default)
  • hoperf (default)
  • smallnumber (default)
  • split (settable with the class Split and its subclasses)
Returns:
adds the field paramsel to opt, which is alist containing the following fields:
  • lambdas = array containing the value of the regularization parameter lambda maximizing the mean validation accuracy over the classes, replicated as many times as the number of classes
  • sigma = values of the kernel parameter maximizing the validation accuracy
  • 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 99 of file siglamho.h.

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


    GurlsOptionsList* nestedOpt = new GurlsOptionsList("nested");
    nestedOpt->copyOpt("nlambda", opt);
    nestedOpt->copyOpt("nholdouts", opt);
    nestedOpt->copyOpt("hoperf", opt);
    nestedOpt->copyOpt("smallnumber", opt);
    nestedOpt->copyOpt("split", opt);

    GurlsOptionsList* kernel = new GurlsOptionsList("kernel");
    kernel->addOpt("type", "rbf");
    nestedOpt->addOpt("kernel", kernel);


    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("lambdas");
        paramsel->removeOpt("sigma");
    }
    else
        paramsel = new GurlsOptionsList("paramsel");


    gMat2D<T>* dist = NULL;

    // if ~isfield(opt.kernel,'distance')
    if(!kernel->hasOpt("distance"))
        //  opt.kernel.distance = squareform(pdist(X));
    {
        dist = new gMat2D<T>(X.rows(), X.rows());

        squareform<T>(X.getData(), X.rows(), X.cols(), dist->getData(), X.rows());

        T *distSquared = new T[X.rows()*X.rows()];
        copy(distSquared , dist->getData(), X.rows()*X.rows());

        mult<T>(distSquared, distSquared, dist->getData(), X.rows()*X.rows());

        kernel->addOpt("distance", new OptMatrix<gMat2D<T> >(*dist));
        delete [] distSquared;
    }
    else
    {
        GurlsOption *dist_opt = kernel->getOpt("distance");
        dist = &(OptMatrix<gMat2D<T> >::dynacast(dist_opt))->getValue();
    }


    //  if ~isfield(opt,'sigmamin')
    if(!opt.hasOpt("sigmamin"))
    {
        //  %D = sort(opt.kernel.distance);
        //  %opt.sigmamin = median(D(2,:));
        //  D = sort(squareform(opt.kernel.distance));
        int d_len = X.rows()*(X.rows()-1)/2;
        T* distY = new T[d_len];
        //        squareform<T>(dist->getData(), dist->rows(), dist->cols(), distY, 1);

        const int size = dist->cols();
        T* it = distY;

        for(int i=1; i< size; it+=i, ++i)
            copy(it , dist->getData()+(i*size), i);

        std::sort(distY, distY + d_len);
        //  firstPercentile = round(0.01*numel(D)+0.5);

        int firstPercentile = gurls::round((T)0.01 * d_len + (T)0.5)-1;

        //  opt.sigmamin = D(firstPercentile);
        nestedOpt->addOpt("sigmamin", new OptNumber(sqrt( distY[firstPercentile]) ));

        delete [] distY;
    }
    else
    {
        nestedOpt->addOpt("sigmamin", new OptNumber(opt.getOptAsNumber("sigmamin")));
    }

    T sigmamin = static_cast<T>(nestedOpt->getOptAsNumber("sigmamin"));

    //  if ~isfield(opt,'sigmamax')
    if(!opt.hasOpt("sigmamax"))
    {
        //  %D = sort(opt.kernel.distance);
        //  %opt.sigmamax = median(D(n,:));
        T mAx = *(std::max_element(dist->getData(),dist->getData()+ dist->getSize()));

        //  opt.sigmamax = max(max(opt.kernel.distance));
        nestedOpt->addOpt("sigmamax", new OptNumber( sqrt( mAx )));
    }
    else
    {
        nestedOpt->addOpt("sigmamax", new OptNumber(opt.getOptAsNumber("sigmamax")));
    }

    T sigmamax = static_cast<T>(nestedOpt->getOptAsNumber("sigmamax"));

    // if opt.sigmamin <= 0
    if( le(sigmamin, (T)0.0) )
    {
        //  opt.sigmamin = eps;
        nestedOpt->removeOpt("sigmamin");
        nestedOpt->addOpt("sigmamin", new OptNumber(std::numeric_limits<T>::epsilon()));
        sigmamin = std::numeric_limits<T>::epsilon();
    }

    // if opt.sigmamin <= 0
    if( le(sigmamin, (T)0.0) )
    {
        //  opt.sigmamax = eps;
        nestedOpt->removeOpt("sigmamax");
        nestedOpt->addOpt("sigmamax", new OptNumber(std::numeric_limits<T>::epsilon()));
        sigmamax = std::numeric_limits<T>::epsilon();
    }


    int nlambda = static_cast<int>(opt.getOptAsNumber("nlambda"));
    int nsigma  = static_cast<int>(opt.getOptAsNumber("nsigma"));

    T q = pow( sigmamax/sigmamin, static_cast<T>(1.0/(nsigma-1.0)));

    // PERF = zeros(opt.nsigma,opt.nlambda,T);
    T* perf = new T[nlambda];

    // sigmas = zeros(1,opt.nsigma);

    KernelRBF<T> rbfkernel;
    ParamSelHoDual<T> hodual;


    T maxPerf = (T)-1.0;
    int m = -1;
    T guess = (T)-1.0;

    T* perf_median = new T[nlambda*t];
    T* guesses_median = new T[nlambda];
    T* row = new T[t];

    const unsigned long nholdouts = static_cast<unsigned long>(opt.getOptAsNumber("nholdouts"));
    T* work = new T[nholdouts];

//    for i = 1:opt.nsigma
    for(int i=0; i<nsigma; ++i)
    {
        nestedOpt->addOpt("paramsel", paramsel);

        paramsel->removeOpt("sigma");
        paramsel->addOpt("sigma", new OptNumber( sigmamin * pow(q, i)));

        //  opt.kernel = kernel_rbf(X,y,opt);
        GurlsOptionsList* retKernel = rbfkernel.execute(X, Y, *nestedOpt);

        nestedOpt->removeOpt("kernel");
        nestedOpt->addOpt("kernel", retKernel);

        nestedOpt->removeOpt("paramsel", false);

        //  paramsel = paramsel_hodual(X,y,opt);
        GurlsOptionsList* ret_paramsel = hodual.execute(X, Y, *nestedOpt);


//        PERF(i,:,:) = reshape(median(reshape(cell2mat(paramsel.perf')',opt.nlambda*T,nh),2),T,opt.nlambda)';
        gMat2D<T> &perf_mat = ret_paramsel->getOptValue<OptMatrix<gMat2D<T> > >("perf"); // nholdouts x nlambda*t
        median(perf_mat.getData(), perf_mat.rows(), perf_mat.cols(), 1, perf_median, work);

        for(int j=0;j<nlambda;++j)
        {
            getRow(perf_median, nlambda, t, j, row);
            perf[j] = sumv(row, t);
        }

//        guesses(i,:) = median(cell2mat(paramsel.guesses'),1);
        unsigned long mm = std::max_element(perf, perf + nlambda) - perf;

        if( gt(perf[mm], maxPerf) )
        {
            maxPerf = perf[mm];
            m = i;

            gMat2D<T> &guesses_mat = ret_paramsel->getOptValue<OptMatrix<gMat2D<T> > >("guesses"); // nholdouts x nlambda
            median(guesses_mat.getData(), guesses_mat.rows(), guesses_mat.cols(), 1, guesses_median, work);

            guess = guesses_median[mm];
        }

        delete ret_paramsel;
    }

    delete [] row;
    delete [] work;
    delete [] perf;
    delete [] perf_median;
    delete [] guesses_median;
    delete nestedOpt;


    paramsel->removeOpt("sigma");
    paramsel->addOpt("sigma", new OptNumber( sigmamin * pow(q,m) ));

    // % opt lambda
    // vout.lambdas = guesses(m,n)*ones(1,T);

    gMat2D<T> *LAMBDA = new gMat2D<T>(1, t);
    set(LAMBDA->getData(), guess, t);

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

    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