39 : input_size(in_size), map_size(m_size) {
47 weights.resize(map_size, std::vector<double>(input_size));
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;
59 const std::vector<std::vector<double>> &inputData,
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);
71 return best_matching_unit(inputVector);
75 const std::vector<double> &inputVector) {
77 double minDistance = euclidean_distance(inputVector, weights[0]);
79 for (
int i = 1; i < map_size; ++i) {
80 double distance = euclidean_distance(inputVector, weights[i]);
81 if (distance < minDistance) {
82 minDistance = distance;
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;
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;
108 return sqrt(distance);
KohonenNet(int inputSize, int mapSize)
Constructor for the KohonenNet class.
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.
~KohonenNet()
Destructor for the KohonenNet class.
int classify(const std::vector< double > &inputVector)
Classifies a given input vector and returns the index of the best matching unit.
void initialize_weights()
Initializes the weights of the Kohonen network.
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.
int best_matching_unit(const std::vector< double > &input_vector)
Finds the index of the best matching unit (BMU) for a given input vector.