TestFastHIK.cpp 24 KB

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