IKMNoise.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @file IKMNoise.cpp
  3. * @author Erik Rodner, Alexander Freytag
  4. * @brief Noise matrix (for model regularization) as an implicit kernel matrix (Implementation)
  5. * @date 02/14/2012
  6. */
  7. // STL includes
  8. #include <iostream>
  9. #include <limits>
  10. // NICE-core includes
  11. #include "IKMNoise.h"
  12. using namespace NICE;
  13. using namespace std;
  14. IKMNoise::IKMNoise()
  15. {
  16. this->size = 0;
  17. this->noise = 0.1;
  18. this->optimizeNoise = false;
  19. this->verbose = false;
  20. }
  21. IKMNoise::IKMNoise( uint size, double noise, bool optimizeNoise )
  22. {
  23. this->size = size;
  24. this->noise = noise;
  25. this->optimizeNoise = optimizeNoise;
  26. this->verbose = false;
  27. }
  28. IKMNoise::~IKMNoise()
  29. {
  30. }
  31. void IKMNoise::getDiagonalElements ( Vector & diagonalElements ) const
  32. {
  33. diagonalElements.resize( size );
  34. diagonalElements.set( noise );
  35. }
  36. void IKMNoise::getFirstDiagonalElement ( double & diagonalElement ) const
  37. {
  38. if (verbose)
  39. {
  40. std::cerr << "IKMNoise::getFirstDiagonalElement and labels.size() is zero" << std::endl;
  41. }
  42. diagonalElement = noise ;
  43. }
  44. uint IKMNoise::getNumParameters() const
  45. {
  46. return optimizeNoise ? 1 : 0;
  47. }
  48. void IKMNoise::getParameters(Vector & parameters) const
  49. {
  50. if ( optimizeNoise )
  51. {
  52. parameters.resize(1);
  53. parameters[0] = log(noise);
  54. }
  55. }
  56. void IKMNoise::setParameters(const Vector & parameters)
  57. {
  58. if ( optimizeNoise )
  59. {
  60. noise = exp(parameters[0]);
  61. }
  62. }
  63. bool IKMNoise::outOfBounds(const Vector & parameters) const
  64. {
  65. // we do not have any restrictions
  66. return false;
  67. }
  68. Vector IKMNoise::getParameterLowerBounds() const
  69. {
  70. Vector lB;
  71. if ( optimizeNoise ) {
  72. lB.resize(1);
  73. lB[0] = -std::numeric_limits<double>::max();
  74. }
  75. return lB;
  76. }
  77. Vector IKMNoise::getParameterUpperBounds() const
  78. {
  79. Vector uB;
  80. if ( optimizeNoise ) {
  81. uB.resize(1);
  82. uB[0] = -std::numeric_limits<double>::max();
  83. }
  84. return uB;
  85. }
  86. void IKMNoise::multiply (NICE::Vector & y, const NICE::Vector & x) const
  87. {
  88. y.resize( rows() );
  89. y = noise * x;
  90. }
  91. uint IKMNoise::rows () const
  92. {
  93. return cols();
  94. }
  95. uint IKMNoise::cols () const
  96. {
  97. return size;
  98. }
  99. double IKMNoise::approxFrobNorm() const
  100. {
  101. NICE::Vector diagEl;
  102. this->getDiagonalElements ( diagEl);
  103. return diagEl.normL2();
  104. }
  105. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  106. void IKMNoise::restore ( std::istream & is, int format )
  107. {
  108. if (is.good())
  109. {
  110. is.precision (std::numeric_limits<double>::digits10 + 1);
  111. std::string tmp;
  112. bool b_endOfBlock ( false ) ;
  113. while ( !b_endOfBlock )
  114. {
  115. is >> tmp; // start of block
  116. if ( this->isEndTag( tmp, "IKMNoise" ) )
  117. {
  118. b_endOfBlock = true;
  119. continue;
  120. }
  121. tmp = this->removeStartTag ( tmp );
  122. if ( tmp.compare("size") == 0 )
  123. {
  124. is >> size;
  125. }
  126. else if ( tmp.compare("noise") == 0 )
  127. {
  128. is >> noise;
  129. }
  130. else if ( tmp.compare("optimizeNoise") == 0 )
  131. {
  132. is >> optimizeNoise;
  133. }
  134. else
  135. {
  136. std::cerr << "WARNING -- unexpected IKMNoise object -- " << tmp << " -- for restoration... aborting" << std::endl;
  137. throw;
  138. }
  139. is >> tmp; // end of block
  140. tmp = this->removeEndTag ( tmp );
  141. }
  142. }
  143. else
  144. {
  145. std::cerr << "IKMNoise::restore -- InStream not initialized - restoring not possible!" << std::endl;
  146. }
  147. }
  148. void IKMNoise::store ( std::ostream & os, int format ) const
  149. {
  150. // show starting point
  151. os << this->createStartTag( "IKMNoise" ) << std::endl;
  152. os << this->createStartTag( "size" ) << std::endl;
  153. os << size << std::endl;
  154. os << this->createEndTag( "size" ) << std::endl;
  155. os << this->createStartTag( "noise" ) << std::endl;
  156. os << noise << std::endl;
  157. os << this->createEndTag( "noise" ) << std::endl;
  158. os << this->createStartTag( "optimizeNoise" ) << std::endl;
  159. os << optimizeNoise << std::endl;
  160. os << this->createEndTag( "optimizeNoise" ) << std::endl;
  161. // done
  162. os << this->createEndTag( "IKMNoise" ) << std::endl;
  163. }
  164. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  165. // interface specific methods for incremental extensions
  166. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  167. void IKMNoise::addExample( const NICE::SparseVector * example,
  168. const double & label,
  169. const bool & performOptimizationAfterIncrement
  170. )
  171. {
  172. this->size++;
  173. }
  174. void IKMNoise::addMultipleExamples( const std::vector< const NICE::SparseVector * > & newExamples,
  175. const NICE::Vector & newLabels,
  176. const bool & performOptimizationAfterIncrement
  177. )
  178. {
  179. this->size += newExamples.size();
  180. }