openGPMP
Open Source Mathematics Package
Public Member Functions | Private Member Functions | Private Attributes | List of all members
gpmp::ml::KohonenNet Class Reference

Kohonen Neural Network Cluster Class. More...

#include <kohonen_net.hpp>

Public Member Functions

 KohonenNet (int inputSize, int mapSize)
 Constructor for the KohonenNet class. More...
 
 ~KohonenNet ()
 Destructor for the KohonenNet class. More...
 
void train (const std::vector< std::vector< double >> &inputData, int epochs=1000)
 Trains the Kohonen network using input data. More...
 
int classify (const std::vector< double > &inputVector)
 Classifies a given input vector and returns the index of the best matching unit. More...
 

Private Member Functions

void initialize_weights ()
 Initializes the weights of the Kohonen network. More...
 
int best_matching_unit (const std::vector< double > &input_vector)
 Finds the index of the best matching unit (BMU) for a given input vector. More...
 
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. More...
 
double euclidean_distance (const std::vector< double > &vec1, const std::vector< double > &vec2)
 Calculates the Euclidean distance between two vectors. More...
 

Private Attributes

int input_size
 
int map_size
 
std::vector< std::vector< double > > weights
 

Detailed Description

Kohonen Neural Network Cluster Class.

Referred to as Self-Organizing Map

Definition at line 52 of file kohonen_net.hpp.

Constructor & Destructor Documentation

◆ KohonenNet()

gpmp::ml::KohonenNet::KohonenNet ( int  inputSize,
int  mapSize 
)

Constructor for the KohonenNet class.

Parameters
inputSizeThe size of the input vector
mapSizeThe size of the map in the Kohonen network

Definition at line 38 of file kohonen_net.cpp.

39  : input_size(in_size), map_size(m_size) {
41 }
void initialize_weights()
Initializes the weights of the Kohonen network.
Definition: kohonen_net.cpp:46

References initialize_weights().

◆ ~KohonenNet()

gpmp::ml::KohonenNet::~KohonenNet ( )

Destructor for the KohonenNet class.

Definition at line 43 of file kohonen_net.cpp.

43  {
44 }

Member Function Documentation

◆ best_matching_unit()

int gpmp::ml::KohonenNet::best_matching_unit ( const std::vector< double > &  input_vector)
private

Finds the index of the best matching unit (BMU) for a given input vector.

Parameters
input_vectorThe input vector
Returns
The index of the best matching unit

Definition at line 74 of file kohonen_net.cpp.

75  {
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 }
double euclidean_distance(const std::vector< double > &vec1, const std::vector< double > &vec2)
Calculates the Euclidean distance between two vectors.
std::vector< std::vector< double > > weights
Definition: kohonen_net.hpp:85

◆ classify()

int gpmp::ml::KohonenNet::classify ( const std::vector< double > &  inputVector)

Classifies a given input vector and returns the index of the best matching unit.

Parameters
inputVectorThe input vector to be classified
Returns
The index of the best matching unit

Definition at line 70 of file kohonen_net.cpp.

70  {
71  return best_matching_unit(inputVector);
72 }
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

◆ euclidean_distance()

double gpmp::ml::KohonenNet::euclidean_distance ( const std::vector< double > &  vec1,
const std::vector< double > &  vec2 
)
private

Calculates the Euclidean distance between two vectors.

Parameters
vec1The first vector
vec2The second vector
Returns
The Euclidean distance between the two vectors

Definition at line 101 of file kohonen_net.cpp.

102  {
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 }

◆ initialize_weights()

void gpmp::ml::KohonenNet::initialize_weights ( )
private

Initializes the weights of the Kohonen network.

Definition at line 46 of file kohonen_net.cpp.

46  {
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 }

Referenced by KohonenNet().

◆ train()

void gpmp::ml::KohonenNet::train ( const std::vector< std::vector< double >> &  inputData,
int  epochs = 1000 
)

Trains the Kohonen network using input data.

Parameters
inputDataThe input data consisting of vectors
epochsThe number of training epochs (default: 1000)

Definition at line 58 of file kohonen_net.cpp.

60  {
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 }
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

◆ update_weights()

void gpmp::ml::KohonenNet::update_weights ( int  bmu,
const std::vector< double > &  input_vector,
double  learning_rate 
)
private

Updates the weights of the Kohonen network based on the best matching unit and input vector.

Parameters
bmuThe index of the best matching unit
input_vectorThe input vector
learning_rateThe learning rate for weight updates

Definition at line 90 of file kohonen_net.cpp.

93  {
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 }

Member Data Documentation

◆ input_size

int gpmp::ml::KohonenNet::input_size
private

The size of the input vector

Definition at line 82 of file kohonen_net.hpp.

◆ map_size

int gpmp::ml::KohonenNet::map_size
private

The size of the map in the Kohonen network

Definition at line 83 of file kohonen_net.hpp.

◆ weights

std::vector<std::vector<double> > gpmp::ml::KohonenNet::weights
private

Weight matrix for the Kohonen network

Definition at line 85 of file kohonen_net.hpp.


The documentation for this class was generated from the following files: