TestFastHIK.cpp 23 KB

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