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

RLSDual is the sub-class of Optimizer that implements RLS with the dual formulation.

#include <rlsdual.h>

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

List of all members.

Public Member Functions

GurlsOptionsListexecute (const gMat2D< T > &X, const gMat2D< T > &Y, const GurlsOptionsList &opt)
 Computes a classifier for the dual formulation of RLS.

Static Public Member Functions

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

Detailed Description

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

Definition at line 58 of file rlsdual.h.


Member Function Documentation

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

The regularization parameter is set to the one found in the field paramsel of opt. In case of multiclass problems, the regularizers need to be combined with the function specified inthe field singlelambda of opt

Parameters:
Xinput data matrix
Ylabels matrix
optoptions with the following:
  • singlelambda (default)
  • paramsel (settable with the class ParamSelection and its subclasses)
  • kernel (settable with the class Kernel and its subclasses)
Returns:
adds to opt the field optimizer, which is a list containing the following fields:
  • W = empty matrix
  • C = matrix of coefficient vectors of dual rls estimator for each class
  • X = empty matrix

Implements gurls::Optimizer< T >.

Definition at line 83 of file rlsdual.h.

{
   //   lambda = opt.singlelambda(opt.paramsel.lambdas);
   const gMat2D<T> &ll = opt.getOptValue<OptMatrix<gMat2D<T> > >("paramsel.lambdas");
   T lambda = opt.getOptAs<OptFunction>("singlelambda")->getValue(ll.getData(), ll.getSize());

   const GurlsOptionsList* kernel = opt.getOptAs<GurlsOptionsList>("kernel");
   const gMat2D<T>& K_mat = kernel->getOptValue<OptMatrix<gMat2D<T> > >("K");

   T* K = new T[K_mat.getSize()];
   copy(K, K_mat.getData(), K_mat.getSize());

    //n = size(opt.kernel.K,1);
   const long n = K_mat.rows();

   //T = size(y,2);
   const long t = Y.cols();


//    std::cout << "Solving dual RLS... " << std::endl;

    const T coeff = n*static_cast<T>(lambda);
    long i=0;
    for(T* it = K; i<n; ++i, it+=n+1)
        *it += coeff;


   std::set<T*> garbage;

   gMat2D<T>* retC = NULL;

   try // Try solving it with cholesky first.
   {
//        R = chol(K);
        T* R = new T[n*n];
        garbage.insert(R);
        cholesky(K, n, n, R);

//        cfr.C = R\(R'\y);
        retC = new gMat2D<T>(Y.rows(), t);

        copy(retC->getData(), Y.getData(), Y.getSize());
        mldivide_squared(R, retC->getData(), n, n, retC->rows(), retC->cols(), CblasTrans);
        mldivide_squared(R, retC->getData(), n, n, retC->rows(), retC->cols(), CblasNoTrans);

        delete[] R;
        garbage.erase(R);
   }
   catch (gException& /*gex*/)
   {
       for(typename std::set<T*>::iterator it = garbage.begin(); it != garbage.end(); ++it)
           delete[] (*it);

       garbage.clear();

       if(retC != NULL)
           delete retC;


//           [Q,L,V] = svd(K);
//           Q = double(Q);
//           L = double(diag(L));
       T *Q, *L, *V;
       int Q_rows, Q_cols;
       int L_len;
       int V_rows, V_cols;

       svd(K, Q, L, V, n, n, Q_rows, Q_cols, L_len, V_rows, V_cols);


//           cfr.C = rls_eigen(Q,L,y,lambda,n);
       retC = new gMat2D<T>(Q_rows, Y.cols());

       T* Qty = new T[Q_cols*Y.cols()];
       dot(Q, Y.getData(), Qty, Q_rows, Q_cols, Y.rows(), Y.cols(), Q_cols, Y.cols(), CblasTrans, CblasNoTrans, CblasColMajor);

       T* work = new T[L_len*(Q_rows+1)];
       rls_eigen(Q, L, Qty, retC->getData(), lambda, n, Q_rows, Q_cols, L_len, Q_cols, Y.cols(), work);

       delete [] work;
       delete [] Qty;
       delete [] Q;
       delete [] L;
       delete [] V;
   }

   delete[] K;

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

//       if strcmp(opt.kernel.type, 'linear')
   if(kernel->getOptAsString("type") == "linear")
   {
//           cfr.W = X'*cfr.C;
       gMat2D<T>* W  = new gMat2D<T>(X.cols(), retC->cols());
       dot(X.getData(), retC->getData(), W->getData(), X.rows(), X.cols(), retC->rows(), retC->cols(), W->rows(), W->cols(), CblasTrans, CblasNoTrans, CblasColMajor);
       optimizer->addOpt("W", new OptMatrix<gMat2D<T> >(*W));

//           cfr.C = [];
       gMat2D<T>* emptyC = new gMat2D<T>();
       optimizer->addOpt("C", new OptMatrix<gMat2D<T> >(*emptyC));

//           cfr.X = [];
       gMat2D<T>* emptyX = new gMat2D<T>();
       optimizer->addOpt("X", new OptMatrix<gMat2D<T> >(*emptyX));

       delete retC;
   }
   else
   {
//           cfr.W = [];
       gMat2D<T>* emptyW = new gMat2D<T>();
       optimizer->addOpt("W", new OptMatrix<gMat2D<T> >(*emptyW));

//           cfr.C = retC;
       optimizer->addOpt("C", new OptMatrix<gMat2D<T> >(*retC));

//           cfr.X = X;
       gMat2D<T>* optX = new gMat2D<T>(X);
       optimizer->addOpt("X", new OptMatrix<gMat2D<T> >(*optX));
   }

    return optimizer;
}
template<typename T>
static Optimizer<T>* gurls::Optimizer< T >::factory ( const std::string &  id) throw (BadOptimizerCreation) [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 130 of file optimization.h.

    {
      if(id == "rlsauto")
        return new RLSAuto<T>;
      if(id == "rlsprimal")
        return new RLSPrimal<T>;
      if(id == "rlsprimalr")
        return new RLSPrimalr<T>;
      if(id == "rlsdual")
        return new RLSDual<T>;
      if(id == "rlsdualr")
        return new RLSDualr<T>;
      if(id == "rlspegasos")
        return new RLSPegasos<T>;
      if(id == "rlsgpregr")
        return new RLSGPRegr<T>;
      if(id == "rlsprimalrecinit")
        return new RLSPrimalRecInit<T>;
      if(id == "rlsprimalrecupdate")
        return new RLSPrimalRecUpdate<T>;
      if(id == "rlsrandfeats")
        return new RLSRandFeats<T>;

        throw BadOptimizerCreation(id);
    }

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