123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790 |
- /**
- * @file TestGPHIKOnlineLearnable.cpp
- * @brief CppUnit-Testcase to verify that GPHIKClassifier methods herited from Persistent (store and restore) work as desired.
- * @author Alexander Freytag
- * @date 21-12-2013
- */
- #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/GPHIKClassifier.h"
- #include "gp-hik-core/GPHIKRawClassifier.h"
- #include "TestGPHIKOnlineLearnable.h"
- using namespace std; //C basics
- using namespace NICE; // nice-core
- const bool verboseStartEnd = true;
- const bool verbose = false;
- const bool writeClassifiersForVerification = false;
- CPPUNIT_TEST_SUITE_REGISTRATION( TestGPHIKOnlineLearnable );
- void TestGPHIKOnlineLearnable::setUp() {
- }
- void TestGPHIKOnlineLearnable::tearDown() {
- }
- void readData ( const std::string filename, NICE::Matrix & data, NICE::Vector & yBin, NICE::Vector & yMulti )
- {
- std::ifstream ifs ( filename.c_str() , ios::in );
- if ( ifs.good() )
- {
- ifs >> data;
- ifs >> yBin;
- ifs >> yMulti;
- ifs.close();
- }
- else
- {
- std::cerr << "Unable to read data from file " << filename << " -- aborting." << std::endl;
- CPPUNIT_ASSERT ( ifs.good() );
- }
- }
- void prepareLabelMappings (std::map< uint, uint > & mapClNoToIdxTrain,
- const GPHIKClassifier * classifier,
- std::map< uint,uint > & mapClNoToIdxTest,
- const NICE::Vector & yMultiTest
- )
- {
- // determine classes known during training and corresponding mapping
- // thereby allow for non-continous class labels
- std::set< uint > classesKnownTraining = classifier->getKnownClassNumbers();
-
- uint noClassesKnownTraining ( classesKnownTraining.size() );
- std::set< uint >::const_iterator clTrIt = classesKnownTraining.begin();
- for ( uint i=0; i < noClassesKnownTraining; i++, clTrIt++ )
- mapClNoToIdxTrain.insert ( std::pair< uint, uint > ( *clTrIt, i ) );
-
- // determine classes known during testing and corresponding mapping
- // thereby allow for non-continous class labels
- std::set< uint> classesKnownTest;
- classesKnownTest.clear();
-
- // determine which classes we have in our label vector
- // -> MATLAB: myClasses = unique(y);
- for ( NICE::Vector::const_iterator it = yMultiTest.begin(); it != yMultiTest.end(); it++ )
- {
- if ( classesKnownTest.find ( *it ) == classesKnownTest.end() )
- {
- classesKnownTest.insert ( *it );
- }
- }
-
- uint noClassesKnownTest ( classesKnownTest.size() );
- std::set< uint >::const_iterator clTestIt = classesKnownTest.begin();
- for ( uint i=0; i < noClassesKnownTest; i++, clTestIt++ )
- mapClNoToIdxTest.insert ( std::pair< uint,uint > ( *clTestIt, i ) );
- }
- void evaluateClassifier ( NICE::Matrix & confusionMatrix,
- const NICE::GPHIKClassifier * classifier,
- const NICE::Matrix & data,
- const NICE::Vector & yMulti,
- const std::map< uint,uint > & mapClNoToIdxTrain,
- const std::map< uint,uint > & mapClNoToIdxTest
- )
- {
- int i_loopEnd ( (int)data.rows() );
-
- for (int i = 0; i < i_loopEnd ; i++)
- {
- NICE::Vector example ( data.getRow(i) );
- NICE::SparseVector scores;
- uint result;
-
- // classify with incrementally trained classifier
- classifier->classify( &example, result, scores );
-
- confusionMatrix( mapClNoToIdxTrain.find(result)->second, mapClNoToIdxTest.find(yMulti[i])->second ) += 1.0;
- }
- }
- void evaluateClassifierRaw ( NICE::Matrix & confusionMatrix,
- const NICE::GPHIKRawClassifier * classifier,
- const NICE::Matrix & data,
- const NICE::Vector & yMulti,
- const std::map< uint,uint > & mapClNoToIdxTrain,
- const std::map< uint,uint > & mapClNoToIdxTest
- )
- {
- int i_loopEnd ( (int)data.rows() );
- for (int i = 0; i < i_loopEnd ; i++)
- {
- NICE::Vector example_nonsparse ( data.getRow(i) );
- NICE::SparseVector example (example_nonsparse);
- NICE::SparseVector scores;
- uint result;
- // classify with incrementally trained classifier
- classifier->classify( &example, result, scores );
- uint gtlabel = mapClNoToIdxTest.find(yMulti[i])->second;
- uint predlabel = mapClNoToIdxTrain.find(result)->second;
- confusionMatrix( gtlabel, predlabel ) += 1.0;
- }
- }
- void compareClassifierOutputs ( const NICE::GPHIKClassifier * classifier,
- const NICE::GPHIKClassifier * classifierScratch,
- const NICE::Matrix & data
- )
- {
- int i_loopEnd ( (int)data.rows() );
-
- for (int i = 0; i < i_loopEnd ; i++)
- {
- NICE::Vector example ( data.getRow(i) );
-
- NICE::SparseVector scores;
- uint result;
-
- // classify with incrementally trained classifier
- classifier->classify( &example, result, scores );
-
- NICE::SparseVector scoresScratch;
- uint resultScratch;
- classifierScratch->classify( &example, resultScratch, scoresScratch );
-
-
- bool equal(true);
- NICE::SparseVector::const_iterator itScores = scores.begin();
- NICE::SparseVector::const_iterator itScoresScratch = scoresScratch.begin();
- for ( ; itScores != scores.end(); itScores++, itScoresScratch++)
- {
- if ( fabs( itScores->second - itScoresScratch->second ) > 10e-3)
- {
- std::cerr << " itScores->second: " << itScores->second << " itScores->second: " << itScores->second << std::endl;
- equal = false;
- break;
- }
- }
-
- CPPUNIT_ASSERT_EQUAL ( equal, true );
- }
- }
- void compareClassifierOutputsRaw ( const NICE::GPHIKClassifier * classifier,
- const NICE::GPHIKRawClassifier * classifierRaw,
- const NICE::Matrix & data
- )
- {
- int i_loopEnd ( (int)data.rows() );
- for (int i = 0; i < i_loopEnd ; i++)
- {
- NICE::Vector example ( data.getRow(i) );
- NICE::SparseVector scores;
- uint result;
- // classify with incrementally trained classifier
- classifier->classify( &example, result, scores );
- NICE::SparseVector scoresRaw;
- uint resultRaw;
- NICE::SparseVector example_sparse(example);
- classifierRaw->classify( &example_sparse, resultRaw, scoresRaw );
- bool equal(true);
- NICE::SparseVector::const_iterator itScores = scores.begin();
- NICE::SparseVector::const_iterator itScoresScratch = scoresRaw.begin();
- for ( ; itScores != scores.end(); itScores++, itScoresScratch++)
- {
- if ( fabs( itScores->second - itScoresScratch->second ) > 10e-6)
- {
- std::cerr << " itScores->second: " << itScores->second << " itScores->second: " << itScores->second << std::endl;
- equal = false;
- break;
- }
- }
- CPPUNIT_ASSERT_EQUAL ( equal, true );
- }
- }
- void TestGPHIKOnlineLearnable::testOnlineLearningStartEmpty()
- {
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningStartEmpty ===================== " << std::endl;
-
- NICE::Config conf;
-
- conf.sB ( "GPHIKClassifier", "eig_verbose", false);
- conf.sS ( "GPHIKClassifier", "optimization_method", "none");
-
- std::string s_trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
-
- //------------- read the training data --------------
-
- NICE::Matrix dataTrain;
- NICE::Vector yBinTrain;
- NICE::Vector yMultiTrain;
-
- readData ( s_trainData, dataTrain, yBinTrain, yMultiTrain );
-
- //----------------- 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::GPHIKClassifier * classifier;
- classifier = new NICE::GPHIKClassifier ( &conf );
- bool performOptimizationAfterIncrement ( true );
- // add training samples, but without running training method first
- classifier->addMultipleExamples ( examplesTrain,yMultiTrain, performOptimizationAfterIncrement );
-
- // create second object trained in the standard way
- NICE::GPHIKClassifier * classifierScratch = new NICE::GPHIKClassifier ( &conf );
- classifierScratch->train ( examplesTrain, yMultiTrain );
-
-
- // TEST both classifiers to produce equal results
-
- //------------- read the test data --------------
-
-
- NICE::Matrix dataTest;
- NICE::Vector yBinTest;
- NICE::Vector yMultiTest;
-
- std::string s_testData = conf.gS( "main", "testData", "toyExampleTest.data" );
-
- readData ( s_testData, dataTest, yBinTest, yMultiTest );
-
- // ------------------------------------------
- // ------------- PREPARATION --------------
- // ------------------------------------------
-
- // determine classes known during training/testing and corresponding mapping
- // thereby allow for non-continous class labels
- std::map< uint,uint > mapClNoToIdxTrain;
- std::map< uint,uint > mapClNoToIdxTest;
- prepareLabelMappings (mapClNoToIdxTrain, classifier, mapClNoToIdxTest, yMultiTest);
-
-
- NICE::Matrix confusionMatrix ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
- NICE::Matrix confusionMatrixScratch ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
-
-
- // ------------------------------------------
- // ------------- CLASSIFICATION --------------
- // ------------------------------------------
- evaluateClassifier ( confusionMatrix, classifier, dataTest, yMultiTest,
- mapClNoToIdxTrain,mapClNoToIdxTest );
-
- evaluateClassifier ( confusionMatrixScratch, classifierScratch, dataTest, yMultiTest,
- mapClNoToIdxTrain,mapClNoToIdxTest );
-
-
- // post-process confusion matrices
- confusionMatrix.normalizeColumnsL1();
- double arr ( confusionMatrix.trace()/confusionMatrix.cols() );
- confusionMatrixScratch.normalizeColumnsL1();
- double arrScratch ( confusionMatrixScratch.trace()/confusionMatrixScratch.cols() );
-
- if ( verbose )
- {
- std::cerr << "confusionMatrix: " << confusionMatrix << std::endl;
-
- std::cerr << "confusionMatrixScratch: " << confusionMatrixScratch << std::endl;
- }
-
- CPPUNIT_ASSERT_DOUBLES_EQUAL( arr, arrScratch, 1e-8);
-
- // don't waste memory
-
- delete classifier;
- delete classifierScratch;
-
- for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
- {
- delete *exTrainIt;
- }
-
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningStartEmpty done ===================== " << std::endl;
- }
- void TestGPHIKOnlineLearnable::testOnlineLearningOCCtoBinary()
- {
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningOCCtoBinary ===================== " << std::endl;
-
- NICE::Config conf;
-
- conf.sB ( "GPHIKClassifier", "eig_verbose", false);
- conf.sS ( "GPHIKClassifier", "optimization_method", "none");
- conf.sB ( "GPHIKClassifier", "verbose", true);
-
- std::string s_trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
-
- //------------- read the training data --------------
-
- NICE::Matrix dataTrain;
- NICE::Vector yBinTrain;
- NICE::Vector yMultiTrain;
-
- readData ( s_trainData, dataTrain, yBinTrain, yMultiTrain );
-
- //----------------- convert data to sparse data structures ---------
- std::vector< const NICE::SparseVector *> examplesTrain;
- std::vector< const NICE::SparseVector *> examplesTrainPlus;
- std::vector< const NICE::SparseVector *> examplesTrainMinus;
-
- examplesTrain.resize( dataTrain.rows() );
-
- // to check whether non-consecutive and even wrongly odered class numbers work as well
- int clNoFirst ( 2 );
- int clNoSecond ( 0 );
-
- 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) );
-
- if ( yBinTrain[i] == 1 )
- {
- examplesTrainPlus.push_back ( *exTrainIt );
- yBinTrain[i] = clNoFirst;
- }
- else
- {
- examplesTrainMinus.push_back ( *exTrainIt );
- yBinTrain[i] = clNoSecond;
- }
- }
- NICE::Vector yBinPlus ( examplesTrainPlus.size(), clNoFirst ) ;
- NICE::Vector yBinMinus ( examplesTrainMinus.size(), clNoSecond );
-
-
- //create classifier object
- NICE::GPHIKClassifier * classifier;
- classifier = new NICE::GPHIKClassifier ( &conf );
- bool performOptimizationAfterIncrement ( true );
- // training with examples for positive class only
- classifier->train ( examplesTrainPlus, yBinPlus );
- // add samples for negative class, thereby going from OCC to binary setting
- classifier->addMultipleExamples ( examplesTrainMinus, yBinMinus, performOptimizationAfterIncrement );
-
- // create second object trained in the standard way
- NICE::GPHIKClassifier * classifierScratch = new NICE::GPHIKClassifier ( &conf );
- classifierScratch->train ( examplesTrain, yBinTrain );
-
- NICE::GPHIKRawClassifier * classifierScratchRaw = new NICE::GPHIKRawClassifier ( &conf );
- classifierScratchRaw->train ( examplesTrain, yBinTrain );
-
- // TEST both classifiers to produce equal results
-
- //------------- read the test data --------------
-
-
- NICE::Matrix dataTest;
- NICE::Vector yBinTest;
- NICE::Vector yMultiTest;
-
- std::string s_testData = conf.gS( "main", "testData", "toyExampleTest.data" );
-
- readData ( s_testData, dataTest, yBinTest, yMultiTest );
-
- // ------------------------------------------
- // ------------- PREPARATION --------------
- // ------------------------------------------
-
- // determine classes known during training/testing and corresponding mapping
- // thereby allow for non-continous class labels
- std::map< uint,uint > mapClNoToIdxTrain;
- std::map< uint,uint > mapClNoToIdxTest;
- prepareLabelMappings (mapClNoToIdxTrain, classifier, mapClNoToIdxTest, yMultiTest);
-
-
- NICE::Matrix confusionMatrix ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
- NICE::Matrix confusionMatrixScratch ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
- NICE::Matrix confusionMatrixScratchRaw ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
-
- // ------------------------------------------
- // ------------- CLASSIFICATION --------------
- // ------------------------------------------
- evaluateClassifier ( confusionMatrix, classifier, dataTest, yBinTest,
- mapClNoToIdxTrain, mapClNoToIdxTest );
-
- evaluateClassifier ( confusionMatrixScratch, classifierScratch, dataTest, yBinTest,
- mapClNoToIdxTrain, mapClNoToIdxTest );
-
- evaluateClassifierRaw ( confusionMatrixScratchRaw, classifierScratchRaw, dataTest, yBinTest,
- mapClNoToIdxTrain, mapClNoToIdxTest );
-
- // post-process confusion matrices
- confusionMatrix.normalizeColumnsL1();
- double arr ( confusionMatrix.trace()/confusionMatrix.cols() );
- confusionMatrixScratch.normalizeColumnsL1();
- double arrScratch ( confusionMatrixScratch.trace()/confusionMatrixScratch.cols() );
- confusionMatrixScratchRaw.normalizeColumnsL1();
- double arrScratchRaw ( confusionMatrixScratchRaw.trace()/confusionMatrixScratchRaw.cols() );
-
- if ( verbose )
- {
- std::cerr << "confusionMatrix: " << confusionMatrix << std::endl;
-
- std::cerr << "confusionMatrixScratch: " << confusionMatrixScratch << std::endl;
- std::cerr << "confusionMatrixScratchRaw: " << confusionMatrixScratchRaw << std::endl;
- }
-
- CPPUNIT_ASSERT_DOUBLES_EQUAL( arr, arrScratch, 1e-8);
- CPPUNIT_ASSERT_DOUBLES_EQUAL( arrScratch, arrScratchRaw, 1e-8);
- compareClassifierOutputsRaw(classifier, classifierScratchRaw, dataTest);
-
- // don't waste memory
-
- delete classifier;
- delete classifierScratch;
- delete classifierScratchRaw;
-
- for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
- {
- delete *exTrainIt;
- }
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningOCCtoBinary done ===================== " << std::endl;
- }
- void TestGPHIKOnlineLearnable::testOnlineLearningBinarytoMultiClass()
- {
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningBinarytoMultiClass ===================== " << std::endl;
- NICE::Config conf;
-
- conf.sB ( "GPHIKClassifier", "eig_verbose", false);
- conf.sS ( "GPHIKClassifier", "optimization_method", "downhillsimplex");
- // conf.sS ( "GPHIKClassifier", "optimization_method", "none");
-
- std::string s_trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
-
- //------------- read the training data --------------
-
- NICE::Matrix dataTrain;
- NICE::Vector yBinTrain;
- NICE::Vector yMultiTrain;
-
- readData ( s_trainData, dataTrain, yBinTrain, yMultiTrain );
-
- //----------------- convert data to sparse data structures ---------
- std::vector< const NICE::SparseVector *> examplesTrain;
- std::vector< const NICE::SparseVector *> examplesTrain12;
- std::vector< const NICE::SparseVector *> examplesTrain3;
-
- NICE::Vector yMulti12 ( yMultiTrain.size(), 1 ) ;
- NICE::Vector yMulti3 ( yMultiTrain.size(), 1 ) ;
- int cnt12 ( 0 );
- int cnt3 ( 0 );
-
- 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) );
-
- if ( ( yMultiTrain[i] == 0 ) || ( yMultiTrain[i] == 1 ) )
- {
- examplesTrain12.push_back ( *exTrainIt );
- yMulti12[ cnt12 ] = yMultiTrain[i];
- cnt12++;
- }
- else
- {
- examplesTrain3.push_back ( *exTrainIt );
- yMulti3[cnt3] = 2;
- cnt3++;
- }
- }
-
- yMulti12.resize ( examplesTrain12.size() );
- yMulti3.resize ( examplesTrain3.size() );
-
- //create classifier object
- NICE::GPHIKClassifier * classifier;
- classifier = new NICE::GPHIKClassifier ( &conf );
- bool performOptimizationAfterIncrement ( true );
- // training with examples for first and second class only
- classifier->train ( examplesTrain12, yMulti12 );
- // add samples for third class, thereby going from binary to multi-class setting
- classifier->addMultipleExamples ( examplesTrain3, yMulti3, performOptimizationAfterIncrement );
-
- // create second object trained in the standard way
- NICE::GPHIKClassifier * classifierScratch = new NICE::GPHIKClassifier ( &conf );
- classifierScratch->train ( examplesTrain, yMultiTrain );
-
-
- // TEST both classifiers to produce equal results
-
- //------------- read the test data --------------
-
-
- NICE::Matrix dataTest;
- NICE::Vector yBinTest;
- NICE::Vector yMultiTest;
-
- std::string s_testData = conf.gS( "main", "testData", "toyExampleTest.data" );
-
- readData ( s_testData, dataTest, yBinTest, yMultiTest );
-
- // ------------------------------------------
- // ------------- PREPARATION --------------
- // ------------------------------------------
-
- // determine classes known during training/testing and corresponding mapping
- // thereby allow for non-continous class labels
- std::map< uint,uint > mapClNoToIdxTrain;
- std::map< uint,uint > mapClNoToIdxTest;
- prepareLabelMappings (mapClNoToIdxTrain, classifier, mapClNoToIdxTest, yMultiTest);
-
-
- NICE::Matrix confusionMatrix ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
- NICE::Matrix confusionMatrixScratch ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
-
-
- // ------------------------------------------
- // ------------- CLASSIFICATION --------------
- // ------------------------------------------
-
- compareClassifierOutputs ( classifier, classifierScratch, dataTest );
- evaluateClassifier ( confusionMatrix, classifier, dataTest, yMultiTest,
- mapClNoToIdxTrain,mapClNoToIdxTest );
-
- evaluateClassifier ( confusionMatrixScratch, classifierScratch, dataTest, yMultiTest,
- mapClNoToIdxTrain,mapClNoToIdxTest );
-
-
- // post-process confusion matrices
- confusionMatrix.normalizeColumnsL1();
- double arr ( confusionMatrix.trace()/confusionMatrix.cols() );
- confusionMatrixScratch.normalizeColumnsL1();
- double arrScratch ( confusionMatrixScratch.trace()/confusionMatrixScratch.cols() );
-
- if ( verbose )
- {
- std::cerr << "confusionMatrix: " << confusionMatrix << std::endl;
-
- std::cerr << "confusionMatrixScratch: " << confusionMatrixScratch << std::endl;
- }
-
- CPPUNIT_ASSERT_DOUBLES_EQUAL( arr, arrScratch, 1e-8);
-
- // don't waste memory
-
- delete classifier;
- delete classifierScratch;
-
- for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
- {
- delete *exTrainIt;
- }
-
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningBinarytoMultiClass done ===================== " << std::endl;
- }
- void TestGPHIKOnlineLearnable::testOnlineLearningMultiClass()
- {
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningMultiClass ===================== " << std::endl;
-
- NICE::Config conf;
-
- conf.sB ( "GPHIKClassifier", "eig_verbose", false);
- conf.sS ( "GPHIKClassifier", "optimization_method", "downhillsimplex");//downhillsimplex greedy
-
- std::string s_trainData = conf.gS( "main", "trainData", "toyExampleSmallScaleTrain.data" );
-
- //------------- read the training data --------------
-
- NICE::Matrix dataTrain;
- NICE::Vector yBinTrain;
- NICE::Vector yMultiTrain;
-
- readData ( s_trainData, dataTrain, yBinTrain, yMultiTrain );
- //----------------- convert data to sparse data structures ---------
- std::vector< const NICE::SparseVector *> examplesTrain;
- examplesTrain.resize( dataTrain.rows()-1 );
-
- std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
- for (int i = 0; i < (int)dataTrain.rows()-1; i++, exTrainIt++)
- {
- *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
- }
-
- // TRAIN INITIAL CLASSIFIER FROM SCRATCH
- NICE::GPHIKClassifier * classifier;
- classifier = new NICE::GPHIKClassifier ( &conf );
- //use all but the first example for training and add the first one lateron
- NICE::Vector yMultiRelevantTrain ( yMultiTrain.getRangeRef( 0, yMultiTrain.size()-2 ) );
-
- classifier->train ( examplesTrain , yMultiRelevantTrain );
-
-
- // RUN INCREMENTAL LEARNING
-
- bool performOptimizationAfterIncrement ( true );
-
- NICE::SparseVector * exampleToAdd = new NICE::SparseVector ( dataTrain.getRow( (int)dataTrain.rows()-1 ) );
- classifier->addExample ( exampleToAdd, yMultiTrain[ (int)dataTrain.rows()-2 ], performOptimizationAfterIncrement );
-
- if ( verbose )
- std::cerr << "label of example to add: " << yMultiTrain[ (int)dataTrain.rows()-1 ] << std::endl;
-
- // TRAIN SECOND CLASSIFIER FROM SCRATCH USING THE SAME OVERALL AMOUNT OF EXAMPLES
- examplesTrain.push_back( exampleToAdd );
- NICE::GPHIKClassifier * classifierScratch = new NICE::GPHIKClassifier ( &conf );
- classifierScratch->train ( examplesTrain, yMultiTrain );
-
- if ( verbose )
- std::cerr << "trained both classifiers - now start evaluating them" << std::endl;
-
-
- // TEST that both classifiers produce equal store-files
- if ( writeClassifiersForVerification )
- {
- std::string s_destination_save_IL ( "myClassifierIL.txt" );
-
- std::filebuf fbOut;
- fbOut.open ( s_destination_save_IL.c_str(), ios::out );
- std::ostream os (&fbOut);
- //
- classifier->store( os );
- //
- fbOut.close();
-
- std::string s_destination_save_scratch ( "myClassifierScratch.txt" );
-
- std::filebuf fbOutScratch;
- fbOutScratch.open ( s_destination_save_scratch.c_str(), ios::out );
- std::ostream osScratch (&fbOutScratch);
- //
- classifierScratch->store( osScratch );
- //
- fbOutScratch.close();
- }
-
-
- // TEST both classifiers to produce equal results
-
- //------------- read the test data --------------
-
-
- NICE::Matrix dataTest;
- NICE::Vector yBinTest;
- NICE::Vector yMultiTest;
-
- std::string s_testData = conf.gS( "main", "testData", "toyExampleTest.data" );
-
- readData ( s_testData, dataTest, yBinTest, yMultiTest );
-
- // ------------------------------------------
- // ------------- PREPARATION --------------
- // ------------------------------------------
-
- // determine classes known during training/testing and corresponding mapping
- // thereby allow for non-continous class labels
- std::map< uint,uint > mapClNoToIdxTrain;
- std::map< uint,uint > mapClNoToIdxTest;
- prepareLabelMappings (mapClNoToIdxTrain, classifier, mapClNoToIdxTest, yMultiTest);
-
-
- NICE::Matrix confusionMatrix ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
- NICE::Matrix confusionMatrixScratch ( mapClNoToIdxTrain.size(), mapClNoToIdxTest.size(), 0.0);
-
- // ------------------------------------------
- // ------------- CLASSIFICATION --------------
- // ------------------------------------------
- evaluateClassifier ( confusionMatrix, classifier, dataTest, yMultiTest,
- mapClNoToIdxTrain,mapClNoToIdxTest );
-
- evaluateClassifier ( confusionMatrixScratch, classifierScratch, dataTest, yMultiTest,
- mapClNoToIdxTrain,mapClNoToIdxTest );
-
-
- // post-process confusion matrices
- confusionMatrix.normalizeColumnsL1();
- double arr ( confusionMatrix.trace()/confusionMatrix.cols() );
- confusionMatrixScratch.normalizeColumnsL1();
- double arrScratch ( confusionMatrixScratch.trace()/confusionMatrixScratch.cols() );
- if ( verbose )
- {
- std::cerr << "confusionMatrix: " << confusionMatrix << std::endl;
-
- std::cerr << "confusionMatrixScratch: " << confusionMatrixScratch << std::endl;
- }
-
-
- CPPUNIT_ASSERT_DOUBLES_EQUAL( arr, arrScratch, 1e-8);
-
- // don't waste memory
-
- delete classifier;
- delete classifierScratch;
-
- for (std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin(); exTrainIt != examplesTrain.end(); exTrainIt++)
- {
- delete *exTrainIt;
- }
-
- if (verboseStartEnd)
- std::cerr << "================== TestGPHIKOnlineLearnable::testOnlineLearningMultiClass done ===================== " << std::endl;
-
- }
- #endif
|