TestFastHIK.cpp 25 KB

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