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

ParamSelSiglamLooGPRegr is the sub-class of ParamSelection that implements leave-one-ot parameter selection for Gaussian process regression.

#include <siglamloogpregr.h>

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

List of all members.

Public Member Functions

GurlsOptionsListexecute (const gMat2D< T > &X, const gMat2D< T > &Y, const GurlsOptionsList &opt)
 Performs parameter selection for Gaussian process regression with the dual formulation 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::ParamSelSiglamLooGPRegr< T >

Definition at line 66 of file siglamloogpregr.h.


Member Function Documentation

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

The leave-one-out approach is used on 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)
  • kernel (settbale with the class Kernel 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 91 of file siglamloogpregr.h.

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

    const unsigned long d = X.cols();

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

//    if ~isfield(opt,'kernel')
    if(!opt.hasOpt("kernel"))
    {
//        opt.kernel.type = 'rbf';
        GurlsOptionsList* kernel = new GurlsOptionsList("kernel");
        kernel->addOpt("type", "rbf");

        nestedOpt->addOpt("kernel", kernel);
    }
    else
        nestedOpt->copyOpt("kernel", opt);


    GurlsOptionsList* kernel = nestedOpt->getOptAs<GurlsOptionsList>("kernel");


    gMat2D<T> *distance;

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

//        opt.kernel.distance = square_distance(X',X');
        distance_transposed(X.getData(), X.getData(), d, n, n, distance->getData());

        kernel->addOpt("distance", new OptMatrix<gMat2D<T> >(*distance));
    }
    else
        distance = &(kernel->getOptValue<OptMatrix<gMat2D<T> > >("distance"));


//    if ~isfield(opt,'sigmamin')
    if(!opt.hasOpt("sigmamin"))
    {
        int d_len = n*(n-1)/2;
        T* distLinearized = new T[d_len];

//        D = sort(opt.kernel.distance(tril(true(n),-1)));

        const unsigned long size = distance->cols();
        T* it = distLinearized;
        T* d_it = distance->getData();

        for(unsigned long i=1; i< size; ++i)
        {
            gurls::copy(it , d_it+i, size - i);

            it += size - i;
            d_it += size;
        }

        std::sort(distLinearized, distLinearized + 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( distLinearized[firstPercentile]) ));

        delete [] distLinearized;
    }
    else
        nestedOpt->copyOpt("sigmamin", opt);


//    if ~isfield(opt,'sigmamax')
    if(!opt.hasOpt("sigmamax"))
    {
//        opt.sigmamax = sqrt(max(max(opt.kernel.distance)));
        double sigmaMax = sqrt(*std::max_element(distance->getData(), distance->getData()+distance->getSize()));
        nestedOpt->addOpt("sigmamax", new OptNumber(sigmaMax));
    }
    else
        nestedOpt->copyOpt("sigmamax", opt);


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

    const int nsigma = static_cast<int>(opt.getOptAsNumber("nsigma"));
    const int nlambda = static_cast<int>(opt.getOptAsNumber("nlambda"));
    const T sigmamin = static_cast<T>(nestedOpt->getOptAsNumber("sigmamin"));

//    q = (opt.sigmamax/opt.sigmamin)^(1/(opt.nsigma-1));
    T q = static_cast<T>(std::pow(nestedOpt->getOptAsNumber("sigmamax")/nestedOpt->getOptAsNumber("sigmamin"), 1.0/(nsigma-1.0)));

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

//    sigmas = zeros(1,opt.nsigma);
//    T* sigmas = new T[nsigma];

    T* guesses = new T[nsigma*nlambda];

    KernelRBF<T> rbf;
    ParamSelLooGPRegr<T> loogp;

    T* work = new T[t];

    GurlsOptionsList* paramsel_rbf = new GurlsOptionsList("paramsel");
    nestedOpt->addOpt("paramsel", paramsel_rbf);

//    for i = 1:opt.nsigma
    for(int i=0; i<nsigma; ++i)
    {
//        sigmas(i) = (opt.sigmamin*(q^(i-1)));
        const T sigma = sigmamin* std::pow(q, i);
//        sigmas[i] = sigma;

//        opt.paramsel.sigma = sigmas(i);
        paramsel_rbf->removeOpt("sigma");
        paramsel_rbf->addOpt("sigma", new OptNumber(sigma));


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

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

//        paramsel = paramsel_loogpregr(X,y,opt);
        GurlsOptionsList* paramsel_loogp = loogp.execute(X, Y, *nestedOpt);

//        perf(i,:,:) = paramsel.perf;
        gMat2D<T> &perf_mat = paramsel_loogp->getOptValue<OptMatrix<gMat2D<T> > >("perf"); // nlambda x t

        T* perf_it = perf + i;
        for(int j=0; j<nlambda; ++j, perf_it += nsigma)
        {
            getRow(perf_mat.getData(), perf_mat.rows(), perf_mat.cols(), j, work);

            *perf_it = sumv(work, t);
        }

//        guesses(i,:) = paramsel.guesses;
        gMat2D<T> &guesses_mat = paramsel_loogp->getOptValue<OptMatrix<gMat2D<T> > >("guesses"); // nlambda x 1
        copy(guesses+i, guesses_mat.getData(), nlambda, nsigma, 1);

        delete paramsel_loogp;
    }

    delete [] work;
    delete nestedOpt;

//    M = sum(perf,3); % sum over classes
//    [dummy,i] = max(M(:));
    int i = std::max_element(perf, perf +(nsigma*nlambda)) - perf;

//    [m,n] = ind2sub(size(M),i);
    int im = i%nsigma;

    delete [] perf;

    GurlsOptionsList* paramsel = new GurlsOptionsList("paramsel");

//    % opt sigma
//    vout.sigma = opt.sigmamin*(q^(m-1));
    paramsel->addOpt("sigma", new OptNumber( sigmamin*(std::pow(q, im))) );

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

    gMat2D<T> *lambdas = new gMat2D<T>(1, t);
    set(lambdas->getData(), guesses[i], t);

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

    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