IKMNoise.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include <iostream>
  8. #include <limits>
  9. #include "IKMNoise.h"
  10. using namespace NICE;
  11. using namespace std;
  12. IKMNoise::IKMNoise()
  13. {
  14. this->size = 0;
  15. this->noise = 0.1;
  16. this->optimizeNoise = false;
  17. this->np = 0;
  18. this->nn = 0;
  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->np = 0;
  27. this->nn = 0;
  28. this->verbose = false;
  29. }
  30. IKMNoise::IKMNoise( const Vector & labels, double noise, bool optimizeNoise )
  31. {
  32. this->size = labels.size();
  33. this->noise = noise;
  34. this->optimizeNoise = optimizeNoise;
  35. this->labels = labels;
  36. this->np = 0;
  37. this->nn = 0;
  38. this->verbose = false;
  39. for ( uint i = 0 ; i < labels.size(); i++ )
  40. if ( labels[i] == 1 )
  41. this->np++;
  42. else
  43. this->nn++;
  44. if (verbose)
  45. {
  46. std::cerr << "IKMNoise np : " << np << " nn: " << nn << std::endl;
  47. }
  48. }
  49. IKMNoise::~IKMNoise()
  50. {
  51. }
  52. void IKMNoise::getDiagonalElements ( Vector & diagonalElements ) const
  53. {
  54. diagonalElements.resize( size );
  55. if ( labels.size() == 0 ) {
  56. diagonalElements.set( noise );
  57. } else {
  58. for ( uint i = 0 ; i < labels.size(); i++ )
  59. if ( labels[i] == 1 ) {
  60. diagonalElements[i] = 2*np*noise/size;
  61. } else {
  62. diagonalElements[i] = 2*nn*noise/size;
  63. }
  64. }
  65. }
  66. void IKMNoise::getFirstDiagonalElement ( double & diagonalElement ) const
  67. {
  68. if ( labels.size() == 0 )
  69. {
  70. if (verbose)
  71. {
  72. std::cerr << "IKMNoise::getFirstDiagonalElement and labels.size() is zero" << std::endl;
  73. }
  74. diagonalElement = noise ;
  75. }
  76. else
  77. {
  78. if ( labels[0] == 1 )
  79. {
  80. if (verbose)
  81. {
  82. std::cerr << "IKMNoise::getFirstDiagonalElement -- and first entry is +1" << std::endl;
  83. }
  84. diagonalElement = 2*np*noise/size;
  85. }
  86. else
  87. {
  88. if (verbose)
  89. {
  90. std::cerr << "IKMNoise::getFirstDiagonalElement -- and first entry is -1" << std::endl;
  91. }
  92. diagonalElement = 2*nn*noise/size;
  93. }
  94. }
  95. }
  96. uint IKMNoise::getNumParameters() const
  97. {
  98. return optimizeNoise ? 1 : 0;
  99. }
  100. void IKMNoise::getParameters(Vector & parameters) const
  101. {
  102. if ( optimizeNoise )
  103. {
  104. parameters.resize(1);
  105. parameters[0] = log(noise);
  106. }
  107. }
  108. void IKMNoise::setParameters(const Vector & parameters)
  109. {
  110. if ( optimizeNoise )
  111. {
  112. noise = exp(parameters[0]);
  113. }
  114. }
  115. bool IKMNoise::outOfBounds(const Vector & parameters) const
  116. {
  117. // we do not have any restrictions
  118. return false;
  119. }
  120. Vector IKMNoise::getParameterLowerBounds() const
  121. {
  122. Vector lB;
  123. if ( optimizeNoise ) {
  124. lB.resize(1);
  125. lB[0] = -std::numeric_limits<double>::max();
  126. }
  127. return lB;
  128. }
  129. Vector IKMNoise::getParameterUpperBounds() const
  130. {
  131. Vector uB;
  132. if ( optimizeNoise ) {
  133. uB.resize(1);
  134. uB[0] = -std::numeric_limits<double>::max();
  135. }
  136. return uB;
  137. }
  138. void IKMNoise::multiply (NICE::Vector & y, const NICE::Vector & x) const
  139. {
  140. y.resize( rows() );
  141. if ( labels.size() == 0 )
  142. {
  143. y = noise * x;
  144. } else {
  145. for ( uint i = 0 ; i < labels.size(); i++ )
  146. if ( labels[i] == 1 ) {
  147. y[i] = 2*np*noise/size * x[i];
  148. } else {
  149. y[i] = 2*nn*noise/size * x[i];
  150. }
  151. }
  152. }
  153. uint IKMNoise::rows () const
  154. {
  155. return cols();
  156. }
  157. uint IKMNoise::cols () const
  158. {
  159. return size;
  160. }
  161. double IKMNoise::approxFrobNorm() const
  162. {
  163. NICE::Vector diagEl;
  164. this->getDiagonalElements ( diagEl);
  165. return diagEl.normL2();
  166. }
  167. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  168. void IKMNoise::restore ( std::istream & is, int format )
  169. {
  170. if (is.good())
  171. {
  172. is.precision (std::numeric_limits<double>::digits10 + 1);
  173. std::string tmp;
  174. is >> tmp; //class name
  175. is >> tmp;
  176. is >> size;
  177. is >> tmp;
  178. is >> noise;
  179. is >> tmp;
  180. is >> optimizeNoise;
  181. is >> tmp;
  182. is >> np;
  183. is >> tmp;
  184. is >> nn;
  185. is >> tmp;
  186. is >> labels;
  187. }
  188. }
  189. void IKMNoise::store ( std::ostream & os, int format ) const
  190. {
  191. os << "IKMNoise" << std::endl;
  192. os << "size: " << size << std::endl;
  193. os << "noise: " << noise << std::endl;
  194. os << "optimizeNoise: " << optimizeNoise << std::endl;
  195. os << "np: " << np << std::endl;
  196. os << "nn: " << nn << std::endl;
  197. os << "labels: " << labels << std::endl;
  198. }