TestFastHIK.cpp 25 KB

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