IKMLinearCombination.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @file IKMLinearCombination.cpp
  3. * @brief Combination of several (implicit) kernel matrices, such as noise matrix and gp-hik kernel matrix (Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 02/14/2012
  6. */
  7. // STL includes
  8. #include <iostream>
  9. // gp-hik-core includes
  10. #include "gp-hik-core/IKMLinearCombination.h"
  11. using namespace NICE;
  12. using namespace std;
  13. IKMLinearCombination::IKMLinearCombination()
  14. {
  15. this->verbose = false;
  16. }
  17. IKMLinearCombination::~IKMLinearCombination()
  18. {
  19. if ( this->matrices.size() != 0)
  20. {
  21. for (int i = 0; (uint)i < this->matrices.size(); i++)
  22. delete this->matrices[i];
  23. }
  24. }
  25. ///////////////////// ///////////////////// /////////////////////
  26. // GET / SET
  27. ///////////////////// ///////////////////// ///////////////////
  28. void IKMLinearCombination::getDiagonalElements ( Vector & diagonalElements ) const
  29. {
  30. diagonalElements.resize ( rows() );
  31. diagonalElements.set(0.0);
  32. for ( std::vector<NICE::ImplicitKernelMatrix *>::const_iterator i = this->matrices.begin(); i != this->matrices.end(); i++ )
  33. {
  34. NICE::ImplicitKernelMatrix *ikm = *i;
  35. NICE::Vector diagonalElementsSingle;
  36. ikm->getDiagonalElements ( diagonalElementsSingle );
  37. diagonalElements += diagonalElementsSingle;
  38. }
  39. }
  40. void IKMLinearCombination::getFirstDiagonalElement ( double & diagonalElement ) const
  41. {
  42. diagonalElement = 0.0;
  43. for ( std::vector<NICE::ImplicitKernelMatrix *>::const_iterator i = this->matrices.begin(); i != this->matrices.end(); i++ )
  44. {
  45. NICE::ImplicitKernelMatrix *ikm = *i;
  46. double firstElem;
  47. ikm->getFirstDiagonalElement(firstElem);
  48. diagonalElement += firstElem;
  49. }
  50. }
  51. uint IKMLinearCombination::getNumParameters() const
  52. {
  53. return parameterRanges[ parameterRanges.size() - 1 ] + matrices[ parameterRanges.size() - 1 ]->getNumParameters();
  54. }
  55. void IKMLinearCombination::getParameters(Vector & parameters) const
  56. {
  57. uint ind = 0;
  58. parameters.resize ( getNumParameters() );
  59. if (verbose)
  60. cerr << "Number of total parameters: " << parameters.size() << endl;
  61. for ( vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++, ind++ )
  62. {
  63. ImplicitKernelMatrix *ikm = *i;
  64. if (verbose)
  65. cerr << "Model " << ind << " has " << ikm->getNumParameters() << " parameters" << endl;
  66. if ( ikm->getNumParameters() == 0 ) continue;
  67. Vector singleParameterRef = parameters.getRangeRef( parameterRanges[ ind ], parameterRanges[ ind ] + ikm->getNumParameters() - 1 );
  68. ikm->getParameters ( singleParameterRef );
  69. }
  70. }
  71. void IKMLinearCombination::setParameters(const Vector & parameters)
  72. {
  73. uint ind = 0;
  74. for ( vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++, ind++ )
  75. {
  76. ImplicitKernelMatrix *ikm = *i;
  77. if ( ikm->getNumParameters() == 0 ) continue;
  78. ikm->setParameters ( parameters.getRange( parameterRanges[ ind ], parameterRanges[ ind ] + ikm->getNumParameters() - 1) );
  79. }
  80. }
  81. void IKMLinearCombination::setVerbose(const bool& _verbose)
  82. {
  83. verbose = _verbose;
  84. }
  85. bool IKMLinearCombination::outOfBounds(const Vector & parameters) const
  86. {
  87. uint ind = 0;
  88. for ( vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++, ind++ )
  89. {
  90. ImplicitKernelMatrix *ikm = *i;
  91. if ( ikm->getNumParameters() == 0 ) continue;
  92. if ( ikm->outOfBounds( parameters.getRange( parameterRanges[ ind ], parameterRanges[ ind ] + ikm->getNumParameters() - 1) ) )
  93. return true;
  94. }
  95. return false;
  96. }
  97. Vector IKMLinearCombination::getParameterLowerBounds() const
  98. {
  99. Vector lB;
  100. for ( vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++ )
  101. {
  102. ImplicitKernelMatrix *ikm = *i;
  103. if ( ikm->getNumParameters() == 0 ) continue;
  104. lB.append( ikm->getParameterLowerBounds() );
  105. }
  106. return lB;
  107. }
  108. Vector IKMLinearCombination::getParameterUpperBounds() const
  109. {
  110. Vector uB;
  111. for ( vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++ )
  112. {
  113. ImplicitKernelMatrix *ikm = *i;
  114. if ( ikm->getNumParameters() == 0 ) continue;
  115. uB.append( ikm->getParameterUpperBounds() );
  116. }
  117. return uB;
  118. }
  119. void IKMLinearCombination::updateParameterRanges()
  120. {
  121. if ( matrices.size() == 0 ) {
  122. parameterRanges.clear();
  123. } else {
  124. parameterRanges.resize(matrices.size());
  125. parameterRanges[0] = 0;
  126. if ( matrices.size() == 1 ) return;
  127. uint ind = 1;
  128. vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin();
  129. for ( ; ind < parameterRanges.size(); i++, ind++ )
  130. {
  131. ImplicitKernelMatrix *ikm = *i;
  132. if (verbose)
  133. cerr << "Parameter range: size is " << parameterRanges.size() << ", index is " << ind << endl;
  134. parameterRanges[ind] = parameterRanges[ind-1] + ikm->getNumParameters();
  135. if (verbose)
  136. cerr << "Value is " << parameterRanges[ind] << endl;
  137. }
  138. }
  139. }
  140. void IKMLinearCombination::addModel ( ImplicitKernelMatrix *ikm )
  141. {
  142. matrices.push_back ( ikm );
  143. updateParameterRanges();
  144. }
  145. void IKMLinearCombination::multiply (NICE::Vector & y, const NICE::Vector & x) const
  146. {
  147. y.resize( rows() );
  148. y.set(0.0);
  149. for ( vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++ )
  150. {
  151. ImplicitKernelMatrix *ikm = *i;
  152. Vector ySingle;
  153. ikm->multiply ( ySingle, x );
  154. y += ySingle;
  155. }
  156. }
  157. uint IKMLinearCombination::rows () const
  158. {
  159. return cols();
  160. }
  161. uint IKMLinearCombination::cols () const
  162. {
  163. if ( matrices.empty() )
  164. fthrow(Exception, "No models stored, cols() and rows() are unavailable");
  165. return (* matrices.begin())->cols();
  166. }
  167. double IKMLinearCombination::approxFrobNorm() const
  168. {
  169. double frobNormApprox(0.0);
  170. if (verbose)
  171. std::cerr << "IKMLinCom: single approx: " ;
  172. for ( vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++ )
  173. {
  174. ImplicitKernelMatrix *ikm = *i;
  175. frobNormApprox += ikm->approxFrobNorm();
  176. if (verbose)
  177. std::cerr << ikm->approxFrobNorm() << " ";
  178. }
  179. if (verbose)
  180. std::cerr << std::endl;
  181. return frobNormApprox;
  182. }
  183. void IKMLinearCombination::setApproximationScheme(const int & _approxScheme)
  184. {
  185. for ( std::vector<ImplicitKernelMatrix *>::const_iterator i = matrices.begin(); i != matrices.end(); i++ )
  186. {
  187. ImplicitKernelMatrix *ikm = *i;
  188. ikm->setApproximationScheme(_approxScheme);
  189. }
  190. }
  191. ImplicitKernelMatrix * IKMLinearCombination::getModel(const uint & idx) const
  192. {
  193. if ( idx <= matrices.size() )
  194. return matrices[idx];
  195. else
  196. return NULL;
  197. }
  198. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  199. void IKMLinearCombination::restore ( std::istream & is, int format )
  200. {
  201. if (is.good())
  202. {
  203. is.precision (std::numeric_limits<double>::digits10 + 1);
  204. std::string tmp;
  205. bool b_endOfBlock ( false ) ;
  206. while ( !b_endOfBlock )
  207. {
  208. is >> tmp; // start of block
  209. if ( this->isEndTag( tmp, "IKMLinearCombination" ) )
  210. {
  211. b_endOfBlock = true;
  212. continue;
  213. }
  214. tmp = this->removeStartTag ( tmp );
  215. is >> tmp; // end of block
  216. tmp = this->removeEndTag ( tmp );
  217. }
  218. }
  219. }
  220. void IKMLinearCombination::store ( std::ostream & os, int format ) const
  221. {
  222. if ( os.good() )
  223. {
  224. // show starting point
  225. os << this->createStartTag( "IKMLinearCombination" ) << std::endl;
  226. // done
  227. os << this->createEndTag( "IKMLinearCombination" ) << std::endl;
  228. }
  229. else
  230. {
  231. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  232. }
  233. }
  234. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  235. // interface specific methods for incremental extensions
  236. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  237. void IKMLinearCombination::addExample( const NICE::SparseVector * example,
  238. const double & label,
  239. const bool & performOptimizationAfterIncrement
  240. )
  241. {
  242. for ( std::vector<NICE::ImplicitKernelMatrix *>::iterator i = matrices.begin(); i != matrices.end(); i++ )
  243. {
  244. (*i)->addExample( example, label);
  245. }
  246. }
  247. void IKMLinearCombination::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  248. const NICE::Vector & newLabels,
  249. const bool & performOptimizationAfterIncrement
  250. )
  251. {
  252. for ( std::vector<NICE::ImplicitKernelMatrix *>::iterator i = matrices.begin(); i != matrices.end(); i++ )
  253. {
  254. (*i)->addMultipleExamples( newExamples, newLabels);
  255. }
  256. }