TestFastHIK.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. #ifdef NICE_USELIB_CPPUNIT
  2. #include <string>
  3. #include <exception>
  4. #include <core/algebra/ILSConjugateGradients.h>
  5. #include <core/algebra/GMStandard.h>
  6. #include <core/basics/Timer.h>
  7. #include <gp-hik-core/tools.h>
  8. #include <gp-hik-core/kernels/IntersectionKernelFunction.h>
  9. #include <gp-hik-core/kernels/GeneralizedIntersectionKernelFunction.h>
  10. #include <gp-hik-core/parameterizedFunctions/ParameterizedFunction.h>
  11. #include <gp-hik-core/parameterizedFunctions/PFAbsExp.h>
  12. #include <gp-hik-core/GMHIKernelRaw.h>
  13. //
  14. //
  15. #include "gp-hik-core/quantization/Quantization.h"
  16. #include "gp-hik-core/quantization/Quantization1DAequiDist0To1.h"
  17. #include "TestFastHIK.h"
  18. const bool b_debug = false;
  19. const bool verbose = true;
  20. const bool verboseStartEnd = true;
  21. // this test seems to be broken
  22. const bool solveLinWithoutRand = false;
  23. const uint n = 1500;//1500;//1500;//10;
  24. const uint d = 100;//200;//2;
  25. const uint numBins = 11;//1001;//1001;
  26. const uint solveLinMaxIterations = 1000;
  27. const double sparse_prob = 0.6;
  28. const bool smallTest = false;
  29. bool compareVVector(const NICE::VVector & A, const NICE::VVector & B, const double & tolerance = 10e-8)
  30. {
  31. bool result(true);
  32. // std::cerr << "A.size(): " << A.size() << " B.size(): " << B.size() << std::endl;
  33. NICE::VVector::const_iterator itA = A.begin();
  34. NICE::VVector::const_iterator itB = B.begin();
  35. while ( (itA != A.end()) && ( itB != B.end()) )
  36. {
  37. if (itA->size() != itB->size())
  38. {
  39. result = false;
  40. break;
  41. }
  42. for(uint i = 0; (i < itA->size()) && (i < itB->size()); i++)
  43. {
  44. if (fabs((*itA)[i] - (*itB)[i]) > tolerance)
  45. {
  46. result = false;
  47. break;
  48. }
  49. }
  50. if (result == false)
  51. break;
  52. itA++;
  53. itB++;
  54. }
  55. return result;
  56. }
  57. bool compareLUTs(const double* LUT1, const double* LUT2, const int & size, const double & tolerance = 10e-8)
  58. {
  59. bool result = true;
  60. for (int i = 0; i < size; i++)
  61. {
  62. if ( fabs(LUT1[i] - LUT2[i]) > tolerance)
  63. {
  64. result = false;
  65. std::cerr << "problem in : " << i << " / " << size << " LUT1: " << LUT1[i] << " LUT2: " << LUT2[i] << std::endl;
  66. break;
  67. }
  68. }
  69. return result;
  70. }
  71. using namespace NICE;
  72. using namespace std;
  73. CPPUNIT_TEST_SUITE_REGISTRATION( TestFastHIK );
  74. void TestFastHIK::setUp() {
  75. }
  76. void TestFastHIK::tearDown() {
  77. }
  78. void TestFastHIK::testKernelMultiplication()
  79. {
  80. if (verboseStartEnd)
  81. std::cerr << "================== TestFastHIK::testKernelMultiplication ===================== " << std::endl;
  82. vector< vector<double> > dataMatrix;
  83. generateRandomFeatures ( d, n, dataMatrix );
  84. int nrZeros(0);
  85. for ( uint i = 0 ; i < d; i++ )
  86. {
  87. for ( uint k = 0; k < n; k++ )
  88. if ( drand48() < sparse_prob )
  89. {
  90. dataMatrix[i][k] = 0.0;
  91. nrZeros++;
  92. }
  93. }
  94. if ( b_debug ) {
  95. cerr << "data matrix: " << endl;
  96. printMatrix ( dataMatrix );
  97. cerr << endl;
  98. }
  99. double noise = 1.0;
  100. NICE::Timer t;
  101. t.start();
  102. FastMinKernel fmk ( dataMatrix, noise );
  103. t.stop();
  104. if (verbose)
  105. std::cerr << "Time for FastMinKernel setup: " << t.getLast() << endl;
  106. if ( (n*d)>0)
  107. {
  108. CPPUNIT_ASSERT_DOUBLES_EQUAL(fmk.getSparsityRatio(), (double)nrZeros/(double)(n*d), 1e-8);
  109. if (verbose)
  110. std::cerr << "fmk.getSparsityRatio(): " << fmk.getSparsityRatio() << " (double)nrZeros/(double)(n*d): " << (double)nrZeros/(double)(n*d) << std::endl;
  111. }
  112. GMHIKernel gmk ( &fmk );
  113. if (verbose)
  114. gmk.setVerbose(true); //we want to see the size of size(A)+size(B) for non-sparse vs sparse solution
  115. else
  116. gmk.setVerbose(false); //we don't want to see the size of size(A)+size(B) for non-sparse vs sparse solution
  117. Vector y ( n );
  118. for ( uint i = 0; i < y.size(); i++ )
  119. y[i] = sin(i);
  120. // Test the GMHIKernel interface
  121. Vector alpha;
  122. t.start();
  123. gmk.multiply ( alpha, y );
  124. t.stop();
  125. if (verbose)
  126. std::cerr << "Time for kernel multiplication with GMHIKernel: " << t.getLast() << std::endl;
  127. // convert data structures to test the GMHIKernelRaw interface
  128. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  129. transposeVectorOfVectors(dataMatrix_transposed);
  130. std::vector< const NICE::SparseVector * > dataMatrix_sparse;
  131. for ( std::vector< std::vector<double> >::const_iterator i = dataMatrix_transposed.begin(); i != dataMatrix_transposed.end(); i++ )
  132. {
  133. Vector w ( *i );
  134. SparseVector *v = new SparseVector ( w );
  135. dataMatrix_sparse.push_back(v);
  136. }
  137. t.start();
  138. GMHIKernelRaw gmk_raw ( dataMatrix_sparse, noise );
  139. t.stop();
  140. if (verbose)
  141. std::cerr << "Time for GMHIKernelRaw setup: " << t.getLast() << std::endl;
  142. Vector alpha_raw;
  143. t.start();
  144. gmk_raw.multiply ( alpha_raw, y );
  145. t.stop();
  146. if (verbose)
  147. std::cerr << "Time for kernel multiplication with GMHIKernelRaw: " << t.getLast() << std::endl;
  148. // compute the kernel matrix multiplication exactly
  149. NICE::IntersectionKernelFunction<double> hikSlow;
  150. // tic
  151. time_t slow_start = clock();
  152. NICE::Matrix K (hikSlow.computeKernelMatrix(dataMatrix_transposed, noise));
  153. //toc
  154. float time_slowComputation = (float) (clock() - slow_start);
  155. if (verbose)
  156. std::cerr << "Time for computing the kernel matrix without using sparsity: " << time_slowComputation/CLOCKS_PER_SEC << " s" << std::endl;
  157. // tic
  158. time_t slow_sparse_start = clock();
  159. NICE::Matrix KSparseCalculated (hikSlow.computeKernelMatrix(fmk.featureMatrix(), noise));
  160. //toc
  161. float time_slowComputation_usingSparsity = (float) (clock() - slow_sparse_start);
  162. if (verbose)
  163. std::cerr << "Time for computing the kernel matrix using sparsity: " << time_slowComputation_usingSparsity/CLOCKS_PER_SEC << " s" << std::endl;
  164. // check the trace calculation
  165. //CPPUNIT_ASSERT_DOUBLES_EQUAL( K.trace(), fmk.featureMatrix().hikTrace() + noise*n, 1e-12 );
  166. CPPUNIT_ASSERT_DOUBLES_EQUAL( K.trace(), fmk.featureMatrix().hikTrace() + noise*n, 1e-8 );
  167. // let us compute the kernel multiplication with the slow version
  168. Vector alpha_slow = K*y;
  169. if (b_debug)
  170. std::cerr << "Sparse multiplication [alpha, alpha_slow, alpha_raw]: " << std::endl << alpha << std::endl << alpha_slow << std::endl << alpha_raw << std::endl << std::endl;
  171. CPPUNIT_ASSERT_DOUBLES_EQUAL((alpha-alpha_slow).normL1(), 0.0, 1e-8);
  172. CPPUNIT_ASSERT_DOUBLES_EQUAL((alpha_raw-alpha_slow).normL1(), 0.0, 1e-8);
  173. Vector gmk_diag;
  174. Vector gmk_raw_diag;
  175. gmk.getDiagonalElements(gmk_diag);
  176. gmk_raw.getDiagonalElements(gmk_raw_diag);
  177. CPPUNIT_ASSERT_DOUBLES_EQUAL((gmk_diag - gmk_raw_diag).normL1(), 0.0, 1e-8);
  178. // test the case, where we first transform and then use the multiply stuff
  179. NICE::GeneralizedIntersectionKernelFunction<double> ghikSlow ( 1.2 );
  180. NICE::Matrix gK ( ghikSlow.computeKernelMatrix(dataMatrix_transposed, noise) );
  181. ParameterizedFunction *pf = new PFAbsExp( 1.2 );
  182. fmk.applyFunctionToFeatureMatrix( pf );
  183. // pf->applyFunctionToFeatureMatrix ( fmk.featureMatrix() );
  184. Vector galpha;
  185. gmk.multiply ( galpha, y );
  186. Vector galpha_slow = gK * y;
  187. CPPUNIT_ASSERT_DOUBLES_EQUAL((galpha-galpha_slow).normL1(), 0.0, 1e-8);
  188. if (verboseStartEnd)
  189. std::cerr << "================== TestFastHIK::testKernelMultiplication done ===================== " << std::endl;
  190. delete pf;
  191. }
  192. void TestFastHIK::testKernelMultiplicationFast()
  193. {
  194. if (verboseStartEnd)
  195. std::cerr << "================== TestFastHIK::testKernelMultiplicationFast ===================== " << std::endl;
  196. NICE::Quantization * q_gen;
  197. q_gen = new Quantization1DAequiDist0To1 ( numBins );
  198. NICE::Quantization * q;
  199. q = new Quantization1DAequiDist0To1 ( 2*numBins -1 );
  200. // data is generated, such that there is no approximation error
  201. vector< vector<double> > dataMatrix;
  202. for ( uint i = 0; i < d ; i++ )
  203. {
  204. vector<double> v;
  205. v.resize(n);
  206. for ( uint k = 0; k < n; k++ ) {
  207. if ( drand48() < sparse_prob ) {
  208. v[k] = 0;
  209. } else {
  210. v[k] = q_gen->getPrototype( (rand() % numBins) );
  211. }
  212. }
  213. dataMatrix.push_back(v);
  214. }
  215. double noise = 1.0;
  216. FastMinKernel fmk ( dataMatrix, noise );
  217. GMHIKernel gmk ( &fmk );
  218. if (verbose)
  219. gmk.setVerbose(true); //we want to see the size of size(A)+size(B) for non-sparse vs sparse solution
  220. else
  221. gmk.setVerbose(false); //we don't want to see the size of size(A)+size(B) for non-sparse vs sparse solution
  222. Vector y ( n );
  223. for ( uint i = 0; i < y.size(); i++ )
  224. y[i] = sin(i);
  225. ParameterizedFunction *pf = new PFAbsExp ( 1.0 );
  226. GMHIKernel gmkFast ( &fmk, pf, q );
  227. // pf.applyFunctionToFeatureMatrix ( fmk.featureMatrix() );
  228. Vector alpha;
  229. gmk.multiply ( alpha, y );
  230. Vector alphaFast;
  231. gmkFast.multiply ( alphaFast, y );
  232. NICE::IntersectionKernelFunction<double> hikSlow;
  233. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  234. transposeVectorOfVectors(dataMatrix_transposed);
  235. NICE::Matrix K (hikSlow.computeKernelMatrix(dataMatrix_transposed, noise));
  236. // check the trace calculation
  237. //CPPUNIT_ASSERT_DOUBLES_EQUAL( K.trace(), fmk.featureMatrix().hikTrace() + noise*n, 1e-12 );
  238. CPPUNIT_ASSERT_DOUBLES_EQUAL( K.trace(), fmk.featureMatrix().hikTrace() + noise*n, 1e-8 );
  239. // let us compute the kernel multiplication with the slow version
  240. Vector alpha_slow = K*y;
  241. if ( b_debug )
  242. std::cerr << "Sparse multiplication [alpha, alphaFast, alpha_slow]: " << std::endl << alpha << std::endl << alphaFast << std::endl << alpha_slow << std::endl << std::endl;
  243. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, (alphaFast-alpha_slow).normL1(), 1e-8);
  244. // test the case, where we first transform and then use the multiply stuff
  245. NICE::GeneralizedIntersectionKernelFunction<double> ghikSlow ( 1.2 );
  246. NICE::Matrix gK ( ghikSlow.computeKernelMatrix(dataMatrix_transposed, noise) );
  247. pf->parameters()[0] = 1.2;
  248. fmk.applyFunctionToFeatureMatrix( pf );
  249. Vector galphaFast;
  250. gmkFast.multiply ( galphaFast, y );
  251. Vector galpha;
  252. gmk.multiply ( galpha, y );
  253. Vector galpha_slow = gK * y;
  254. if ( b_debug )
  255. std::cerr << "Sparse multiplication [galpha, galphaFast, galpha_slow]: " << std::endl << galpha << std::endl << galphaFast << std::endl << galpha_slow << std::endl << std::endl;
  256. // clean-up
  257. delete q_gen;
  258. delete q;
  259. // final assertion
  260. CPPUNIT_ASSERT_DOUBLES_EQUAL((galphaFast-galpha_slow).normL1(), 0.0, 1e-8);
  261. if (verboseStartEnd)
  262. std::cerr << "================== TestFastHIK::testKernelMultiplicationFast done ===================== " << std::endl;
  263. delete pf;
  264. }
  265. void TestFastHIK::testKernelSum()
  266. {
  267. if (verboseStartEnd)
  268. std::cerr << "================== TestFastHIK::testKernelSum ===================== " << std::endl;
  269. vector< vector<double> > dataMatrix;
  270. generateRandomFeatures ( d, n, dataMatrix );
  271. int nrZeros(0);
  272. for ( uint i = 0 ; i < d; i++ )
  273. {
  274. for ( uint k = 0; k < n; k++ )
  275. if ( drand48() < sparse_prob )
  276. {
  277. dataMatrix[i][k] = 0.0;
  278. nrZeros++;
  279. }
  280. }
  281. if ( b_debug ) {
  282. cerr << "data matrix: " << endl;
  283. printMatrix ( dataMatrix );
  284. cerr << endl;
  285. }
  286. double noise = 1.0;
  287. FastMinKernel fmk ( dataMatrix, noise );
  288. Vector alpha = Vector::UniformRandom( n, 0.0, 1.0, 0 );
  289. NICE::VVector ASparse;
  290. NICE::VVector BSparse;
  291. fmk.hik_prepare_alpha_multiplications ( alpha, ASparse, BSparse );
  292. Vector xstar (d);
  293. for ( uint i = 0 ; i < d ; i++ )
  294. if ( drand48() < sparse_prob ) {
  295. xstar[i] = 0.0;
  296. } else {
  297. xstar[i] = rand();
  298. }
  299. SparseVector xstarSparse ( xstar );
  300. double betaSparse;
  301. fmk.hik_kernel_sum ( ASparse, BSparse, xstarSparse, betaSparse );
  302. if (verbose)
  303. std::cerr << "kernelSumSparse done, now do the thing without exploiting sparsity" << std::endl;
  304. // checking the result
  305. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  306. transposeVectorOfVectors(dataMatrix_transposed);
  307. NICE::IntersectionKernelFunction<double> hikSlow;
  308. std::vector<double> xstar_stl;
  309. xstar_stl.resize(d);
  310. for ( uint i = 0 ; i < d; i++ )
  311. xstar_stl[i] = xstar[i];
  312. std::vector<double> kstar_stl = hikSlow.computeKernelVector ( dataMatrix_transposed, xstar_stl );
  313. double beta_slow = 0.0;
  314. for ( uint i = 0 ; i < n; i++ )
  315. beta_slow += kstar_stl[i] * alpha[i];
  316. if (verbose)
  317. std::cerr << "difference of beta_slow and betaSparse: " << fabs(beta_slow - betaSparse) << std::endl;
  318. CPPUNIT_ASSERT_DOUBLES_EQUAL(beta_slow, betaSparse, 1e-8);
  319. if (verboseStartEnd)
  320. std::cerr << "================== TestFastHIK::testKernelSum done ===================== " << std::endl;
  321. }
  322. void TestFastHIK::testKernelSumFast()
  323. {
  324. if (verboseStartEnd)
  325. std::cerr << "================== TestFastHIK::testKernelSumFast ===================== " << std::endl;
  326. NICE::Quantization * q;
  327. q = new Quantization1DAequiDist0To1 ( numBins );
  328. // data is generated, such that there is no approximation error
  329. vector< vector<double> > dataMatrix;
  330. for ( uint i = 0; i < d ; i++ )
  331. {
  332. vector<double> v;
  333. v.resize(n);
  334. for ( uint k = 0; k < n; k++ ) {
  335. if ( drand48() < sparse_prob ) {
  336. v[k] = 0;
  337. } else {
  338. v[k] = q->getPrototype( (rand() % numBins) );
  339. }
  340. }
  341. dataMatrix.push_back(v);
  342. }
  343. if ( b_debug ) {
  344. cerr << "data matrix: " << endl;
  345. printMatrix ( dataMatrix );
  346. cerr << endl;
  347. }
  348. double noise = 1.0;
  349. FastMinKernel fmk ( dataMatrix, noise );
  350. Vector alpha = Vector::UniformRandom( n, 0.0, 1.0, 0 );
  351. if ( b_debug )
  352. std::cerr << "alpha = " << alpha << endl;
  353. // generate xstar
  354. Vector xstar (d);
  355. for ( uint i = 0 ; i < d ; i++ )
  356. if ( drand48() < sparse_prob ) {
  357. xstar[i] = 0;
  358. } else {
  359. xstar[i] = q->getPrototype( (rand() % numBins) );
  360. }
  361. // convert to STL vector
  362. vector<double> xstar_stl;
  363. xstar_stl.resize(d);
  364. for ( uint i = 0 ; i < d; i++ )
  365. xstar_stl[i] = xstar[i];
  366. if ( b_debug )
  367. cerr << "xstar = " << xstar << endl;
  368. for ( double gamma = 1.0 ; gamma < 2.0; gamma += 0.5 )
  369. {
  370. if (verbose)
  371. std::cerr << "testing hik_kernel_sum_fast with ghik parameter: " << gamma << endl;
  372. PFAbsExp pf ( gamma );
  373. // pf.applyFunctionToFeatureMatrix ( fmk.featureMatrix() );
  374. fmk.applyFunctionToFeatureMatrix( &pf );
  375. NICE::VVector A;
  376. NICE::VVector B;
  377. if (verbose)
  378. std::cerr << "fmk.hik_prepare_alpha_multiplications ( alpha, A, B ) " << std::endl;
  379. fmk.hik_prepare_alpha_multiplications ( alpha, A, B );
  380. if (b_debug)
  381. //std::cerr << "double *Tlookup = fmk.hik_prepare_alpha_multiplications_fast( A, B, q )" << std::endl;
  382. std::cerr << "double *Tlookup = fmk.hik_prepare_alpha_multiplications_fast_alltogether( alpha, q, &pf )" << std::endl;
  383. double *TlookupOld = fmk.hik_prepare_alpha_multiplications_fast( A, B, q, &pf );
  384. double *TlookupNew = fmk.hikPrepareLookupTable( alpha, q, &pf );
  385. int maxAcces(numBins*d);
  386. if (b_debug)
  387. {
  388. std::cerr << "TlookupOld: " << std::endl;
  389. for (int i = 0; i < maxAcces; i++)
  390. {
  391. std::cerr << TlookupOld[i] << " ";
  392. if ( (i%numBins) == (numBins-1))
  393. std::cerr << std::endl;
  394. }
  395. std::cerr << "TlookupNew: " << std::endl;
  396. for (int i = 0; i < maxAcces; i++)
  397. {
  398. std::cerr << TlookupNew[i] << " ";
  399. if ( (i%numBins) == (numBins-1))
  400. std::cerr << std::endl;
  401. }
  402. }
  403. if (verbose)
  404. std::cerr << "fmk.hik_kernel_sum_fast ( Tlookup, q, xstar, beta_fast )" << std::endl;
  405. double beta_fast;
  406. fmk.hik_kernel_sum_fast ( TlookupNew, q, xstar, beta_fast );
  407. NICE::SparseVector xstar_sparse(xstar);
  408. double beta_fast_sparse;
  409. fmk.hik_kernel_sum_fast ( TlookupNew, q, xstar_sparse, beta_fast_sparse );
  410. double betaSparse;
  411. fmk.hik_kernel_sum ( A, B, xstar_sparse, betaSparse, &pf );
  412. // checking the result
  413. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  414. transposeVectorOfVectors(dataMatrix_transposed);
  415. NICE::GeneralizedIntersectionKernelFunction<double> hikSlow (gamma);
  416. vector<double> kstar_stl = hikSlow.computeKernelVector ( dataMatrix_transposed, xstar_stl );
  417. double beta_slow = 0.0;
  418. for ( uint i = 0 ; i < n; i++ )
  419. beta_slow += kstar_stl[i] * alpha[i];
  420. if (b_debug)
  421. std::cerr << "beta_slow: " << beta_slow << std::endl << "beta_fast: " << beta_fast << std::endl << "beta_fast_sparse: " << beta_fast_sparse << std::endl << "betaSparse: " << betaSparse<< std::endl;
  422. // clean-up
  423. delete [] TlookupNew;
  424. delete [] TlookupOld;
  425. // final assertion
  426. CPPUNIT_ASSERT_DOUBLES_EQUAL(beta_slow, beta_fast_sparse, 1e-8);
  427. } // for-loop
  428. // clean-up
  429. delete q;
  430. if (verboseStartEnd)
  431. std::cerr << "================== TestFastHIK::testKernelSumFast done ===================== " << std::endl;
  432. }
  433. void TestFastHIK::testLUTUpdate()
  434. {
  435. if (verboseStartEnd)
  436. std::cerr << "================== TestFastHIK::testLUTUpdate ===================== " << std::endl;
  437. NICE::Quantization * q;
  438. q = new Quantization1DAequiDist0To1 ( numBins );
  439. // data is generated, such that there is no approximation error
  440. std::vector< std::vector<double> > dataMatrix;
  441. for ( uint i = 0; i < d ; i++ )
  442. {
  443. std::vector<double> v;
  444. v.resize(n);
  445. for ( uint k = 0; k < n; k++ ) {
  446. if ( drand48() < sparse_prob ) {
  447. v[k] = 0;
  448. } else {
  449. v[k] = q->getPrototype( (rand() % numBins) );
  450. }
  451. }
  452. dataMatrix.push_back(v);
  453. }
  454. if ( b_debug ) {
  455. cerr << "data matrix: " << endl;
  456. printMatrix ( dataMatrix );
  457. cerr << endl;
  458. }
  459. double noise = 1.0;
  460. NICE::FastMinKernel fmk ( dataMatrix, noise );
  461. NICE::ParameterizedFunction *pf = new PFAbsExp ( 1.0 );
  462. NICE::Vector alpha ( n );
  463. for ( uint i = 0; i < alpha.size(); i++ )
  464. alpha[i] = sin(i);
  465. if (verbose)
  466. std::cerr << "prepare LUT" << std::endl;
  467. double * T = fmk.hikPrepareLookupTable(alpha, q, pf);
  468. if (verbose)
  469. std::cerr << "preparation done -- printing T" << std::endl;
  470. int maxAcces(numBins*d);
  471. if (verbose)
  472. {
  473. for (int i = 0; i < maxAcces; i++)
  474. {
  475. std::cerr << T[i] << " ";
  476. if ( (i%numBins) == (numBins-1))
  477. std::cerr << std::endl;
  478. }
  479. }
  480. //lets change index 2
  481. int idx(2);
  482. double valAlphaOld(alpha[idx]);
  483. double valAlphaNew(1.2); //this value is definitely different from the previous one
  484. Vector alphaNew(alpha);
  485. alphaNew[idx] = valAlphaNew;
  486. double * TNew = fmk.hikPrepareLookupTable(alphaNew, q, pf);
  487. if (verbose)
  488. std::cerr << "calculated the new LUT, no print it: " << std::endl;
  489. if (verbose)
  490. {
  491. for (int i = 0; i < maxAcces; i++)
  492. {
  493. std::cerr << TNew[i] << " ";
  494. if ( (i%numBins) == (numBins-1))
  495. std::cerr << std::endl;
  496. }
  497. }
  498. if (verbose)
  499. std::cerr << "change the old LUT by a new value for alpha_i" << std::endl;
  500. fmk.hikUpdateLookupTable(T, valAlphaNew, valAlphaOld, idx, q, pf );
  501. if (verbose)
  502. std::cerr << "update is done, now print the updated version: " << std::endl;
  503. if (verbose)
  504. {
  505. for (int i = 0; i < maxAcces; i++)
  506. {
  507. std::cerr << T[i] << " ";
  508. if ( (i%numBins) == (numBins-1))
  509. std::cerr << std::endl;
  510. }
  511. }
  512. bool equal = compareLUTs(T, TNew, q->getNumberOfBins()*d, 10e-8);
  513. if (verbose)
  514. {
  515. if (equal)
  516. std::cerr << "LUTs are equal :) " << std::endl;
  517. else
  518. {
  519. std::cerr << "T are not equal :( " << std::endl;
  520. for (uint i = 0; i < q->getNumberOfBins()*d; i++)
  521. {
  522. if ( (i % q->getNumberOfBins()) == 0)
  523. std::cerr << std::endl;
  524. std::cerr << T[i] << " ";
  525. }
  526. std::cerr << "TNew: "<< std::endl;
  527. for (uint i = 0; i < q->getNumberOfBins()*d; i++)
  528. {
  529. if ( (i % q->getNumberOfBins()) == 0)
  530. std::cerr << std::endl;
  531. std::cerr << TNew[i] << " ";
  532. }
  533. }
  534. }
  535. // clean-up
  536. delete q;
  537. delete pf;
  538. delete [] T;
  539. delete [] TNew;
  540. // final assertion
  541. CPPUNIT_ASSERT(equal == true);
  542. if (verboseStartEnd)
  543. std::cerr << "================== TestFastHIK::testLUTUpdate done ===================== " << std::endl;
  544. }
  545. void TestFastHIK::testLinSolve()
  546. {
  547. if (verboseStartEnd)
  548. std::cerr << "================== TestFastHIK::testLinSolve ===================== " << std::endl;
  549. NICE::Quantization * q;
  550. q = new Quantization1DAequiDist0To1 ( numBins );
  551. // data is generated, such that there is no approximation error
  552. std::vector< std::vector<double> > dataMatrix;
  553. for ( uint i = 0; i < d ; i++ )
  554. {
  555. std::vector<double> v;
  556. v.resize(n);
  557. for ( uint k = 0; k < n; k++ ) {
  558. if ( drand48() < sparse_prob ) {
  559. v[k] = 0;
  560. } else {
  561. v[k] = q->getPrototype( (rand() % numBins) );
  562. }
  563. }
  564. dataMatrix.push_back(v);
  565. }
  566. if ( b_debug ) {
  567. std::cerr << "data matrix: " << std::endl;
  568. printMatrix ( dataMatrix );
  569. std::cerr << std::endl;
  570. }
  571. // Let's test the FastMinKernel solveLin core methods.
  572. // Looks like their odd convergence makes it only possible
  573. // to test this for smaller cases.
  574. double noise = 1.0;
  575. NICE::FastMinKernel fmk ( dataMatrix, noise );
  576. NICE::ParameterizedFunction *pf = new NICE::PFAbsExp ( 1.0 );
  577. fmk.applyFunctionToFeatureMatrix( pf );
  578. NICE::Vector y ( n );
  579. for ( uint i = 0; i < y.size(); i++ )
  580. y[i] = sin(i);
  581. NICE::Vector alphaRandomized;
  582. if ( verbose )
  583. std::cerr << "solveLin with randomization" << std::endl;
  584. // tic
  585. NICE::Timer t;
  586. t.start();
  587. //let's try to do 10.000 iterations and sample in each iteration 30 examples randomly
  588. fmk.solveLin(y, alphaRandomized, q, pf, true, solveLinMaxIterations, 30 /* random subset */);
  589. //toc
  590. t.stop();
  591. float time_randomizedSolving = t.getLast();
  592. if ( verbose )
  593. std::cerr << "Time for solving with random subsets: " << time_randomizedSolving << " s" << std::endl;
  594. // test the case, where we first transform and then use the multiply stuff
  595. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  596. transposeVectorOfVectors(dataMatrix_transposed);
  597. NICE::GeneralizedIntersectionKernelFunction<double> ghikSlow ( 1.0 );
  598. NICE::Matrix gK ( ghikSlow.computeKernelMatrix(dataMatrix_transposed, noise) );
  599. NICE::Vector K_alphaRandomized;
  600. K_alphaRandomized.multiply(gK, alphaRandomized);
  601. NICE::Vector alpha;
  602. if (solveLinWithoutRand)
  603. {
  604. if ( verbose )
  605. std::cerr << "solveLin without randomization" << std::endl;
  606. fmk.solveLin(y, alpha, q, pf, false, 30 /* max iterations */);
  607. Vector K_alpha;
  608. K_alpha.multiply(gK, alpha);
  609. if ( verbose )
  610. {
  611. std::cerr << "now assert that K_alpha == y" << std::endl;
  612. std::cerr << "(K_alpha-y).normL1(): " << (K_alpha - y).normL1() << std::endl;
  613. }
  614. }
  615. // std::cerr << "alpha: " << alpha << std::endl;
  616. // std::cerr << "K_times_alpha: " << K_alpha << std::endl;
  617. // std::cerr << "y: " << y << std::endl;
  618. //
  619. // Vector test_alpha;
  620. // ILSConjugateGradients cgm;
  621. // cgm.solveLin( GMStandard(gK),y,test_alpha);
  622. //
  623. // K_alpha.multiply( gK, test_alpha);
  624. //
  625. // std::cerr << "test_alpha (CGM): " << test_alpha << std::endl;
  626. // std::cerr << "K_times_alpha (CGM): " << K_alpha << std::endl;
  627. if ( verbose )
  628. {
  629. std::cerr << "now assert that K_alphaRandomized == y" << std::endl;
  630. // TODO: normL1 sometimes delivered a negative number here
  631. std::cerr << "(K_alphaRandomized-y).normL1(): " << (K_alphaRandomized - y).normL1() << std::endl;
  632. }
  633. // clean-up
  634. delete q;
  635. delete pf;
  636. // final assertion
  637. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, (K_alphaRandomized-y).normL1(), 1e-6);
  638. if (verboseStartEnd)
  639. std::cerr << "================== TestFastHIK::testLinSolve done ===================== " << std::endl;
  640. }
  641. void TestFastHIK::testKernelVector()
  642. {
  643. if (verboseStartEnd)
  644. std::cerr << "================== TestFastHIK::testKernelVector ===================== " << std::endl;
  645. std::vector< std::vector<double> > dataMatrix;
  646. std::vector<double> dim1; dim1.push_back(0.2);dim1.push_back(0.1);dim1.push_back(0.0);dim1.push_back(0.0);dim1.push_back(0.4); dataMatrix.push_back(dim1);
  647. std::vector<double> dim2; dim2.push_back(0.3);dim2.push_back(0.6);dim2.push_back(1.0);dim2.push_back(0.4);dim2.push_back(0.3); dataMatrix.push_back(dim2);
  648. std::vector<double> dim3; dim3.push_back(0.5);dim3.push_back(0.3);dim3.push_back(0.0);dim3.push_back(0.6);dim3.push_back(0.3); dataMatrix.push_back(dim3);
  649. if ( b_debug ) {
  650. std::cerr << "data matrix: " << std::endl;
  651. printMatrix ( dataMatrix );
  652. std::cerr << endl;
  653. }
  654. double noise = 1.0;
  655. FastMinKernel fmk ( dataMatrix, noise, b_debug );
  656. std::vector<double> xStar; xStar.push_back(0.2);xStar.push_back(0.7);xStar.push_back(0.1);
  657. NICE::Vector xStarVec (xStar);
  658. std::vector<double> x2; x2.push_back(0.7);x2.push_back(0.3);xStar.push_back(0.0);
  659. NICE::Vector x2Vec (x2);
  660. NICE::SparseVector xStarsparse( xStarVec );
  661. NICE::SparseVector x2sparse( x2Vec );
  662. if ( b_debug )
  663. {
  664. fmk.store ( std::cerr );
  665. xStarsparse.store ( std::cerr );
  666. }
  667. NICE::Vector k1;
  668. fmk.hikComputeKernelVector( xStarsparse, k1 );
  669. NICE::Vector k2;
  670. fmk.hikComputeKernelVector( x2sparse, k2 );
  671. NICE::Vector k1GT(5); k1GT[0] = 0.6; k1GT[1] = 0.8; k1GT[2] = 0.7; k1GT[3] = 0.5; k1GT[4] = 0.6;
  672. NICE::Vector k2GT(5); k2GT[0] = 0.5; k2GT[1] = 0.4; k2GT[2] = 0.3; k2GT[3] = 0.3; k2GT[4] = 0.7;
  673. if (b_debug)
  674. {
  675. std::cerr << "k1: " << k1 << std::endl;
  676. std::cerr << "GT: " << k1GT << std::endl;
  677. std::cerr << "k2: " << k2 << std::endl;
  678. std::cerr << "GT: " << k2GT << std::endl;
  679. }
  680. for (int i = 0; i < 5; i++)
  681. {
  682. CPPUNIT_ASSERT_DOUBLES_EQUAL(k1[i]-k1GT[i], 0.0, 1e-6);
  683. CPPUNIT_ASSERT_DOUBLES_EQUAL(k2[i]-k2GT[i], 0.0, 1e-6);
  684. }
  685. if (verboseStartEnd)
  686. std::cerr << "================== TestFastHIK::testKernelVector done ===================== " << std::endl;
  687. }
  688. #endif