openGPMP
Open Source Mathematics Package
kohonen_net.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 #include <cmath>
34 #include <cstdlib>
35 #include <iostream>
37 
38 gpmp::ml::KohonenNet::KohonenNet(int in_size, int m_size)
39  : input_size(in_size), map_size(m_size) {
41 }
42 
44 }
45 
47  weights.resize(map_size, std::vector<double>(input_size));
48 
49  // Initialize weights randomly or using some other strategy
50  // For simplicity, we initialize them with random values between 0 and 1
51  for (int i = 0; i < map_size; ++i) {
52  for (int j = 0; j < input_size; ++j) {
53  weights[i][j] = static_cast<double>(rand()) / RAND_MAX;
54  }
55  }
56 }
57 
59  const std::vector<std::vector<double>> &inputData,
60  int epochs) {
61  for (int epoch = 0; epoch < epochs; ++epoch) {
62  for (const auto &inputVector : inputData) {
63  int bmu = best_matching_unit(inputVector);
64  double learningRate = 1.0 - static_cast<double>(epoch) / epochs;
65  update_weights(bmu, inputVector, learningRate);
66  }
67  }
68 }
69 
70 int gpmp::ml::KohonenNet::classify(const std::vector<double> &inputVector) {
71  return best_matching_unit(inputVector);
72 }
73 
75  const std::vector<double> &inputVector) {
76  int bmu = 0;
77  double minDistance = euclidean_distance(inputVector, weights[0]);
78 
79  for (int i = 1; i < map_size; ++i) {
80  double distance = euclidean_distance(inputVector, weights[i]);
81  if (distance < minDistance) {
82  minDistance = distance;
83  bmu = i;
84  }
85  }
86 
87  return bmu;
88 }
89 
91  int bmu,
92  const std::vector<double> &inputVector,
93  double learningRate) {
94  for (int i = 0; i < input_size; ++i) {
95  double delta = inputVector[i] - weights[bmu][i];
96  weights[bmu][i] += learningRate * delta;
97  }
98 }
99 
100 double
101 gpmp::ml::KohonenNet::euclidean_distance(const std::vector<double> &vec1,
102  const std::vector<double> &vec2) {
103  double distance = 0.0;
104  for (size_t i = 0; i < vec1.size(); ++i) {
105  double diff = vec1[i] - vec2[i];
106  distance += diff * diff;
107  }
108  return sqrt(distance);
109 }
KohonenNet(int inputSize, int mapSize)
Constructor for the KohonenNet class.
Definition: kohonen_net.cpp:38
void update_weights(int bmu, const std::vector< double > &input_vector, double learning_rate)
Updates the weights of the Kohonen network based on the best matching unit and input vector.
Definition: kohonen_net.cpp:90
~KohonenNet()
Destructor for the KohonenNet class.
Definition: kohonen_net.cpp:43
int classify(const std::vector< double > &inputVector)
Classifies a given input vector and returns the index of the best matching unit.
Definition: kohonen_net.cpp:70
void initialize_weights()
Initializes the weights of the Kohonen network.
Definition: kohonen_net.cpp:46
double euclidean_distance(const std::vector< double > &vec1, const std::vector< double > &vec2)
Calculates the Euclidean distance between two vectors.
void train(const std::vector< std::vector< double >> &inputData, int epochs=1000)
Trains the Kohonen network using input data.
Definition: kohonen_net.cpp:58
int best_matching_unit(const std::vector< double > &input_vector)
Finds the index of the best matching unit (BMU) for a given input vector.
Definition: kohonen_net.cpp:74