예제 #1
0
파일: primal.cpp 프로젝트: angcui/GURLS.jl
/**
  * 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;

}
예제 #2
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;

}
예제 #3
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;

}
예제 #4
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");

}
예제 #5
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();
        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;

}