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

PredKernelTrainTest is the sub-class of PredKernel that computes the kernel matrix between training and test sets.

#include <predkerneltraintest.h>

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

List of all members.

Public Member Functions

GurlsOptionsListexecute (const gMat2D< T > &X, const gMat2D< T > &Y, const GurlsOptionsList &opt) throw (gException)
 Computes the kernel matrix between the training set and the test set X, in order to predict the labels for X.

Static Public Member Functions

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

Detailed Description

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

Definition at line 61 of file predkerneltraintest.h.


Member Function Documentation

template<typename T >
GurlsOptionsList * gurls::PredKernelTrainTest< T >::execute ( const gMat2D< T > &  X,
const gMat2D< T > &  Y,
const GurlsOptionsList opt 
) throw (gException) [virtual]
Parameters:
Xinput data matrix
Ynot used
optoptions with the following required fields:
  • optimizer (list with the field X containing the input matrix. Required only if the field type of kernel is 'rbf' or 'chisquared', is is settable with the class Optimizer and its subclasses )
  • kernel (list with the field type, settable through the class Kernel and its subclasses)
  • testkernel (only if opt.kernel.type is 'load')
  • paramsel (list with the field sigma, required, only if opt.kernel.type is 'rbf', and settable with the class ParamSel and its subclasses SigLam and SiglamHo)
Returns:
predkernel GurlsOptionsList with at least the field K containing the kernel matrix

Implements gurls::PredKernel< T >.

Definition at line 82 of file predkerneltraintest.h.

{
    const GurlsOptionsList* optimizer = opt.getOptAs<GurlsOptionsList>("optimizer");

    std::string kernelType = opt.getOptValue<OptString>("kernel.type");

    const gMat2D<T>& rls_X = optimizer->getOptValue<OptMatrix<gMat2D<T> > >("X");


    const unsigned long xr = X.rows();
    const unsigned long xc = X.cols();
    const unsigned long rls_xr = rls_X.rows();

    if(xc != rls_X.cols())
        throw gException(Exception_Inconsistent_Size);


    GurlsOptionsList* predkernel = new GurlsOptionsList("predkernel");
    predkernel->addOpt("type", kernelType);

    gMat2D<T>* K;

    if(kernelType == "rbf")
    {
        double sigma = opt.getOptValue<OptNumber>("paramsel.sigma");

//                opt.predkernel.distance = distance(X',opt.rls.X');
        gMat2D<T> *dist = new gMat2D<T>(xr, rls_xr);

        distance_transposed(X.getData(), rls_X.getData(), xc, xr, rls_xr, dist->getData());


//                fk.distance = opt.predkernel.distance;
        predkernel->addOpt("distance", new OptMatrix<gMat2D<T> > (*dist));


        K = new gMat2D<T>(xr, rls_xr);
        copy(K->getData(), dist->getData(), dist->getSize());

//            fk.K = exp(-(opt.predkernel.distance)/(opt.paramsel.sigma^2));
        scal(K->getSize(), (T)(-1.0/pow(sigma, 2)), K->getData(), 1);
        exp(K->getData(), K->getSize());

        if(optimizer->hasOpt("L"))
        {
            gMat2D<T> *Ktest = new gMat2D<T>(xr, 1);
            set(Ktest->getData(), (T)1.0, xr);

            predkernel->addOpt("Ktest", new OptMatrix<gMat2D<T> >(*Ktest));
        }

    }

    else if(kernelType == "load")
    {
//            load(opt.testkernel);
        std::string testKernel = opt.getOptAsString("testKernel");

//            fk.K = K_tetr;
        K = new gMat2D<T>();
        K->load(testKernel);

    }

    else if(kernelType == "chisquared")
    {
        const T epsilon = std::numeric_limits<T>::epsilon();

        K = new gMat2D<T>(xr, rls_xr);
        T* Kbuf = K->getData();
        set(Kbuf, (T)0.0, K->getSize());

//            for i = 1:size(X,1)
        for(unsigned long i=0; i<xr; ++i)
        {
//                for j = 1:size(opt.rls.X,1)
            for(unsigned long j=0; j<rls_xr; ++j)
            {

//                    fk.K(i,j) = sum(...
//                                    ( (X(i,:) - opt.rls.X(j,:)).^2 ) ./ ...
//                                    ( 0.5*(X(i,:) + opt.rls.X(j,:)) + eps));
                T sum = 0;
                for(unsigned long k=0; k< xc; ++k)
                {
                    const T X_ik = X.getData()[i+(xr*k)];
                    const T rlsX_jk = rls_X.getData()[j+(rls_xr*k)];

                    sum += pow(X_ik - rlsX_jk, 2) / static_cast<T>(((0.5*(X_ik + rlsX_jk)) + epsilon));
                }

                Kbuf[i+(xr*j)] = sum;
            }
        }

    }

    else
        throw gException(Exception_Required_Parameter_Missing);

    predkernel->addOpt("K", new OptMatrix<gMat2D<T> > (*K));


    return predkernel;
}
template<typename T>
static PredKernel<T>* gurls::PredKernel< T >::factory ( const std::string &  id) throw (BadPredKernelCreation) [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 97 of file predkernel.h.

    {
        if(id == "traintest")
            return new PredKernelTrainTest<T>;

        throw BadPredKernelCreation(id);
    }

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