123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /**
- * @file TestGPHIKRegression.cpp
- * @brief CppUnit-Testcase to verify that GPHIKRegression works as desired.
- * @author Alexander Freytag
- * @date 16-01-2014 (dd-mm-yyyy)
- */
- #ifdef NICE_USELIB_CPPUNIT
- // STL includes
- #include <iostream>
- #include <vector>
- // NICE-core includes
- #include <core/basics/Config.h>
- #include <core/basics/Timer.h>
- // gp-hik-core includes
- #include "gp-hik-core/GPHIKRegression.h"
- #include "TestGPHIKRegression.h"
- using namespace std; //C basics
- using namespace NICE; // nice-core
- const bool verboseStartEnd = true;
- const bool verbose = false;
- CPPUNIT_TEST_SUITE_REGISTRATION( TestGPHIKRegression );
- void TestGPHIKRegression::setUp() {
- }
- void TestGPHIKRegression::tearDown() {
- }
- void readData ( const std::string filename, NICE::Matrix & data, NICE::Vector & yValues )
- {
- std::ifstream ifs ( filename.c_str() , ios::in );
- if ( ifs.good() )
- {
- NICE::Vector tmp;
- ifs >> data;
- ifs >> tmp; //yBin;
- ifs >> yValues;
- ifs.close();
- }
- else
- {
- std::cerr << "Unable to read data from file " << filename << " -- aborting." << std::endl;
- CPPUNIT_ASSERT ( ifs.good() );
- }
- }
- void evaluateRegressionMethod ( double & regressionLoss,
- const NICE::GPHIKRegression * regressionMethod,
- const NICE::Matrix & data,
- const NICE::Vector & yValues
- )
- {
- regressionLoss = 0.0;
-
- int i_loopEnd ( (int)data.rows() );
-
- for (int i = 0; i < i_loopEnd ; i++)
- {
- NICE::Vector example ( data.getRow(i) );
- double result;
-
- // classify with previously trained regression method
- regressionMethod->estimate( &example, result );
-
- if ( verbose )
- std::cerr << "i: " << i << " gt: " << yValues[i] << " result: " << result << std::endl;
-
- //use L2-loss for evaluation
- regressionLoss += pow( yValues[i] - result, 2 );
- }
- }
- void TestGPHIKRegression::testRegressionHoldInData()
- {
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKRegression::testRegressionHoldInData ===================== " << std::endl;
-
- NICE::Config conf;
-
- conf.sB ( "GPHIKRegression", "eig_verbose", false);
- conf.sS ( "GPHIKRegression", "optimization_method", "downhillsimplex");
- // set pretty low built-in noise for hold-in regression estimation
- conf.sD ( "GPHIKRegression", "noise", 1e-6 );
-
- std::string s_trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
-
- //------------- read the training data --------------
-
- NICE::Matrix dataTrain;
- NICE::Vector yValues;
-
- readData ( s_trainData, dataTrain, yValues );
-
- //----------------- convert data to sparse data structures ---------
- std::vector< const NICE::SparseVector *> examplesTrain;
- examplesTrain.resize( dataTrain.rows() );
-
- std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
- for (int i = 0; i < (int)dataTrain.rows(); i++, exTrainIt++)
- {
- *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
- }
-
- //create classifier object
- NICE::GPHIKRegression * regressionMethod;
- regressionMethod = new NICE::GPHIKRegression ( &conf );
- regressionMethod->train ( examplesTrain , yValues );
-
- double holdInLoss ( 0.0 );
-
-
- // ------------------------------------------
- // ------------- REGRESSION --------------
- // ------------------------------------------
- evaluateRegressionMethod ( holdInLoss, regressionMethod, dataTrain, yValues );
-
-
- if ( verbose )
- {
- std::cerr << " holdInLoss: " << holdInLoss << std::endl;
- }
-
- CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, holdInLoss, 1e-8);
-
- // don't waste memory
-
- delete regressionMethod;
-
- for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
- {
- delete *exTrainIt;
- }
-
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKRegression::testRegressionHoldInData done ===================== " << std::endl;
- }
- void TestGPHIKRegression::testRegressionHoldOutData()
- {
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKRegression::testRegressionHoldOutData ===================== " << std::endl;
- NICE::Config conf;
-
- conf.sB ( "GPHIKRegression", "eig_verbose", false);
- conf.sS ( "GPHIKRegression", "optimization_method", "downhillsimplex");
- // set pretty low built-in noise for hold-in regression estimation
- conf.sD ( "GPHIKRegression", "noise", 1e-6 );
-
- std::string s_trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
-
- //------------- read the training data --------------
-
- NICE::Matrix dataTrain;
- NICE::Vector yValues;
-
- readData ( s_trainData, dataTrain, yValues );
-
- //----------------- convert data to sparse data structures ---------
- std::vector< const NICE::SparseVector *> examplesTrain;
- examplesTrain.resize( dataTrain.rows() );
-
- std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
- for (int i = 0; i < (int)dataTrain.rows(); i++, exTrainIt++)
- {
- *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
- }
-
- //create classifier object
- NICE::GPHIKRegression * regressionMethod;
- regressionMethod = new NICE::GPHIKRegression ( &conf );
- regressionMethod->train ( examplesTrain , yValues );
-
- //------------- read the test data --------------
-
-
- NICE::Matrix dataTest;
- NICE::Vector yValuesTest;
-
- std::string s_testData = conf.gS( "main", "testData", "toyExampleTest.data" );
-
- readData ( s_testData, dataTest, yValuesTest );
-
- double holdOutLoss ( 0.0 );
-
-
- // ------------------------------------------
- // ------------- REGRESSION --------------
- // ------------------------------------------
- evaluateRegressionMethod ( holdOutLoss, regressionMethod, dataTest, yValuesTest );
- // acceptable difference for every estimated y-value on average
- double diffOkay ( 0.35 );
-
- if ( verbose )
- {
- std::cerr << " holdOutLoss: " << holdOutLoss << " accepting: " << pow(diffOkay,2)*yValuesTest.size() << std::endl;
- }
-
- CPPUNIT_ASSERT( pow(diffOkay,2)*yValuesTest.size() - holdOutLoss > 0.0);
-
- // don't waste memory
-
- delete regressionMethod;
-
- for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
- {
- delete *exTrainIt;
- }
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKRegression::testRegressionHoldOutData done ===================== " << std::endl;
- }
-
- void TestGPHIKRegression::testRegressionOnlineLearning()
- {
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKRegression::testRegressionOnlineLearning ===================== " << std::endl;
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKRegression::testRegressionOnlineLearning done ===================== " << std::endl;
- }
- #endif
|