Esempio n. 1
0
void RecursiveRLSWrapper<T>::train(const gMat2D<T> &X, const gMat2D<T> &y)
{
    this->opt->removeOpt("split");
    this->opt->removeOpt("paramsel");
    this->opt->removeOpt("optimizer");
    this->opt->removeOpt("kernel");


    SplitHo<T> splitTask;
    GurlsOptionsList* split = splitTask.execute(X, y, *(this->opt));
    this->opt->addOpt("split", split);


    const gMat2D<unsigned long>& split_indices = split->getOptValue<OptMatrix<gMat2D<unsigned long> > >("indices");
    const gMat2D<unsigned long>& split_lasts = split->getOptValue<OptMatrix<gMat2D<unsigned long> > >("lasts");

    const unsigned long n = X.rows();
    const unsigned long d = X.cols();
    const unsigned long t = y.cols();

    const unsigned long last = split_lasts.getData()[0];
    const unsigned long nva = n-last;

    unsigned long* va = new unsigned long[nva];
    copy(va, split_indices.getData()+last, nva);

    gMat2D<T>* Xva = new gMat2D<T>(nva, d);
    gMat2D<T>* yva = new gMat2D<T>(nva, t);

    subMatrixFromRows(X.getData(), n, d, va, nva, Xva->getData());
    subMatrixFromRows(y.getData(), n, t, va, nva, yva->getData());

    gMat2D<T>* XtX = new gMat2D<T>(d, d);
    gMat2D<T>* Xty = new gMat2D<T>(d, t);


    dot(X.getData(), X.getData(), XtX->getData(), n, d, n, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor);
    dot(X.getData(), y.getData(), Xty->getData(), n, d, n, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);


    GurlsOptionsList* kernel = new GurlsOptionsList("kernel");
    kernel->addOpt("XtX", new OptMatrix<gMat2D<T> >(*XtX));
    kernel->addOpt("Xty", new OptMatrix<gMat2D<T> >(*Xty));

    kernel->addOpt("Xva", new OptMatrix<gMat2D<T> >(*Xva));
    kernel->addOpt("yva", new OptMatrix<gMat2D<T> >(*yva));

    nTot = n;
    this->opt->addOpt("kernel", kernel);

    ParamSelHoPrimal<T> paramselTask;
    this->opt->addOpt("paramsel", paramselTask.execute(X, y, *(this->opt)));


    RLSPrimalRecInit<T> optimizerTask;
    this->opt->addOpt("optimizer", optimizerTask.execute(X, y, *(this->opt)));
}
Esempio n. 2
0
void KernelWrapper<T>::setSigma(double value)
{
    if(this->opt->hasOpt("paramsel.sigma"))
        this->opt->template getOptValue<OptNumber>("paramsel.sigma") = value;
    else
    {
        GurlsOptionsList* paramsel = this->opt->template getOptAs<GurlsOptionsList>("paramsel");
        paramsel->addOpt("sigma", new OptNumber(value));
    }

    setNSigma(1);
}
Esempio n. 3
0
gurls::GurlsOptionsList* train(T* X, T* y, unsigned long n, unsigned long d, unsigned long t, 
							std::string algorithm, std::string kernel, std::string problem, std::string savefile)
{
    gMat2D<T> Xtr(X, n, d, false);
    gMat2D<T> ytr(y, n, t, false);
	GurlsWrapper<T> *gurlsWrap;

    try
    {
		if(algorithm=="krls")
			gurlsWrap= new KernelRLSWrapper<T>("Train");
		else{
			algorithm="krls";
			gurlsWrap= new KernelRLSWrapper<T>("Train");
			std::cout<<"algorithm forced to krls"<<std::endl;
			}
		
		GurlsOptionsList *retopt = new GurlsOptionsList(gurlsWrap->getOpt());
		gurlsWrap->loadOpt(*retopt, false);

		if(kernel=="linear" && algorithm=="krls")
			 dynamic_cast<KernelRLSWrapper<T>*>(gurlsWrap)->setKernelType(KernelWrapper<T>::LINEAR);
		else if(algorithm=="krls")
			dynamic_cast<KernelRLSWrapper<T>*>(gurlsWrap)->setKernelType(KernelWrapper<T>::RBF);

		if(problem=="classification"){
			gurlsWrap->setProblemType(gurlsWrap->CLASSIFICATION);}
		else if(problem=="regression")
			gurlsWrap->setProblemType(gurlsWrap->REGRESSION);
		else{
			const char * probList[] = { "Classification", "Regression"};
            typename KernelWrapper<T>::ProblemType prob =gurlsWrap->problemTypeFromData(Xtr, ytr);
			std::cout<<"Problem automatically set to: "<<probList[prob];
			gurlsWrap->setProblemType(prob);}

		gurlsWrap->train(Xtr, ytr);
		
		if(savefile!="")
			retopt->save(savefile);

		delete gurlsWrap;
		return retopt;
    }

    catch (gException& e)
    {
        std::cout << e.getMessage() << std::endl;
		delete gurlsWrap;
        throw e;
    }
}
Esempio n. 4
0
void RLSlinear::trainModel(gMat2D<float> &X, gMat2D<float> &Y, bool newGURLS)
{
    if(modelLinearRLS!=NULL)
        delete modelLinearRLS;

    if (newGURLS==false)
    {
        OptTaskSequence *seq = new OptTaskSequence();

        OptProcess* process_train = new OptProcess();
        OptProcess* process_predict = new OptProcess();

        *seq << "kernel:linear" << "split:ho" << "paramsel:hodual";
        *process_train << GURLS::computeNsave << GURLS::compute << GURLS::computeNsave;
        *process_predict << GURLS::load << GURLS::ignore << GURLS::load;

        *seq<< "optimizer:rlsdual"<< "pred:dual";
        *process_train<<GURLS::computeNsave<<GURLS::ignore;
        *process_predict<<GURLS::load<<GURLS::computeNsave;

        GurlsOptionsList * processes = new GurlsOptionsList("processes", false);
        processes->addOpt("train",process_train);
        processes->addOpt("test",process_predict);

        modelLinearRLS = new GurlsOptionsList(className, true);

        modelLinearRLS->addOpt("seq", seq);
        modelLinearRLS->addOpt("processes", processes);

        RLS.run(X,Y,*modelLinearRLS,"train");

    }
    else
    {
        unsigned long n = X.rows();
        unsigned long d = X.cols();
        unsigned long t = Y.cols(); // should be 1

        modelLinearRLS = train(X.getData(), Y.getData(), n, d, t, "krls", "linear");
    }

}
Esempio n. 5
0
void RecursiveRLSWrapper<T>::retrain()
{
    GurlsOptionsList* kernel = this->opt->template getOptAs<GurlsOptionsList>("kernel");
    const gMat2D<T> &Xva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xva");
    const gMat2D<T> &yva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("yva");

    const unsigned long nva = Xva.rows();

    this->opt->removeOpt("paramsel");
    this->opt->removeOpt("optimizer");

    GurlsOptionsList* split = this->opt->template getOptAs<GurlsOptionsList>("split");
    split->removeOpt("indices");
    split->removeOpt("lasts");


    gMat2D<unsigned long>* indices = new gMat2D<unsigned long>(1, nTot);
    gMat2D<unsigned long>* lasts = new gMat2D<unsigned long>(1, 1);

    set(indices->getData(), 0ul, nTot-nva);
    unsigned long * it = indices->getData() + (nTot-nva);
    for(unsigned long i=0; i<nva; ++i, ++it)
        *it = i;

    lasts->getData()[0] = (nTot-nva);

    split->addOpt("indices", new OptMatrix<gMat2D<unsigned long> >(*indices));
    split->addOpt("lasts", new OptMatrix<gMat2D<unsigned long> >(*lasts));


    ParamSelHoPrimal<T> paramselTask;
    this->opt->addOpt("paramsel", paramselTask.execute(Xva, yva, *(this->opt)));


    RLSPrimalRecInit<T> optimizerTask;
    gMat2D<T> emptyMat;
    this->opt->addOpt("nTot", new OptNumber(nTot));
    this->opt->addOpt("optimizer", optimizerTask.execute(emptyMat, emptyMat, *(this->opt)));
    this->opt->removeOpt("nTot");

}
Esempio n. 6
0
/**
  * Main function
  */
