IKMNoise.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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->np = 0;
  20. this->nn = 0;
  21. this->verbose = false;
  22. }
  23. IKMNoise::IKMNoise( uint size, double noise, bool optimizeNoise )
  24. {
  25. this->size = size;
  26. this->noise = noise;
  27. this->optimizeNoise = optimizeNoise;
  28. this->np = 0;
  29. this->nn = 0;
  30. this->verbose = false;
  31. }
  32. IKMNoise::IKMNoise( const Vector & labels, double noise, bool optimizeNoise )
  33. {
  34. this->size = labels.size();
  35. this->noise = noise;
  36. this->optimizeNoise = optimizeNoise;
  37. this->labels = labels;
  38. this->np = 0;
  39. this->nn = 0;
  40. this->verbose = false;
  41. for ( uint i = 0 ; i < labels.size(); i++ )
  42. if ( labels[i] == 1 )
  43. this->np++;
  44. else
  45. this->nn++;
  46. if (verbose)
  47. {
  48. std::cerr << "IKMNoise np : " << np << " nn: " << nn << std::endl;
  49. }
  50. }
  51. IKMNoise::~IKMNoise()
  52. {
  53. }
  54. void IKMNoise::getDiagonalElements ( Vector & diagonalElements ) const
  55. {
  56. diagonalElements.resize( size );
  57. if ( labels.size() == 0 ) {
  58. diagonalElements.set( noise );
  59. } else {
  60. for ( uint i = 0 ; i < labels.size(); i++ )
  61. if ( labels[i] == 1 ) {
  62. diagonalElements[i] = 2*np*noise/size;
  63. } else {
  64. diagonalElements[i] = 2*nn*noise/size;
  65. }
  66. }
  67. }
  68. void IKMNoise::getFirstDiagonalElement ( double & diagonalElement ) const
  69. {
  70. if ( labels.size() == 0 )
  71. {
  72. if (verbose)
  73. {
  74. std::cerr << "IKMNoise::getFirstDiagonalElement and labels.size() is zero" << std::endl;
  75. }
  76. diagonalElement = noise ;
  77. }
  78. else
  79. {
  80. if ( labels[0] == 1 )
  81. {
  82. if (verbose)
  83. {
  84. std::cerr << "IKMNoise::getFirstDiagonalElement -- and first entry is +1" << std::endl;
  85. }
  86. diagonalElement = 2*np*noise/size;
  87. }
  88. else
  89. {
  90. if (verbose)
  91. {
  92. std::cerr << "IKMNoise::getFirstDiagonalElement -- and first entry is -1" << std::endl;
  93. }
  94. diagonalElement = 2*nn*noise/size;
  95. }
  96. }
  97. }
  98. uint IKMNoise::getNumParameters() const
  99. {
  100. return optimizeNoise ? 1 : 0;
  101. }
  102. void IKMNoise::getParameters(Vector & parameters) const
  103. {
  104. if ( optimizeNoise )
  105. {
  106. parameters.resize(1);
  107. parameters[0] = log(noise);
  108. }
  109. }
  110. void IKMNoise::setParameters(const Vector & parameters)
  111. {
  112. if ( optimizeNoise )
  113. {
  114. noise = exp(parameters[0]);
  115. }
  116. }
  117. bool IKMNoise::outOfBounds(const Vector & parameters) const
  118. {
  119. // we do not have any restrictions
  120. return false;
  121. }
  122. Vector IKMNoise::getParameterLowerBounds() const
  123. {
  124. Vector lB;
  125. if ( optimizeNoise ) {
  126. lB.resize(1);
  127. lB[0] = -std::numeric_limits<double>::max();
  128. }
  129. return lB;
  130. }
  131. Vector IKMNoise::getParameterUpperBounds() const
  132. {
  133. Vector uB;
  134. if ( optimizeNoise ) {
  135. uB.resize(1);
  136. uB[0] = -std::numeric_limits<double>::max();
  137. }
  138. return uB;
  139. }
  140. void IKMNoise::multiply (NICE::Vector & y, const NICE::Vector & x) const
  141. {
  142. y.resize( rows() );
  143. if ( labels.size() == 0 )
  144. {
  145. y = noise * x;
  146. } else {
  147. for ( uint i = 0 ; i < labels.size(); i++ )
  148. if ( labels[i] == 1 ) {
  149. y[i] = 2*np*noise/size * x[i];
  150. } else {
  151. y[i] = 2*nn*noise/size * x[i];
  152. }
  153. }
  154. }
  155. uint IKMNoise::rows () const
  156. {
  157. return cols();
  158. }
  159. uint IKMNoise::cols () const
  160. {
  161. return size;
  162. }
  163. double IKMNoise::approxFrobNorm() const
  164. {
  165. NICE::Vector diagEl;
  166. this->getDiagonalElements ( diagEl);
  167. return diagEl.normL2();
  168. }
  169. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  170. void IKMNoise::restore ( std::istream & is, int format )
  171. {
  172. if (is.good())
  173. {
  174. is.precision (std::numeric_limits<double>::digits10 + 1);
  175. std::string tmp;
  176. bool b_endOfBlock ( false ) ;
  177. while ( !b_endOfBlock )
  178. {
  179. is >> tmp; // start of block
  180. if ( this->isEndTag( tmp, "IKMNoise" ) )
  181. {
  182. b_endOfBlock = true;
  183. continue;
  184. }
  185. tmp = this->removeStartTag ( tmp );
  186. if ( tmp.compare("size") == 0 )
  187. {
  188. is >> size;
  189. }
  190. else if ( tmp.compare("noise") == 0 )
  191. {
  192. is >> noise;
  193. }
  194. else if ( tmp.compare("optimizeNoise") == 0 )
  195. {
  196. is >> optimizeNoise;
  197. }
  198. else if ( tmp.compare("np") == 0 )
  199. {
  200. is >> np;
  201. }
  202. else if ( tmp.compare("nn") == 0 )
  203. {
  204. is >> nn;
  205. }
  206. else if ( tmp.compare("labels") == 0 )
  207. {
  208. is >> labels;
  209. }
  210. else
  211. {
  212. std::cerr << "WARNING -- unexpected IKMNoise object -- " << tmp << " -- for restoration... aborting" << std::endl;
  213. throw;
  214. }
  215. is >> tmp; // end of block
  216. tmp = this->removeEndTag ( tmp );
  217. }
  218. }
  219. else
  220. {
  221. std::cerr << "IKMNoise::restore -- InStream not initialized - restoring not possible!" << std::endl;
  222. }
  223. }
  224. void IKMNoise::store ( std::ostream & os, int format ) const
  225. {
  226. // show starting point
  227. os << this->createStartTag( "IKMNoise" ) << std::endl;
  228. os << this->createStartTag( "size" ) << std::endl;
  229. os << size << std::endl;
  230. os << this->createEndTag( "size" ) << std::endl;
  231. os << this->createStartTag( "noise" ) << std::endl;
  232. os << noise << std::endl;
  233. os << this->createEndTag( "noise" ) << std::endl;
  234. os << this->createStartTag( "optimizeNoise" ) << std::endl;
  235. os << optimizeNoise << std::endl;
  236. os << this->createEndTag( "optimizeNoise" ) << std::endl;
  237. os << this->createStartTag( "np" ) << std::endl;
  238. os << np << std::endl;
  239. os << this->createEndTag( "np" ) << std::endl;
  240. os << this->createStartTag( "nn" ) << std::endl;
  241. os << nn << std::endl;
  242. os << this->createEndTag( "nn" ) << std::endl;
  243. os << this->createStartTag( "labels" ) << std::endl;
  244. os << labels << std::endl;
  245. os << this->createEndTag( "labels" ) << std::endl;
  246. // done
  247. os << this->createEndTag( "IKMNoise" ) << std::endl;
  248. }