TestFastHIK.cpp 24 KB

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