TestFastHIK.cpp 24 KB

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