104 std::vector<std::valarray<double>> *W,
105 std::valarray<double> *D,
double alpha,
int R) {
107 int num_out = W->size();
114 for (j = 0; j < num_out; j++) {
117 (*D)[j] = (((*W)[j] - x) * ((*W)[j] - x)).
sum();
122 auto result = std::min_element(std::begin(*D), std::end(*D));
124 int d_min_idx = std::distance(std::begin(*D), result);
127 int from_node = std::max(0, d_min_idx - R);
128 int to_node = std::min(num_out, d_min_idx + R + 1);
135 for (j = from_node; j < to_node; j++) {
137 (*W)[j] += alpha * (x - (*W)[j]);
150 std::vector<std::valarray<double>> *W,
152 int num_samples = X.size();
154 int num_out = W->size();
155 int R = num_out >> 2, iter = 0;
158 std::valarray<double> D(num_out);
163 for (
int sample = 0; sample < num_samples; sample++) {
169 if (iter % 10 == 0 && R > 1) {
175 }
while (alpha > alpha_min);
197 const int N =
data->size();
198 const double R = 0.75, dr = 0.3;
199 double a_t = 0., b_t = 2.f * M_PI;
200 double a_r = R - dr, b_r = R + dr;
206 for (i = 0; i < N; i++) {
208 double theta =
_random(a_t, b_t);
209 data[0][i][0] = r * cos(theta);
210 data[0][i][1] = r * sin(theta);
237 std::vector<std::valarray<double>> X(N);
238 std::vector<std::valarray<double>> W(num_out);
239 for (
int i = 0; i < std::max(num_out, N); i++) {
242 X[i] = std::valarray<double>(features);
245 W[i] = std::valarray<double>(features);
250 for (j = 0; j < features; j++) {
260 kohonen_som_tracer(X, &W, 0.1);
319 std::vector<std::valarray<double>> X(N);
320 std::vector<std::valarray<double>> W(num_out);
321 for (
int i = 0; i < std::max(num_out, N); i++) {
324 X[i] = std::valarray<double>(features);
327 W[i] = std::valarray<double>(features);
332 for (j = 0; j < features; j++) {
342 kohonen_som_tracer(X, &W, 0.01);
360 const int N =
data->size();
361 const double R = 0.1;
363 const int num_classes = 8;
364 const std::array<const std::array<double, 3>, num_classes> centres = {
366 std::array<double, 3>({.5, .5, .5}),
367 std::array<double, 3>({.5, .5, -.5}),
368 std::array<double, 3>({.5, -.5, .5}),
369 std::array<double, 3>({.5, -.5, -.5}),
370 std::array<double, 3>({-.5, .5, .5}),
371 std::array<double, 3>({-.5, .5, -.5}),
372 std::array<double, 3>({-.5, -.5, .5}),
373 std::array<double, 3>({-.5, -.5, -.5})
379 for (i = 0; i < N; i++) {
381 std::rand() % num_classes;
384 data[0][i][0] =
_random(centres[cls][0] - R, centres[cls][0] + R);
385 data[0][i][1] =
_random(centres[cls][1] - R, centres[cls][1] + R);
386 data[0][i][2] =
_random(centres[cls][2] - R, centres[cls][2] + R);
418 std::vector<std::valarray<double>> X(N);
419 std::vector<std::valarray<double>> W(num_out);
420 for (
int i = 0; i < std::max(num_out, N); i++) {
423 X[i] = std::valarray<double>(features);
426 W[i] = std::valarray<double>(features);
431 for (j = 0; j < features; j++) {
441 kohonen_som_tracer(X, &W, 0.01);