TestFastHIK.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 = 500;//1500;//1500;//10;
  24. const uint d = 50;//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. // test the case, where we first transform and then use the multiply stuff
  174. NICE::GeneralizedIntersectionKernelFunction<double> ghikSlow ( 1.2 );
  175. NICE::Matrix gK ( ghikSlow.computeKernelMatrix(dataMatrix_transposed, noise) );
  176. ParameterizedFunction *pf = new PFAbsExp( 1.2 );
  177. fmk.applyFunctionToFeatureMatrix( pf );
  178. // pf->applyFunctionToFeatureMatrix ( fmk.featureMatrix() );
  179. Vector galpha;
  180. gmk.multiply ( galpha, y );
  181. Vector galpha_slow = gK * y;
  182. CPPUNIT_ASSERT_DOUBLES_EQUAL((galpha-galpha_slow).normL1(), 0.0, 1e-8);
  183. if (verboseStartEnd)
  184. std::cerr << "================== TestFastHIK::testKernelMultiplication done ===================== " << std::endl;
  185. delete pf;
  186. }
  187. void TestFastHIK::testKernelMultiplicationFast()
  188. {
  189. if (verboseStartEnd)
  190. std::cerr << "================== TestFastHIK::testKernelMultiplicationFast ===================== " << std::endl;
  191. NICE::Quantization * q_gen;
  192. q_gen = new Quantization1DAequiDist0To1 ( numBins );
  193. NICE::Quantization * q;
  194. q = new Quantization1DAequiDist0To1 ( 2*numBins -1 );
  195. // data is generated, such that there is no approximation error
  196. vector< vector<double> > dataMatrix;
  197. for ( uint i = 0; i < d ; i++ )
  198. {
  199. vector<double> v;
  200. v.resize(n);
  201. for ( uint k = 0; k < n; k++ ) {
  202. if ( drand48() < sparse_prob ) {
  203. v[k] = 0;
  204. } else {
  205. v[k] = q_gen->getPrototype( (rand() % numBins) );
  206. }
  207. }
  208. dataMatrix.push_back(v);
  209. }
  210. double noise = 1.0;
  211. FastMinKernel fmk ( dataMatrix, noise );
  212. GMHIKernel gmk ( &fmk );
  213. if (verbose)
  214. gmk.setVerbose(true); //we want to see the size of size(A)+size(B) for non-sparse vs sparse solution
  215. else
  216. gmk.setVerbose(false); //we don't want to see the size of size(A)+size(B) for non-sparse vs sparse solution
  217. Vector y ( n );
  218. for ( uint i = 0; i < y.size(); i++ )
  219. y[i] = sin(i);
  220. ParameterizedFunction *pf = new PFAbsExp ( 1.0 );
  221. GMHIKernel gmkFast ( &fmk, pf, q );
  222. // pf.applyFunctionToFeatureMatrix ( fmk.featureMatrix() );
  223. Vector alpha;
  224. gmk.multiply ( alpha, y );
  225. Vector alphaFast;
  226. gmkFast.multiply ( alphaFast, y );
  227. NICE::IntersectionKernelFunction<double> hikSlow;
  228. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  229. transposeVectorOfVectors(dataMatrix_transposed);
  230. NICE::Matrix K (hikSlow.computeKernelMatrix(dataMatrix_transposed, noise));
  231. // check the trace calculation
  232. //CPPUNIT_ASSERT_DOUBLES_EQUAL( K.trace(), fmk.featureMatrix().hikTrace() + noise*n, 1e-12 );
  233. CPPUNIT_ASSERT_DOUBLES_EQUAL( K.trace(), fmk.featureMatrix().hikTrace() + noise*n, 1e-8 );
  234. // let us compute the kernel multiplication with the slow version
  235. Vector alpha_slow = K*y;
  236. if ( b_debug )
  237. std::cerr << "Sparse multiplication [alpha, alphaFast, alpha_slow]: " << std::endl << alpha << std::endl << alphaFast << std::endl << alpha_slow << std::endl << std::endl;
  238. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, (alphaFast-alpha_slow).normL1(), 1e-8);
  239. // test the case, where we first transform and then use the multiply stuff
  240. NICE::GeneralizedIntersectionKernelFunction<double> ghikSlow ( 1.2 );
  241. NICE::Matrix gK ( ghikSlow.computeKernelMatrix(dataMatrix_transposed, noise) );
  242. pf->parameters()[0] = 1.2;
  243. fmk.applyFunctionToFeatureMatrix( pf );
  244. Vector galphaFast;
  245. gmkFast.multiply ( galphaFast, y );
  246. Vector galpha;
  247. gmk.multiply ( galpha, y );
  248. Vector galpha_slow = gK * y;
  249. if ( b_debug )
  250. std::cerr << "Sparse multiplication [galpha, galphaFast, galpha_slow]: " << std::endl << galpha << std::endl << galphaFast << std::endl << galpha_slow << std::endl << std::endl;
  251. // clean-up
  252. delete q_gen;
  253. delete q;
  254. // final assertion
  255. CPPUNIT_ASSERT_DOUBLES_EQUAL((galphaFast-galpha_slow).normL1(), 0.0, 1e-8);
  256. if (verboseStartEnd)
  257. std::cerr << "================== TestFastHIK::testKernelMultiplicationFast done ===================== " << std::endl;
  258. delete pf;
  259. }
  260. void TestFastHIK::testKernelSum()
  261. {
  262. if (verboseStartEnd)
  263. std::cerr << "================== TestFastHIK::testKernelSum ===================== " << std::endl;
  264. vector< vector<double> > dataMatrix;
  265. generateRandomFeatures ( d, n, dataMatrix );
  266. int nrZeros(0);
  267. for ( uint i = 0 ; i < d; i++ )
  268. {
  269. for ( uint k = 0; k < n; k++ )
  270. if ( drand48() < sparse_prob )
  271. {
  272. dataMatrix[i][k] = 0.0;
  273. nrZeros++;
  274. }
  275. }
  276. if ( b_debug ) {
  277. cerr << "data matrix: " << endl;
  278. printMatrix ( dataMatrix );
  279. cerr << endl;
  280. }
  281. double noise = 1.0;
  282. FastMinKernel fmk ( dataMatrix, noise );
  283. Vector alpha = Vector::UniformRandom( n, 0.0, 1.0, 0 );
  284. NICE::VVector ASparse;
  285. NICE::VVector BSparse;
  286. fmk.hik_prepare_alpha_multiplications ( alpha, ASparse, BSparse );
  287. Vector xstar (d);
  288. for ( uint i = 0 ; i < d ; i++ )
  289. if ( drand48() < sparse_prob ) {
  290. xstar[i] = 0.0;
  291. } else {
  292. xstar[i] = rand();
  293. }
  294. SparseVector xstarSparse ( xstar );
  295. double betaSparse;
  296. fmk.hik_kernel_sum ( ASparse, BSparse, xstarSparse, betaSparse );
  297. if (verbose)
  298. std::cerr << "kernelSumSparse done, now do the thing without exploiting sparsity" << std::endl;
  299. // checking the result
  300. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  301. transposeVectorOfVectors(dataMatrix_transposed);
  302. NICE::IntersectionKernelFunction<double> hikSlow;
  303. std::vector<double> xstar_stl;
  304. xstar_stl.resize(d);
  305. for ( uint i = 0 ; i < d; i++ )
  306. xstar_stl[i] = xstar[i];
  307. std::vector<double> kstar_stl = hikSlow.computeKernelVector ( dataMatrix_transposed, xstar_stl );
  308. double beta_slow = 0.0;
  309. for ( uint i = 0 ; i < n; i++ )
  310. beta_slow += kstar_stl[i] * alpha[i];
  311. if (verbose)
  312. std::cerr << "difference of beta_slow and betaSparse: " << fabs(beta_slow - betaSparse) << std::endl;
  313. CPPUNIT_ASSERT_DOUBLES_EQUAL(beta_slow, betaSparse, 1e-8);
  314. if (verboseStartEnd)
  315. std::cerr << "================== TestFastHIK::testKernelSum done ===================== " << std::endl;
  316. }
  317. void TestFastHIK::testKernelSumFast()
  318. {
  319. if (verboseStartEnd)
  320. std::cerr << "================== TestFastHIK::testKernelSumFast ===================== " << std::endl;
  321. NICE::Quantization * q;
  322. q = new Quantization1DAequiDist0To1 ( numBins );
  323. // data is generated, such that there is no approximation error
  324. vector< vector<double> > dataMatrix;
  325. for ( uint i = 0; i < d ; i++ )
  326. {
  327. vector<double> v;
  328. v.resize(n);
  329. for ( uint k = 0; k < n; k++ ) {
  330. if ( drand48() < sparse_prob ) {
  331. v[k] = 0;
  332. } else {
  333. v[k] = q->getPrototype( (rand() % numBins) );
  334. }
  335. }
  336. dataMatrix.push_back(v);
  337. }
  338. if ( b_debug ) {
  339. cerr << "data matrix: " << endl;
  340. printMatrix ( dataMatrix );
  341. cerr << endl;
  342. }
  343. double noise = 1.0;
  344. FastMinKernel fmk ( dataMatrix, noise );
  345. Vector alpha = Vector::UniformRandom( n, 0.0, 1.0, 0 );
  346. if ( b_debug )
  347. std::cerr << "alpha = " << alpha << endl;
  348. // generate xstar
  349. Vector xstar (d);
  350. for ( uint i = 0 ; i < d ; i++ )
  351. if ( drand48() < sparse_prob ) {
  352. xstar[i] = 0;
  353. } else {
  354. xstar[i] = q->getPrototype( (rand() % numBins) );
  355. }
  356. // convert to STL vector
  357. vector<double> xstar_stl;
  358. xstar_stl.resize(d);
  359. for ( uint i = 0 ; i < d; i++ )
  360. xstar_stl[i] = xstar[i];
  361. if ( b_debug )
  362. cerr << "xstar = " << xstar << endl;
  363. for ( double gamma = 1.0 ; gamma < 2.0; gamma += 0.5 )
  364. {
  365. if (verbose)
  366. std::cerr << "testing hik_kernel_sum_fast with ghik parameter: " << gamma << endl;
  367. PFAbsExp pf ( gamma );
  368. // pf.applyFunctionToFeatureMatrix ( fmk.featureMatrix() );
  369. fmk.applyFunctionToFeatureMatrix( &pf );
  370. NICE::VVector A;
  371. NICE::VVector B;
  372. if (verbose)
  373. std::cerr << "fmk.hik_prepare_alpha_multiplications ( alpha, A, B ) " << std::endl;
  374. fmk.hik_prepare_alpha_multiplications ( alpha, A, B );
  375. if (b_debug)
  376. //std::cerr << "double *Tlookup = fmk.hik_prepare_alpha_multiplications_fast( A, B, q )" << std::endl;
  377. std::cerr << "double *Tlookup = fmk.hik_prepare_alpha_multiplications_fast_alltogether( alpha, q, &pf )" << std::endl;
  378. double *TlookupOld = fmk.hik_prepare_alpha_multiplications_fast( A, B, q, &pf );
  379. double *TlookupNew = fmk.hikPrepareLookupTable( alpha, q, &pf );
  380. int maxAcces(numBins*d);
  381. if (b_debug)
  382. {
  383. std::cerr << "TlookupOld: " << std::endl;
  384. for (int i = 0; i < maxAcces; i++)
  385. {
  386. std::cerr << TlookupOld[i] << " ";
  387. if ( (i%numBins) == (numBins-1))
  388. std::cerr << std::endl;
  389. }
  390. std::cerr << "TlookupNew: " << std::endl;
  391. for (int i = 0; i < maxAcces; i++)
  392. {
  393. std::cerr << TlookupNew[i] << " ";
  394. if ( (i%numBins) == (numBins-1))
  395. std::cerr << std::endl;
  396. }
  397. }
  398. if (verbose)
  399. std::cerr << "fmk.hik_kernel_sum_fast ( Tlookup, q, xstar, beta_fast )" << std::endl;
  400. double beta_fast;
  401. fmk.hik_kernel_sum_fast ( TlookupNew, q, xstar, beta_fast );
  402. NICE::SparseVector xstar_sparse(xstar);
  403. double beta_fast_sparse;
  404. fmk.hik_kernel_sum_fast ( TlookupNew, q, xstar_sparse, beta_fast_sparse );
  405. double betaSparse;
  406. fmk.hik_kernel_sum ( A, B, xstar_sparse, betaSparse, &pf );
  407. // checking the result
  408. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  409. transposeVectorOfVectors(dataMatrix_transposed);
  410. NICE::GeneralizedIntersectionKernelFunction<double> hikSlow (gamma);
  411. vector<double> kstar_stl = hikSlow.computeKernelVector ( dataMatrix_transposed, xstar_stl );
  412. double beta_slow = 0.0;
  413. for ( uint i = 0 ; i < n; i++ )
  414. beta_slow += kstar_stl[i] * alpha[i];
  415. if (b_debug)
  416. 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;
  417. // clean-up
  418. delete [] TlookupNew;
  419. delete [] TlookupOld;
  420. // final assertion
  421. CPPUNIT_ASSERT_DOUBLES_EQUAL(beta_slow, beta_fast_sparse, 1e-8);
  422. } // for-loop
  423. // clean-up
  424. delete q;
  425. if (verboseStartEnd)
  426. std::cerr << "================== TestFastHIK::testKernelSumFast done ===================== " << std::endl;
  427. }
  428. void TestFastHIK::testLUTUpdate()
  429. {
  430. if (verboseStartEnd)
  431. std::cerr << "================== TestFastHIK::testLUTUpdate ===================== " << std::endl;
  432. NICE::Quantization * q;
  433. q = new Quantization1DAequiDist0To1 ( numBins );
  434. // data is generated, such that there is no approximation error
  435. std::vector< std::vector<double> > dataMatrix;
  436. for ( uint i = 0; i < d ; i++ )
  437. {
  438. std::vector<double> v;
  439. v.resize(n);
  440. for ( uint k = 0; k < n; k++ ) {
  441. if ( drand48() < sparse_prob ) {
  442. v[k] = 0;
  443. } else {
  444. v[k] = q->getPrototype( (rand() % numBins) );
  445. }
  446. }
  447. dataMatrix.push_back(v);
  448. }
  449. if ( b_debug ) {
  450. cerr << "data matrix: " << endl;
  451. printMatrix ( dataMatrix );
  452. cerr << endl;
  453. }
  454. double noise = 1.0;
  455. NICE::FastMinKernel fmk ( dataMatrix, noise );
  456. NICE::ParameterizedFunction *pf = new PFAbsExp ( 1.0 );
  457. NICE::Vector alpha ( n );
  458. for ( uint i = 0; i < alpha.size(); i++ )
  459. alpha[i] = sin(i);
  460. if (verbose)
  461. std::cerr << "prepare LUT" << std::endl;
  462. double * T = fmk.hikPrepareLookupTable(alpha, q, pf);
  463. if (verbose)
  464. std::cerr << "preparation done -- printing T" << std::endl;
  465. int maxAcces(numBins*d);
  466. if (verbose)
  467. {
  468. for (int i = 0; i < maxAcces; i++)
  469. {
  470. std::cerr << T[i] << " ";
  471. if ( (i%numBins) == (numBins-1))
  472. std::cerr << std::endl;
  473. }
  474. }
  475. //lets change index 2
  476. int idx(2);
  477. double valAlphaOld(alpha[idx]);
  478. double valAlphaNew(1.2); //this value is definitely different from the previous one
  479. Vector alphaNew(alpha);
  480. alphaNew[idx] = valAlphaNew;
  481. double * TNew = fmk.hikPrepareLookupTable(alphaNew, q, pf);
  482. if (verbose)
  483. std::cerr << "calculated the new LUT, no print it: " << std::endl;
  484. if (verbose)
  485. {
  486. for (int i = 0; i < maxAcces; i++)
  487. {
  488. std::cerr << TNew[i] << " ";
  489. if ( (i%numBins) == (numBins-1))
  490. std::cerr << std::endl;
  491. }
  492. }
  493. if (verbose)
  494. std::cerr << "change the old LUT by a new value for alpha_i" << std::endl;
  495. fmk.hikUpdateLookupTable(T, valAlphaNew, valAlphaOld, idx, q, pf );
  496. if (verbose)
  497. std::cerr << "update is done, now print the updated version: " << std::endl;
  498. if (verbose)
  499. {
  500. for (int i = 0; i < maxAcces; i++)
  501. {
  502. std::cerr << T[i] << " ";
  503. if ( (i%numBins) == (numBins-1))
  504. std::cerr << std::endl;
  505. }
  506. }
  507. bool equal = compareLUTs(T, TNew, q->getNumberOfBins()*d, 10e-8);
  508. if (verbose)
  509. {
  510. if (equal)
  511. std::cerr << "LUTs are equal :) " << std::endl;
  512. else
  513. {
  514. std::cerr << "T are not equal :( " << std::endl;
  515. for (uint i = 0; i < q->getNumberOfBins()*d; i++)
  516. {
  517. if ( (i % q->getNumberOfBins()) == 0)
  518. std::cerr << std::endl;
  519. std::cerr << T[i] << " ";
  520. }
  521. std::cerr << "TNew: "<< std::endl;
  522. for (uint i = 0; i < q->getNumberOfBins()*d; i++)
  523. {
  524. if ( (i % q->getNumberOfBins()) == 0)
  525. std::cerr << std::endl;
  526. std::cerr << TNew[i] << " ";
  527. }
  528. }
  529. }
  530. // clean-up
  531. delete q;
  532. delete pf;
  533. delete [] T;
  534. delete [] TNew;
  535. // final assertion
  536. CPPUNIT_ASSERT(equal == true);
  537. if (verboseStartEnd)
  538. std::cerr << "================== TestFastHIK::testLUTUpdate done ===================== " << std::endl;
  539. }
  540. void TestFastHIK::testLinSolve()
  541. {
  542. if (verboseStartEnd)
  543. std::cerr << "================== TestFastHIK::testLinSolve ===================== " << std::endl;
  544. NICE::Quantization * q;
  545. q = new Quantization1DAequiDist0To1 ( numBins );
  546. // data is generated, such that there is no approximation error
  547. std::vector< std::vector<double> > dataMatrix;
  548. for ( uint i = 0; i < d ; i++ )
  549. {
  550. std::vector<double> v;
  551. v.resize(n);
  552. for ( uint k = 0; k < n; k++ ) {
  553. if ( drand48() < sparse_prob ) {
  554. v[k] = 0;
  555. } else {
  556. v[k] = q->getPrototype( (rand() % numBins) );
  557. }
  558. }
  559. dataMatrix.push_back(v);
  560. }
  561. if ( b_debug ) {
  562. std::cerr << "data matrix: " << std::endl;
  563. printMatrix ( dataMatrix );
  564. std::cerr << std::endl;
  565. }
  566. // Let's test the FastMinKernel solveLin core methods.
  567. // Looks like their odd convergence makes it only possible
  568. // to test this for smaller cases.
  569. double noise = 1.0;
  570. NICE::FastMinKernel fmk ( dataMatrix, noise );
  571. NICE::ParameterizedFunction *pf = new NICE::PFAbsExp ( 1.0 );
  572. fmk.applyFunctionToFeatureMatrix( pf );
  573. NICE::Vector y ( n );
  574. for ( uint i = 0; i < y.size(); i++ )
  575. y[i] = sin(i);
  576. NICE::Vector alphaRandomized;
  577. if ( verbose )
  578. std::cerr << "solveLin with randomization" << std::endl;
  579. // tic
  580. NICE::Timer t;
  581. t.start();
  582. //let's try to do 10.000 iterations and sample in each iteration 30 examples randomly
  583. fmk.solveLin(y, alphaRandomized, q, pf, true, solveLinMaxIterations, 30 /* random subset */);
  584. //toc
  585. t.stop();
  586. float time_randomizedSolving = t.getLast();
  587. if ( verbose )
  588. std::cerr << "Time for solving with random subsets: " << time_randomizedSolving << " s" << std::endl;
  589. // test the case, where we first transform and then use the multiply stuff
  590. std::vector<std::vector<double> > dataMatrix_transposed (dataMatrix);
  591. transposeVectorOfVectors(dataMatrix_transposed);
  592. NICE::GeneralizedIntersectionKernelFunction<double> ghikSlow ( 1.0 );
  593. NICE::Matrix gK ( ghikSlow.computeKernelMatrix(dataMatrix_transposed, noise) );
  594. NICE::Vector K_alphaRandomized;
  595. K_alphaRandomized.multiply(gK, alphaRandomized);
  596. NICE::Vector alpha;
  597. if (solveLinWithoutRand)
  598. {
  599. if ( verbose )
  600. std::cerr << "solveLin without randomization" << std::endl;
  601. fmk.solveLin(y, alpha, q, pf, false, 30);
  602. Vector K_alpha;
  603. K_alpha.multiply(gK, alpha);
  604. if ( verbose )
  605. {
  606. std::cerr << "now assert that K_alpha == y" << std::endl;
  607. std::cerr << "(K_alpha-y).normL1(): " << (K_alpha - y).normL1() << std::endl;
  608. }
  609. }
  610. // std::cerr << "alpha: " << alpha << std::endl;
  611. // std::cerr << "K_times_alpha: " << K_alpha << std::endl;
  612. // std::cerr << "y: " << y << std::endl;
  613. //
  614. // Vector test_alpha;
  615. // ILSConjugateGradients cgm;
  616. // cgm.solveLin( GMStandard(gK),y,test_alpha);
  617. //
  618. // K_alpha.multiply( gK, test_alpha);
  619. //
  620. // std::cerr << "test_alpha (CGM): " << test_alpha << std::endl;
  621. // std::cerr << "K_times_alpha (CGM): " << K_alpha << std::endl;
  622. if ( verbose )
  623. {
  624. std::cerr << "now assert that K_alphaRandomized == y" << std::endl;
  625. std::cerr << "(K_alphaRandomized-y).normL1(): " << (K_alphaRandomized - y).normL1() << std::endl;
  626. }
  627. // clean-up
  628. delete q;
  629. delete pf;
  630. // final assertion
  631. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, (K_alphaRandomized-y).normL1(), 1e-6);
  632. if (verboseStartEnd)
  633. std::cerr << "================== TestFastHIK::testLinSolve done ===================== " << std::endl;
  634. }
  635. void TestFastHIK::testKernelVector()
  636. {
  637. if (verboseStartEnd)
  638. std::cerr << "================== TestFastHIK::testKernelVector ===================== " << std::endl;
  639. std::vector< std::vector<double> > dataMatrix;
  640. 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);
  641. 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);
  642. 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);
  643. if ( b_debug ) {
  644. std::cerr << "data matrix: " << std::endl;
  645. printMatrix ( dataMatrix );
  646. std::cerr << endl;
  647. }
  648. double noise = 1.0;
  649. FastMinKernel fmk ( dataMatrix, noise, b_debug );
  650. std::vector<double> xStar; xStar.push_back(0.2);xStar.push_back(0.7);xStar.push_back(0.1);
  651. NICE::Vector xStarVec (xStar);
  652. std::vector<double> x2; x2.push_back(0.7);x2.push_back(0.3);xStar.push_back(0.0);
  653. NICE::Vector x2Vec (x2);
  654. NICE::SparseVector xStarsparse( xStarVec );
  655. NICE::SparseVector x2sparse( x2Vec );
  656. if ( b_debug )
  657. {
  658. fmk.store ( std::cerr );
  659. xStarsparse.store ( std::cerr );
  660. }
  661. NICE::Vector k1;
  662. fmk.hikComputeKernelVector( xStarsparse, k1 );
  663. NICE::Vector k2;
  664. fmk.hikComputeKernelVector( x2sparse, k2 );
  665. NICE::Vector k1GT(5); k1GT[0] = 0.6; k1GT[1] = 0.8; k1GT[2] = 0.7; k1GT[3] = 0.5; k1GT[4] = 0.6;
  666. NICE::Vector k2GT(5); k2GT[0] = 0.5; k2GT[1] = 0.4; k2GT[2] = 0.3; k2GT[3] = 0.3; k2GT[4] = 0.7;
  667. if (b_debug)
  668. {
  669. std::cerr << "k1: " << k1 << std::endl;
  670. std::cerr << "GT: " << k1GT << std::endl;
  671. std::cerr << "k2: " << k2 << std::endl;
  672. std::cerr << "GT: " << k2GT << std::endl;
  673. }
  674. for (int i = 0; i < 5; i++)
  675. {
  676. CPPUNIT_ASSERT_DOUBLES_EQUAL(k1[i]-k1GT[i], 0.0, 1e-6);
  677. CPPUNIT_ASSERT_DOUBLES_EQUAL(k2[i]-k2GT[i], 0.0, 1e-6);
  678. }
  679. if (verboseStartEnd)
  680. std::cerr << "================== TestFastHIK::testKernelVector done ===================== " << std::endl;
  681. }
  682. #endif