int main(int argc, char *argv[])
{
    string xtr_file, xte_file, ytr_file, yte_file;

    // check that all inputs are given
    if(argc<4)
    {
        std::cout << "========================================================================================"<< std::endl
                  << " Wrong parameters number ("<<argc <<")." << std::endl
                  << " Provide a valid path for training, test and output files using the following syntax:" << std::endl
                  << " \n\n\t " << argv[0] << " xtr xte ytr yte" << std::endl
                  << "========================================================================================" << std::endl << std::endl;
        return 0;
    }

    // get file names from input
    xtr_file = argv[1];
    xte_file = argv[2];
    ytr_file = argv[3];
    yte_file = argv[4];

    try
    {
        gMat2D<T> Xtr, Xte, ytr, yte;

        // load data from file
        Xtr.readCSV(xtr_file);
        Xte.readCSV(xte_file);
        ytr.readCSV(ytr_file);
        yte.readCSV(yte_file);

        // specify the task sequence
        OptTaskSequence *seq = new OptTaskSequence();
        *seq << "paramsel:loocvprimal" << "optimizer:rlsprimal" << "pred:primal" << "perf:macroavg";
        // *seq << "kernel:linear" << "paramsel:loocvdual" << "optimizer:rlsdual" << "pred:dual" << "perf:macroavg";
        // *seq << "paramsel:siglam"
        //      << "kernel:rbf"
        //      << "optimizer:rlsdual"
        //      << "predkernel:traintest"
        //      << "pred:dual"
        //      << "perf:macroavg";

        GurlsOptionsList * process = new GurlsOptionsList("processes", false);

        // defines instructions for training process
        OptProcess* process1 = new OptProcess();
        *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::ignore << GURLS::ignore;
        // *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave << GURLS::ignore << GURLS::ignore;
        // *process1 << GURLS::computeNsave
        //           << GURLS::computeNsave
        //           << GURLS::computeNsave
        //           << GURLS::ignore
        //           << GURLS::ignore
        //           << GURLS::ignore;
        process->addOpt("one", process1);

        // defines instructions for testing process
        OptProcess* process2 = new OptProcess();
        *process2 << GURLS::load << GURLS::load<< GURLS::computeNsave << GURLS::computeNsave;
        // *process2 << GURLS::load << GURLS::load << GURLS::load<< GURLS::compute << GURLS::compute;
        // *process2 << GURLS::load
        //           << GURLS::load
        //           << GURLS::ignore
        //           << GURLS::compute
        //           << GURLS::compute
        //           << GURLS::compute;
        process->addOpt("two", process2);

        // build an options' structure
        GurlsOptionsList* opt = new GurlsOptionsList("GURLSlooprimal", true);
        opt->addOpt("seq", seq);
        opt->addOpt("processes", process);

        GURLS G;

        string jobId0("one");
        string jobId1("two");

        clock_t    start = std::clock();

        // run gurls for training
        G.run(Xtr, ytr, *opt, jobId0);

        // run gurls for testing
        G.run(Xte, yte, *opt, jobId1);

        cout << "Elased time is " << (clock() - start) / (double)(CLOCKS_PER_SEC) << endl;

        cout << "Accuracy: ";
        cout << OptMatrix<gMat2D<T> >::dynacast(GurlsOptionsList::dynacast(opt->getOpt("perf"))->getOpt("acc"))->getValue()[0][0] << endl;


    }
    catch (gException& e)
    {
        cout << e.getMessage() << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;

}
Esempio n. 7
0
void RecursiveRLSWrapper<T>::update(const gVec<T> &X, const gVec<T> &y)
{
    if(!this->trainedModel())
        throw gException("Error, Train Model First");

    RLSPrimalRecUpdate<T> optimizer;

    const unsigned long d = X.getSize();
    const unsigned long t = y.getSize();

    gMat2D<T>X_mat(1, d);
    copy(X_mat.getData(), X.getData(), d);
    gMat2D<T>y_mat(1, t);
    copy(y_mat.getData(), y.getData(), t);

    GurlsOptionsList* ret = optimizer.execute(X_mat, y_mat, *(this->opt));
    this->opt->removeOpt("optimizer");
    this->opt->addOpt("optimizer", ret);

    ++nTot;

    gMat2D<T>* xtx = new gMat2D<T>(d,d);
    gMat2D<T>* xty = new gMat2D<T>(d,t);

    dot(X.getData(), X.getData(), xtx->getData(), 1, d, 1, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor);
    dot(X.getData(), y.getData(), xty->getData(), 1, d, 1, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);

    GurlsOptionsList* kernel = this->opt->template getOptAs<GurlsOptionsList>("kernel");

    const gMat2D<T>& XtX = kernel->getOptValue<OptMatrix<gMat2D<T> > >("XtX");
    const gMat2D<T>& Xty = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xty");

    axpy(d*d, (T)1.0, XtX.getData(), 1, xtx->getData(), 1);
    axpy(d*t, (T)1.0, Xty.getData(), 1, xty->getData(), 1);

    kernel->removeOpt("XtX");
    kernel->addOpt("XtX", new OptMatrix<gMat2D<T> >(*xtx));

    kernel->removeOpt("Xty");
    kernel->addOpt("Xty", new OptMatrix<gMat2D<T> >(*xty));


    unsigned long proportion = static_cast<unsigned long>(gurls::round(1.0/this->opt->getOptAsNumber("hoproportion")));

    if(nTot % proportion == 0)
    {
        const gMat2D<T>& Xva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("Xva");
        const gMat2D<T>& yva = kernel->getOptValue<OptMatrix<gMat2D<T> > >("yva");

        const unsigned long nva = Xva.rows();
        const unsigned long nva_new = nva+1;


        gMat2D<T>* Xva_new = new gMat2D<T>(nva_new, d);

        const T* old_it = Xva.getData();
        T* new_it = Xva_new->getData();
        for(const T* end = new_it+(nva_new*d); new_it< end; old_it+=nva, new_it +=nva_new)
            copy(new_it, old_it, nva);

        copy(Xva_new->getData()+nva, X.getData(), d, nva_new, 1);

        kernel->removeOpt("Xva");
        kernel->addOpt("Xva", new OptMatrix<gMat2D<T> >(*Xva_new));


        gMat2D<T>* yva_new = new gMat2D<T>(nva_new, t);

        old_it = yva.getData();
        new_it = yva_new->getData();
        for(const T* end = new_it+(nva_new*t); new_it< end; old_it+=nva, new_it +=nva_new)
            copy(new_it, old_it, nva);

        copy(yva_new->getData()+nva, y.getData(), t, nva_new, 1);

        kernel->removeOpt("yva");
        kernel->addOpt("yva", new OptMatrix<gMat2D<T> >(*yva_new));

    }
}
Esempio n. 8
0
/**
  * Main function
  */
int main(int argc, char *argv[])
{
    string xtr_file, xte_file, ytr_file, yte_file;

    // check that all inputs are given
    if(argc<4)
    {
        std::cout << "========================================================================================"<< std::endl
        << " Wrong parameters number ("<<argc <<")." << std::endl
        << " Provide a valid path for training, test and output files using the following syntax:" << std::endl
        << " \n\n\t " << argv[0] << " xtr xte ytr yte" << std::endl
        << "========================================================================================" << std::endl << std::endl;
        return 0;
    }

    // get file names from input
    xtr_file = argv[1];
    xte_file = argv[2];
    ytr_file = argv[3];
    yte_file = argv[4];

    try
    {
        gMat2D<T> *Xtr, *Xte, *ytr, *yte;

        // load data from file
        Xtr = readFile<T>(xtr_file);
        Xte = readFile<T>(xte_file);
        ytr = readFile<T>(ytr_file);
        yte = readFile<T>(yte_file);

        // specify the task sequence
        OptTaskSequence *seq = new OptTaskSequence();
        seq->addTask("paramsel:loocvprimal");
        seq->addTask("optimizer:rlsprimal");
        seq->addTask("pred:primal");
        seq->addTask("perf:macroavg");
        seq->addTask("perf:precrec");

        GurlsOptionsList * process = new GurlsOptionsList("processes", false);
        // defines instructions for training process
        std::vector<double> process1;
        process1.push_back(GURLS::computeNsave);
        process1.push_back(GURLS::computeNsave);
        process1.push_back(GURLS::ignore);
        process1.push_back(GURLS::ignore);
        process1.push_back(GURLS::ignore);
        process->addOpt("one", new OptNumberList(process1));

        // defines instructions for testing process
        std::vector<double> process2;
        process2.push_back(GURLS::load);
        process2.push_back(GURLS::load);
        process2.push_back(GURLS::computeNsave);
        process2.push_back(GURLS::computeNsave);
        process2.push_back(GURLS::computeNsave);
        process->addOpt("two", new OptNumberList(process2));

        // build an options' structure
        GurlsOptionsList* opt = new GurlsOptionsList("GURLSlooprimal", false);
        opt->addOpt("seq", seq);
        opt->addOpt("processes", process);

        GURLS G;

        string jobId0("one");
        string jobId1("two");

        // run gurls for training
        G.run(*Xtr, *ytr, *opt, jobId0);

        // run gurls for testing
        G.run(*Xte, *yte, *opt, jobId1);

    }
    catch (gException& e)
    {
        cout << e.getMessage() << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;

}
Esempio n. 9
0
void KernelRLSWrapper<T>::train(const gMat2D<T> &X, const gMat2D<T> &y)
{
    this->opt->removeOpt("split");
    this->opt->removeOpt("optimizer");


    const unsigned long nlambda = static_cast<unsigned long>(this->opt->getOptAsNumber("nlambda"));
    const unsigned long nsigma = static_cast<unsigned long>(this->opt->getOptAsNumber("nsigma"));

    OptTaskSequence *seq = new OptTaskSequence();
    GurlsOptionsList * process = new GurlsOptionsList("processes", false);
    OptProcess* process1 = new OptProcess();
    process->addOpt("one", process1);
    this->opt->addOpt("seq", seq);
    this->opt->addOpt("processes", process);

    if(this->kType == KernelWrapper<T>::LINEAR)
    {
        if(nlambda > 1ul)
        {
            *seq << "split:ho" << "kernel:linear" << "paramsel:hodual";
            *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave;
        }
        else if(nlambda == 1ul)
        {
            if(this->opt->hasOpt("paramsel.lambdas"))
            {
                *seq << "kernel:linear";
                *process1 << GURLS::computeNsave;
             }
            else
                throw gException("Please set a valid value for the regularization parameter, calling setParam(value)");
        }
        else
            throw gException("Please set a valid value for NParam, calling setNParam(value)");

    }
    else if(this->kType == KernelWrapper<T>::RBF)
    {
        if(nlambda > 1ul)
        {
            if(nsigma > 1ul)
            {
                *seq << "split:ho" << "paramsel:siglamho" << "kernel:rbf";
                *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave;
            }
            else if(nsigma == 1ul)
            {
                if(this->opt->hasOpt("paramsel.sigma"))
                {
                    *seq << "split:ho" << "kernel:rbf" << "paramsel:hodual";
                    *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave;
                }
                else
                    throw gException("Please set a valid value for the kernel parameter, calling setSigma(value)");
            }
            else
                throw gException("Please set a valid value for NSigma, calling setNSigma(value)");
        }
        else if(nlambda == 1ul)
        {
            if(nsigma == 1ul)
            {
                if(this->opt->hasOpt("paramsel.sigma") && this->opt->hasOpt("paramsel.lambdas"))
                {
                    *seq << "split:ho" << "kernel:rbf";
                    *process1 << GURLS::computeNsave << GURLS::computeNsave;
                }
                else
                    throw gException("Please set a valid value for kernel and regularization parameters, calling setParam(value) and setSigma(value)");
            }
            else
                throw gException("Please set a valid value for NSigma, calling setNSigma(value)");
        }
        else
            throw gException("Please set a valid value for NParam, calling setNParam(value)");
    }

    *seq << "optimizer:rlsdual";
    *process1 << GURLS::computeNsave;

    GURLS G;
    G.run(X, y, *(this->opt), "one");

}
Esempio n. 10
0
int main(int argc, char *argv[])
{
    srand(static_cast<unsigned int>(time(NULL)));

    cout.precision(4);
    cout.width(11);
    cout << fixed << right << endl;

    string input_folder("../data");
    if (argc < 2)
    {
        cout << "========================================================================================"<< endl
             << " WARNING: No input folder provided. " << endl
             << " Using the default option: \'" << input_folder <<  "\'" << endl
             <<    " Please make sure such folder exists or provide a valid path using the following syntax:" << endl
             << " " << argv[0]
             << " <path-to-a-valid-input-folder>" << endl
             << "========================================================================================" << endl << endl;
    }
    else
        input_folder = argv[1];


    try
    {
        gMat2D<T> Xtr, Xte, ytr, yte;

        Xtr.readCSV(input_folder+"/Xtr.txt");
        Xte.readCSV(input_folder+"/Xte.txt");
        ytr.readCSV(input_folder+"/ytr.txt");
        yte.readCSV(input_folder+"/yte.txt");

//        cout << "Xtr = " << endl << Xtr << endl;
//        cout << "Xte = " << endl << Xte << endl;


        GurlsOptionsList opt("ExampleExperiment", true);

        OptTaskSequence *seq = new OptTaskSequence();
        *seq << "paramsel:fixsiglam" << "kernel:rbf" << "optimizer:rlsdual" << "predkernel:traintest" << "pred:dual" << "perf:macroavg";
        opt.addOpt("seq", seq);


        GurlsOptionsList * process = new GurlsOptionsList("processes", false);

        OptProcess* process1 = new OptProcess();
        *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave << GURLS::ignore << GURLS::ignore << GURLS::ignore;
        process->addOpt("one", process1);

        OptProcess* process2 = new OptProcess();
        *process2 << GURLS::load << GURLS::load << GURLS::load << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave;
        process->addOpt("two", process2);

        opt.addOpt("processes", process);

        std::cout << opt << std::endl;

        std::string jobid1("one");
        std::string jobid2("two");

        GURLS G;

        G.run(Xtr, ytr, opt, jobid1);

        std::cout << std::endl;

        G.run(Xte, yte, opt, jobid2);

        std::cout << opt << std::endl;

//        opt.save("par1.txt");
//        GurlsOptionsList *s1 = new GurlsOptionsList("dummy", false);
//        s1->load("par1.txt");
//        std::cout << *s1 << std::endl;

    }
    catch (gException& e)
    {
        cout << e.getMessage() << endl;
        return -1;
    }


    return 0;

}
Esempio n. 11
0
void GurlsOptionsList::copyOpt(string key, const GurlsOptionsList &from)
{

    const GurlsOption* toCopy = from.getOpt(key);

    GurlsOption* newOpt = NULL;

    switch(toCopy->getType())
    {
    case StringOption:
        newOpt = new OptString(OptString::dynacast(toCopy)->getValue());
        break;
    case NumberOption:
        newOpt = new OptNumber(OptNumber::dynacast(toCopy)->getValue());
        break;
    case StringListOption:
        newOpt = new OptStringList(OptStringList::dynacast(toCopy)->getValue());
        break;
    case NumberListOption:
        newOpt = new OptNumberList(OptNumberList::dynacast(toCopy)->getValue());
        break;
    case FunctionOption:
        newOpt = new OptFunction(OptFunction::dynacast(toCopy)->getName());
        break;
    case ProcessOption:
        newOpt = new OptProcess(*OptProcess::dynacast(toCopy));
        break;
    case MatrixOption:
    case VectorOption:
    {
       const OptMatrixBase* base = dynamic_cast<const OptMatrixBase*>(toCopy);

        if(base == NULL)
            throw gException(Exception_Illegal_Dynamic_Cast);

#ifdef _BGURLS
        if(base->hasBigArray())
        {
            switch(base->getMatrixType())
            {
                case OptMatrixBase::ULONG:
                    newOpt = copyOptMatrix<BigArray<unsigned long> >(toCopy);
                    break;
                case OptMatrixBase::FLOAT:
                    newOpt = copyOptMatrix<BigArray<float> >(toCopy);
                    break;
                case OptMatrixBase::DOUBLE:
                    newOpt = copyOptMatrix<BigArray<double> >(toCopy);
                    break;
            }
        }
        else
#endif
        {
            switch(base->getMatrixType())
            {
                case OptMatrixBase::ULONG:
                    newOpt = copyOptMatrix<gMat2D<unsigned long> >(toCopy);
                    break;
                case OptMatrixBase::FLOAT:
                    newOpt = copyOptMatrix<gMat2D<float> >(toCopy);
                    break;
                case OptMatrixBase::DOUBLE:
                    newOpt = copyOptMatrix<gMat2D<double> >(toCopy);
                    break;
            }
        }

    }
        break;
    case OptListOption:
    {
        const GurlsOptionsList* toCopy_list = GurlsOptionsList::dynacast(toCopy);

        newOpt = new GurlsOptionsList(*toCopy_list);
    }
        break;
    case OptArrayOption:
        newOpt = new OptArray(*OptArray::dynacast(toCopy));
        break;
    case TaskSequenceOption:
        newOpt = new OptTaskSequence(*OptTaskSequence::dynacast(toCopy));
        break;
    case TaskOption:
        newOpt = new OptTask(*OptTask::dynacast(toCopy));
        break;
    case TaskIDOption:
    case GenericOption:
        break;
    }

    if(newOpt != NULL)
        addOpt(key, newOpt);
}
/**
  * Main function
  */
int main(int argc, char *argv[])
{
    string xtr_file, xte_file, ytr_file, yte_file;

    // check that all inputs are given
    if(argc<4)
    {
        std::cout << "========================================================================================"<< std::endl
        << " Wrong parameters number ("<<argc <<")." << std::endl
        << " Provide a valid path for training, test and output files using the following syntax:" << std::endl
        << " \n\n\t " << argv[0] << " xtr xte ytr yte" << std::endl
        << "========================================================================================" << std::endl << std::endl;
        return 0;
    }

    // get file names from input
    xtr_file = argv[1];
    xte_file = argv[2];
    ytr_file = argv[3];
    yte_file = argv[4];

    try
    {
        gMat2D<T> Xtr, Xte, ytr, yte;

        // load data from file
        Xtr.readCSV(xtr_file);
        Xte.readCSV(xte_file);
        ytr.readCSV(ytr_file);
        yte.readCSV(yte_file);

        // specify the task sequence
        OptTaskSequence *seq = new OptTaskSequence();
        OptTaskSequence *seq2 = new OptTaskSequence();
        
        *seq << "paramsel:loocvprimal" << "optimizer:rlsprimal" << "pred:primal"  << "perf:precrec";
        *seq2 << "pred:primal"<< "perf:macroavg";


        GurlsOptionsList * process = new GurlsOptionsList("processes", false);
        GurlsOptionsList * processbis = new GurlsOptionsList("processes2", false);

        // defines instructions for training process
        OptProcess* process1 = new OptProcess();
        *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::ignore << GURLS::ignore;
        process->addOpt("one", process1);

        // defines instructions for testing process
        OptProcess* process2 = new OptProcess();
        *process2 << GURLS::load << GURLS::load << GURLS::computeNsave << GURLS::computeNsave;
        process->addOpt("two", process2);        // defines instructions for testing process
 
        OptProcess* process3 = new OptProcess();
        *process3 << GURLS::load << GURLS::computeNsave;
        processbis->addOpt("three", process3);

        // build an options' structure
        GurlsOptionsList* opt = new GurlsOptionsList("GURLSlooprimal", true);
        opt->addOpt("seq", seq);
        opt->addOpt("processes", process);
        

        
        GURLS G;

        string jobId0("one");
        string jobId1("two");
        string jobId2("three");

        // run gurls for training
        G.run(Xtr, ytr, *opt, jobId0);

        // run gurls for testing with precrec
        G.run(Xte, yte, *opt, jobId1);
        

        
        GurlsOptionsList* opt2 = new GurlsOptionsList(*opt);    
        opt2->removeOpt("seq");
        opt2->addOpt("seq", seq2);
        opt2->removeOpt("processes");        
        opt2->addOpt("processes", processbis);
        // run gurls for testing with macroavg
        G.run(Xte, yte, *opt2, jobId2);
        
        // Results visualization
        /*** Debug code
        
        opt->printAll();
        int optType =  (*(opt->getOpt("perf.acc"))).getType();        // Get referenced option type
        OptMatrix<gMat2D<double> >* temp = (opt->getOptAs<OptMatrix<gMat2D<double> > >("perf.acc"));
        int ty = temp->getMatrixType();
        cout << "Matrix type: " << ty << endl;
        double *acc = temp->getValue().getData();
        
        ***/

        
        double *acc_precrec = (opt->getOptAs<OptMatrix<gMat2D<double> > >("perf.ap"))->getValue().getData();
        
        cout << "precision recall (perf.ap):" << endl;
        // Print accuracy values
        for (int i=0 ; i<4 ; i++)
            cout << acc_precrec[i] << "\t";
        cout << endl;
        
        
        double *acc_macroavg = (opt2->getOptAs<OptMatrix<gMat2D<double> > >("perf.acc"))->getValue().getData();
        
        cout << "Macro-average (perf.acc):" << endl;
        // Print accuracy values
        for (int i=0 ; i<4 ; i++)
            cout << acc_macroavg[i] << "\t";
        cout << endl;


    }
    catch (gException& e)
    {
        cout << e.getMessage() << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;

}
Esempio n. 13
0
int main(int argc, char *argv[])
{
    if(argc < 3)
    {
        cout << "Usage: " << argv[0] << " <input dir> <shared dir>." << endl;
        cout << "\t - <input dir> is the directory where the csv data files reside" << endl;
        cout << "\t - <shared dir> is a directory accessible by all processes (all processes must be able to access the same path)" << endl;

        return 0;
    }

    srand(static_cast<unsigned int>(time(NULL)));

    cout.precision(4);
    cout.width(11);
    cout << fixed << right << endl;

    path input_directory(argv[1]);
    path shared_directory(argv[2]);

    int numprocs;
    int myid;

    MPI_Init(&argc, &argv);

//     Find out the number of processes
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

//     Get the process rank
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);


    if(myid ==0)
        cout << "Loading Xtr..." << endl;

    BigArray<T> Xtr(path(shared_directory / "Xtr.h5").native(), 0, 0);
    Xtr.readCSV(path(input_directory / "Xtr.txt").native());

    if(myid ==0)
        cout << "Loading Xte..." << endl;

    BigArray<T> Xte(path(shared_directory / "Xte.h5").native(), 0, 0);
    Xte.readCSV(path(input_directory / "Xte.txt").native());

    if(myid ==0)
        cout << "Loading ytr..." << endl;

    BigArray<T> ytr(path(shared_directory / "ytr.h5").native(), 0, 0);
    ytr.readCSV(path(input_directory / "ytr.txt").native());

    if(myid ==0)
        cout << "Loading yte..." << endl;

    BigArray<T> yte(path(shared_directory / "yte.h5").native(), 0, 0);
    yte.readCSV(path(input_directory / "yte.txt").native());


    BGurlsOptionsList opt("examplebgurls", shared_directory.native(), true);

    // remove old experiments results
    if(myid == 0 && boost::filesystem::exists(path(opt.getOptAsString("savefile"))))
        boost::filesystem::remove(path(opt.getOptAsString("savefile")));

    OptTaskSequence *seq = new OptTaskSequence();
    *seq << "bigsplit:ho" << "bigparamsel:hoprimal" << "bigoptimizer:rlsprimal" << "bigpred:primal" << "bigperf:macroavg";
    opt.addOpt("seq", seq);


    GurlsOptionsList * process = new GurlsOptionsList("processes", false);

    OptProcess* process1 = new OptProcess();
    *process1 << GURLS::computeNsave << GURLS::computeNsave << GURLS::computeNsave << GURLS::ignore << GURLS::ignore;
    process->addOpt("one", process1);

    OptProcess* process2 = new OptProcess();
    *process2 << GURLS::load << GURLS::load << GURLS::load << GURLS::computeNsave << GURLS::computeNsave;
    process->addOpt("two", process2);

    opt.addOpt("processes", process);

    if(myid ==0)
        std::cout << opt << std::endl;


    std::string jobid1("one");
    std::string jobid2("two");

    BGURLS G;

//    Run bgurls on the training set
//    cout << "---Training..." << endl;
    G.run(Xtr, ytr, opt, jobid1);
//    cout << "---Training done..." << endl;

//    if(myid ==0)
//        std::cout << opt << std::endl;

//    Run bgurls on the test set
//    cout << "---Testing..." << endl;
    G.run(Xte, yte, opt, jobid2);
//    cout << "---Testing done..." << endl;

    if(myid ==0)
    {
        const gMat2D<T>& acc = opt.getOptValue<OptMatrix<gMat2D<T> > >("perf.acc");
        const int accs = acc.getSize();

        std::cout.precision(4);

        std::cout << std::endl << "Prediction accurcay is:" << std::endl;

        std::cout << "\t";
        for(int i=1; i<= accs; ++i)
            std::cout << "Class " << i << "\t";

        std::cout << std::endl << "\t";

        for(int i=0; i< accs; ++i)
            std::cout << acc.getData()[i]*100.0 << "%\t";

        std::cout << std::endl;

    }

    BigArray<T>::releaseMPIData();
    MPI_Finalize();

    return EXIT_SUCCESS;

}