![]() |
GURLS++
2.0.00
C++ Implementation of GURLS Matlab Toolbox
|
ParamSelHoPrimal is the subclass of ParamSelection that implements hold-out cross validation with the primal formulation of RLS.
#include <hoprimal.h>


Public Member Functions | |
| GurlsOptionsList * | execute (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. | |
Protected Member Functions | |
| unsigned long | eig_function (T *A, T *L, int A_rows_cols, unsigned long d, const GurlsOptionsList &opt, unsigned long last) |
| Auxiliary method used to call the right eig/svd function for this class. | |
Definition at line 72 of file hoprimal.h.
| GurlsOptionsList * gurls::ParamSelHoPrimal< T >::execute | ( | const gMat2D< T > & | X, |
| const gMat2D< T > & | Y, | ||
| const GurlsOptionsList & | opt | ||
| ) | [virtual] |
The hold-out approach is used. The performance measure specified by opt.hoperf is maximized.
| X | input data matrix |
| Y | labels matrix |
| opt | options with the following:
|
Implements gurls::ParamSelection< T >.
Definition at line 140 of file hoprimal.h.
{
// [n,T] = size(y);
const unsigned long t = Y.cols();
const unsigned long d = X.cols();
GurlsOptionsList* nestedOpt = new GurlsOptionsList("nested");
const GurlsOptionsList* split = opt.getOptAs<GurlsOptionsList>("split");
const gMat2D< unsigned long > &indices_mat = split->getOptValue<OptMatrix<gMat2D< unsigned long > > >("indices");
const gMat2D< unsigned long > &lasts_mat = split->getOptValue<OptMatrix<gMat2D< unsigned long > > >("lasts");
const unsigned long n = indices_mat.cols();
const unsigned long *lasts = lasts_mat.getData();
const unsigned long* indices_buffer = indices_mat.getData();
int tot = static_cast<int>(std::ceil( opt.getOptAsNumber("nlambda")));
int nholdouts = static_cast<int>(std::ceil( opt.getOptAsNumber("nholdouts")));
gMat2D<T> *LAMBDA = new gMat2D<T>(1, t);
T* lambdas = LAMBDA->getData();
set(lambdas, (T)0.0, t);
T* Q = new T[d*d];
T* QtXty = new T[d*t];
T *L = new T[d];
gMat2D<T>* perf_mat = new gMat2D<T>(nholdouts, tot*t);
T* perf = perf_mat->getData();
gMat2D<T>* guesses_mat = new gMat2D<T>(nholdouts, tot);
T* ret_guesses = guesses_mat->getData();
gMat2D<T>* lambdas_round_mat = new gMat2D<T>(nholdouts, t);
T* lambdas_round = lambdas_round_mat->getData();
PredPrimal< T > primal;
Performance<T>* perfClass = Performance<T>::factory(opt.getOptAsString("hoperf"));
GurlsOptionsList* optimizer = new GurlsOptionsList("optimizer");
nestedOpt->addOpt("optimizer",optimizer);
gMat2D<T> *W = new gMat2D<T>(d, t);
optimizer->addOpt("W", new OptMatrix<gMat2D<T> >(*W));
bool hasXt = opt.hasOpt("kernel.XtX") && opt.hasOpt("kernel.Xty");
for(int nh=0; nh<nholdouts; ++nh)
{
unsigned long last = lasts[nh];
unsigned long* tr = new unsigned long[last];
unsigned long* va = new unsigned long[n-last];
//copy int tr indices_ from n*nh to last
copy< unsigned long >(tr,indices_buffer + n*nh,last,1,1);
//copy int va indices_ from n*nh+last to n*nh+n
copy< unsigned long >(va,(indices_buffer+ n*nh+last), n-last,1,1);
gMat2D<T> Xva(n-last, d);
gMat2D<T> yva(n-last, t);
subMatrixFromRows(X.getData(), X.rows(), d, va, n-last, Xva.getData());
subMatrixFromRows(Y.getData(), Y.rows(), t, va, n-last, yva.getData());
T* Xtr = NULL;
if(hasXt)
{
T* XvatXva = new T[d*d];
dot(Xva.getData(), Xva.getData(), XvatXva, n-last, d, n-last, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor);
const gMat2D<T>&XtX = opt.getOptValue<OptMatrix<gMat2D<T> > >("kernel.XtX");
// Q = XtX - XvatXva
copy(Q, XtX.getData(), XtX.getSize());
axpy(d*d, (T)-1.0, XvatXva, 1, Q, 1);
delete [] XvatXva;
}
else
{
// K = X(tr,:)'*X(tr,:);
Xtr = new T[last*d];
subMatrixFromRows(X.getData(), n, d, tr, last, Xtr);
dot(Xtr, Xtr, Q, last, d, last, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor);
}
unsigned long k = eig_function(Q, L, d, d, opt, last);
T* guesses = lambdaguesses(L, d, k, last, tot, (T)(opt.getOptAsNumber("smallnumber")));
T* ap = new T[tot*t];
if(hasXt)
{
T* Xvatyva = new T[d*t];
dot(Xva.getData(), yva.getData(), Xvatyva, n-last, d, n-last, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);
const gMat2D<T>&Xty = opt.getOptValue<OptMatrix<gMat2D<T> > >("kernel.Xty");
// QtXty = Q'*(Xty - XvatXva)
T* Xtrtytr = new T[d*t];
copy(Xtrtytr, Xty.getData(), Xty.getSize());
axpy(d*t, (T)-1.0, Xvatyva, 1, Xtrtytr, 1);
dot(Q, Xtrtytr, QtXty, d, d, d, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);
delete [] Xvatyva;
delete [] Xtrtytr;
}
else
{
T* ytr = new T[last*t];
subMatrixFromRows(Y.getData(), n, t, tr, last, ytr);
T* Xtrtytr = new T[d*t];
dot(Xtr, ytr, Xtrtytr, last, d, last, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);
delete [] Xtr;
dot(Q, Xtrtytr, QtXty, d, d, d, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);
delete [] ytr;
delete [] Xtrtytr;
}
T* work = new T[d*(d+1)];
for(int i=0; i<tot; ++i)
{
rls_eigen(Q, L, QtXty, W->getData(), guesses[i], last, d, d, d, d, t, work);
OptMatrix<gMat2D<T> > *ret_pred = primal.execute(Xva, yva, *nestedOpt);
nestedOpt->removeOpt("pred");
nestedOpt->addOpt("pred", ret_pred);
GurlsOptionsList* ret_perf = perfClass->execute(Xva, yva, *nestedOpt);
gMat2D<T> &forho_vec = ret_perf->getOptValue<OptMatrix<gMat2D<T> > >("forho");
copy(ap+i, forho_vec.getData(), t, tot, 1);
delete ret_perf;
}
delete [] va;
delete [] tr;
delete [] work;
//[dummy,idx] = max(ap,[],1);
work = NULL;
unsigned long* idx = new unsigned long[t];
indicesOfMax(ap, tot, t, idx, work, 1);
//vout.lambdas_round{nh} = guesses(idx);
T* lambdas_nh = new T[t];
copyLocations(idx, guesses, t, tot, lambdas_nh);
copy(lambdas_round+nh, lambdas_nh, t, nholdouts, 1);
//add lambdas_nh to lambdas
axpy(t, (T)1, lambdas_nh, 1, lambdas, 1);
delete [] lambdas_nh;
delete [] idx;
// vout.perf{nh} = ap;
copy(perf + nh, ap, tot*t, nholdouts, 1);
// vout.guesses{nh} = guesses;
copy(ret_guesses + nh, guesses, tot, nholdouts, 1);
delete [] guesses;
delete [] ap;
}
delete nestedOpt;
delete perfClass;
delete [] Q;
delete [] QtXty;
delete [] L;
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("lambdas");
paramsel->removeOpt("perf");
paramsel->removeOpt("lambdas_round");
}
else
paramsel = new GurlsOptionsList("paramsel");
paramsel->addOpt("guesses", new OptMatrix<gMat2D<T> >(*guesses_mat));
if(nholdouts>1)
scal(t, (T)1.0/nholdouts, lambdas, 1);
paramsel->addOpt("lambdas", new OptMatrix<gMat2D<T> >(*LAMBDA));
paramsel->addOpt("perf", new OptMatrix<gMat2D<T> >(*perf_mat));
paramsel->addOpt("lambdas_round", new OptMatrix<gMat2D<T> >(*lambdas_round_mat));
return paramsel;
}
| static ParamSelection<T>* gurls::ParamSelection< T >::factory | ( | const std::string & | id | ) | throw (BadParamSelectionCreation) [inline, static, inherited] |
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);
}