Kohonen self organizing map (topological map)
More...
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <valarray>
#include <vector>
|
|
#define | _USE_MATH_DEFINES |
| |
|
#define | MIN_DISTANCE 1e-4 |
| | Minimum average distance of image nodes.
|
| |
|
| double | _random (double a, double b) |
| |
| int | save_2d_data (const char *fname, const std::vector< std::valarray< double >> &X) |
| |
| void | get_min_2d (const std::vector< std::valarray< double >> &X, double *val, int *x_idx, int *y_idx) |
| |
| int | machine_learning::save_u_matrix (const char *fname, const std::vector< std::vector< std::valarray< double >>> &W) |
| |
| double | machine_learning::update_weights (const std::valarray< double > &X, std::vector< std::vector< std::valarray< double >>> *W, std::vector< std::valarray< double >> *D, double alpha, int R) |
| |
| void | machine_learning::kohonen_som (const std::vector< std::valarray< double >> &X, std::vector< std::vector< std::valarray< double >>> *W, double alpha_min) |
| |
| void | test_2d_classes (std::vector< std::valarray< double >> *data) |
| |
| void | test1 () |
| |
| void | test_3d_classes1 (std::vector< std::valarray< double >> *data) |
| |
| void | test2 () |
| |
| void | test_3d_classes2 (std::vector< std::valarray< double >> *data) |
| |
| void | test3 () |
| |
| double | get_clock_diff (clock_t start_t, clock_t end_t) |
| |
| int | main (int argc, char **argv) |
| |
Kohonen self organizing map (topological map)
- Author
- Krishna Vedala This example implements a powerful unsupervised learning algorithm called as a self organizing map. The algorithm creates a connected network of weights that closely follows the given data points. This thus creates a topological map of the given data i.e., it maintains the relationship between varipus data points in a much higher dimesional space by creating an equivalent in a 2-dimensional space.
- Note
- This C++ version of the program is considerable slower than its C counterpart
-
The compiled code is much slower when compiled with MS Visual C++ 2019 than with GCC on windows
- See also
- kohonen_som_trace.cpp
◆ get_clock_diff()
| double get_clock_diff |
( |
clock_t |
start_t, |
|
|
clock_t |
end_t |
|
) |
| |
Convert clock cycle difference to time in seconds
- Parameters
-
| [in] | start_t | start clock |
| [in] | end_t | end clock |
- Returns
- time difference in seconds
560 return static_cast<double>(end_t - start_t) / CLOCKS_PER_SEC;
◆ main()
| int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Main function
566 std::cout <<
"Using OpenMP based parallelization\n";
568 std::cout <<
"NOT using OpenMP based parallelization\n";
592 <<
"(Note: Calculated times include: creating test sets, training "
593 "model and writing files to disk.)\n\n";
◆ test1()
Test that creates a random set of points distributed in four clusters in circumference of a circle and trains an SOM that finds that circular pattern. The following CSV files are created to validate the execution:
test1.csv: random test samples points with a circular pattern
w11.csv: initial random map
w12.csv: trained SOM map
363 for (
int i = 0; i <
std::max(num_out, N); i++) {
369 for (
int k = 0;
k < num_out;
k++) {
374 for (j = 0; j < features; j++)
◆ test2()
Test that creates a random set of points distributed in 4 clusters in 3D space and trains an SOM that finds the topological pattern. The following CSV files are created to validate the execution:
test2.csv: random test samples points with a lamniscate pattern
w21.csv: initial random map
w22.csv: trained SOM map
443 for (
int i = 0; i <
std::max(num_out, N); i++) {
449 for (
int k = 0;
k < num_out;
k++) {
454 for (j = 0; j < features; j++)
◆ test3()
Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM that finds the topological pattern. The following CSV files are created to validate the execution:
test3.csv: random test samples points with a circular pattern
w31.csv: initial random map
w32.csv: trained SOM map
527 for (
int i = 0; i <
std::max(num_out, N); i++) {
533 for (
int k = 0;
k < num_out;
k++) {
538 for (j = 0; j < features; j++)
◆ test_2d_classes()
Creates a random set of points distributed in four clusters in 3D space with centroids at the points
- \((0,5, 0.5, 0.5)\)
- \((0,5,-0.5, -0.5)\)
- \((-0,5, 0.5, 0.5)\)
- \((-0,5,-0.5, -0.5)\)
- Parameters
-
| [out] | data | matrix to store data in |
319 const int N =
data->size();
320 const double R = 0.3;
322 const int num_classes = 4;
323 const double centres[][2] = {
334 for (i = 0; i < N; i++) {
339 data[0][i][0] =
_random(centres[cls][0] - R, centres[cls][0] + R);
340 data[0][i][1] =
_random(centres[cls][1] - R, centres[cls][1] + R);
◆ test_3d_classes1()
Creates a random set of points distributed in four clusters in 3D space with centroids at the points
- \((0,5, 0.5, 0.5)\)
- \((0,5,-0.5, -0.5)\)
- \((-0,5, 0.5, 0.5)\)
- \((-0,5,-0.5, -0.5)\)
- Parameters
-
| [out] | data | matrix to store data in |
398 const int N =
data->size();
399 const double R = 0.3;
401 const int num_classes = 4;
402 const double centres[][3] = {
413 for (i = 0; i < N; i++) {
418 data[0][i][0] =
_random(centres[cls][0] - R, centres[cls][0] + R);
419 data[0][i][1] =
_random(centres[cls][1] - R, centres[cls][1] + R);
420 data[0][i][2] =
_random(centres[cls][2] - R, centres[cls][2] + R);
◆ test_3d_classes2()
Creates a random set of points distributed in four clusters in 3D space with centroids at the points
- \((0,5, 0.5, 0.5)\)
- \((0,5,-0.5, -0.5)\)
- \((-0,5, 0.5, 0.5)\)
- \((-0,5,-0.5, -0.5)\)
- Parameters
-
| [out] | data | matrix to store data in |
478 const int N =
data->size();
479 const double R = 0.2;
481 const int num_classes = 8;
482 const double centres[][3] = {
497 for (i = 0; i < N; i++) {
502 data[0][i][0] =
_random(centres[cls][0] - R, centres[cls][0] + R);
503 data[0][i][1] =
_random(centres[cls][1] - R, centres[cls][1] + R);
504 data[0][i][2] =
_random(centres[cls][2] - R, centres[cls][2] + R);