TestFastHIK.cpp 23 KB

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