You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.9 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. //
  24. // Self-organizing map in TBB flow::graph
  25. //
  26. // we will do a color map (the simple example.)
  27. //
  28. // serial algorithm
  29. //
  30. // initialize map with vectors (could be random, gradient, or something else)
  31. // for some number of iterations
  32. // update radius r, weight of change L
  33. // for each example V
  34. // find the best matching unit
  35. // for each part of map within radius of BMU W
  36. // update vector: W(t+1) = W(t) + w(dist)*L*(V - W(t))
  37. #include "som.h"
  38. #include "tbb/task.h"
  39. std::ostream& operator<<( std::ostream &out, const SOM_element &s) {
  40. out << "(";
  41. for(int i=0;i<(int)s.w.size();++i) {
  42. out << s.w[i];
  43. if(i < (int)s.w.size()-1) {
  44. out << ",";
  45. }
  46. }
  47. out << ")";
  48. return out;
  49. }
  50. void remark_SOM_element(const SOM_element &s) {
  51. printf("(");
  52. for(int i=0;i<(int)s.w.size();++i) {
  53. printf("%g",s.w[i]);
  54. if(i < (int)s.w.size()-1) {
  55. printf(",");
  56. }
  57. }
  58. printf(")");
  59. }
  60. std::ostream& operator<<( std::ostream &out, const search_result_type &s) {
  61. out << "<";
  62. out << get<RADIUS>(s);
  63. out << ", " << get<XV>(s);
  64. out << ", ";
  65. out << get<YV>(s);
  66. out << ">";
  67. return out;
  68. }
  69. void remark_search_result_type(const search_result_type &s) {
  70. printf("<%g,%d,%d>", get<RADIUS>(s), get<XV>(s), get<YV>(s));
  71. }
  72. double
  73. randval( double lowlimit, double highlimit) {
  74. return double(rand()) / double(RAND_MAX) * (highlimit - lowlimit) + lowlimit;
  75. }
  76. void
  77. find_data_ranges(teaching_vector_type &teaching, SOM_element &max_range, SOM_element &min_range ) {
  78. if(teaching.size() == 0) return;
  79. max_range = min_range = teaching[0];
  80. for(int i = 1; i < (int)teaching.size(); ++i) {
  81. max_range.elementwise_max(teaching[i]);
  82. min_range.elementwise_min(teaching[i]);
  83. }
  84. }
  85. void add_fraction_of_difference( SOM_element &to, SOM_element const &from, double frac) {
  86. for(int i = 0; i < (int)from.size(); ++i) {
  87. to[i] += frac*(from[i] - to[i]);
  88. }
  89. }
  90. double
  91. distance_squared(SOM_element x, SOM_element y) {
  92. double rval = 0.0; for(int i=0;i<(int)x.size();++i) {
  93. double diff = x[i] - y[i];
  94. rval += diff*diff;
  95. }
  96. return rval;
  97. }
  98. void SOMap::initialize(InitializeType it, SOM_element &max_range, SOM_element &min_range) {
  99. for(int x = 0; x < xMax; ++x) {
  100. for(int y = 0; y < yMax; ++y) {
  101. for( int i = 0; i < (int)max_range.size(); ++i) {
  102. if(it == InitializeRandom) {
  103. my_map[x][y][i] = (randval(min_range[i], max_range[i]));
  104. }
  105. else if(it == InitializeGradient) {
  106. my_map[x][y][i] = ((double)(x+y)/(xMax+yMax)*(max_range[i]-min_range[i]) + min_range[i]);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. // subsquare [low,high)
  113. double
  114. SOMap::BMU_range( const SOM_element &s, int &xval, int &yval, subsquare_type &r) {
  115. double min_distance_squared = DBL_MAX;
  116. task &my_task = task::self();
  117. int min_x = -1;
  118. int min_y = -1;
  119. for(int x = r.rows().begin(); x != r.rows().end(); ++x) {
  120. for( int y = r.cols().begin(); y != r.cols().end(); ++y) {
  121. double dist = distance_squared(s,my_map[x][y]);
  122. if(dist < min_distance_squared) {
  123. min_distance_squared = dist;
  124. min_x = x;
  125. min_y = y;
  126. }
  127. if(cancel_test && my_task.is_cancelled()) {
  128. xval = r.rows().begin();
  129. yval = r.cols().begin();
  130. return DBL_MAX;
  131. }
  132. }
  133. }
  134. xval = min_x;
  135. yval = min_y;
  136. return sqrt(min_distance_squared);
  137. }
  138. void
  139. SOMap::epoch_update_range( SOM_element const &s, int epoch, int min_x, int min_y, double radius, double learning_rate, blocked_range<int> &r) {
  140. int min_xiter = (int)((double)min_x - radius);
  141. if(min_xiter < 0) min_xiter = 0;
  142. int max_xiter = (int)((double)min_x + radius);
  143. if(max_xiter > (int)my_map.size()-1) max_xiter = (int)my_map.size()-1;
  144. for(int xx = r.begin(); xx <= r.end(); ++xx) {
  145. double xrsq = (xx-min_x)*(xx-min_x);
  146. double ysq = radius*radius - xrsq; // max extent of y influence
  147. double yd;
  148. if(ysq > 0) {
  149. yd = sqrt(ysq);
  150. int lb = (int)(min_y - yd);
  151. int ub = (int)(min_y + yd);
  152. for(int yy = lb; yy < ub; ++yy) {
  153. if(yy >= 0 && yy < (int)my_map[xx].size()) {
  154. // [xx, yy] is in the range of the update.
  155. double my_rsq = xrsq + (yy-min_y)*(yy-min_y); // distance from BMU squared
  156. double theta = exp(-(radius*radius) /(2.0* my_rsq));
  157. add_fraction_of_difference(my_map[xx][yy], s, theta * learning_rate);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. void SOMap::teach(teaching_vector_type &in) {
  164. for(int i = 0; i < nPasses; ++i ) {
  165. int j = (int)(randval(0, (double)in.size())); // this won't be reproducible.
  166. if(j == in.size()) --j;
  167. int min_x = -1;
  168. int min_y = -1;
  169. subsquare_type br2(0, (int)my_map.size(), 1, 0, (int)my_map[0].size(), 1);
  170. (void) BMU_range(in[j],min_x, min_y, br2); // just need min_x, min_y
  171. // radius of interest
  172. double radius = max_radius * exp(-(double)i*radius_decay_rate);
  173. // update circle is min_xiter to max_xiter inclusive.
  174. double learning_rate = max_learning_rate * exp( -(double)i * learning_decay_rate);
  175. epoch_update(in[j], i, min_x, min_y, radius, learning_rate);
  176. }
  177. }
  178. void SOMap::debug_output() {
  179. printf("SOMap:\n");
  180. for(int i = 0; i < (int)(this->my_map.size()); ++i) {
  181. for(int j = 0; j < (int)(this->my_map[i].size()); ++j) {
  182. printf( "map[%d, %d] == ", i, j );
  183. remark_SOM_element( this->my_map[i][j] );
  184. printf("\n");
  185. }
  186. }
  187. }
  188. #define RED 0
  189. #define GREEN 1
  190. #define BLUE 2
  191. void readInputData() {
  192. my_teaching.push_back(SOM_element());
  193. my_teaching.push_back(SOM_element());
  194. my_teaching.push_back(SOM_element());
  195. my_teaching.push_back(SOM_element());
  196. my_teaching.push_back(SOM_element());
  197. my_teaching[0][RED] = 1.0; my_teaching[0][GREEN] = 0.0; my_teaching[0][BLUE] = 0.0;
  198. my_teaching[1][RED] = 0.0; my_teaching[1][GREEN] = 1.0; my_teaching[1][BLUE] = 0.0;
  199. my_teaching[2][RED] = 0.0; my_teaching[2][GREEN] = 0.0; my_teaching[2][BLUE] = 1.0;
  200. my_teaching[3][RED] = 0.3; my_teaching[3][GREEN] = 0.3; my_teaching[3][BLUE] = 0.0;
  201. my_teaching[4][RED] = 0.5; my_teaching[4][GREEN] = 0.5; my_teaching[4][BLUE] = 0.9;
  202. }