Skip to content

Commit

Permalink
Create function to find BMU
Browse files Browse the repository at this point in the history
  • Loading branch information
sumedhe committed Oct 7, 2018
1 parent e83672c commit 00a7e5b
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 24 deletions.
4 changes: 2 additions & 2 deletions resources/input.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
12,3,4
1,1,1.1
2,3,4
6,5,4
6,5,4.0
2 changes: 1 addition & 1 deletion src/core/trainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class trainer {
public:
std::vector<std::vector<int>> input_vector;
std::vector<std::vector<float>> input_space;
Grid* grid;
};

Expand Down
19 changes: 18 additions & 1 deletion src/grid/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@
//

#include "grid.h"

#include <limits>

#include "../utils/math_helper.h"
#include "../utils/trainer_helper.h"

double Grid::CalculateDistance(Position pos1, Position pos2) {
float Grid::CalculateDistance(Position pos1, Position pos2) {
return euclidean_distance(pos1, pos2);
}

int Grid::FindBMU(vector<float> input_vector) {
float min_dist = std::numeric_limits<float>::max();
int bmu = -1;
for (int i = 0; i < this->neurones.size(); ++i) {
float dist = squared_euclidean_distance(input_vector, neurones[i]);
if (dist < min_dist){
min_dist = dist;
bmu = i;
}
}
return bmu;
}
14 changes: 12 additions & 2 deletions src/grid/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Grid {
int height;
int dimention;
int no_of_neurones;
vector<vector<int>> neurones;
vector<vector<float>> neurones;

/**
* Constructor
Expand Down Expand Up @@ -46,13 +46,23 @@ class Grid {
*/
virtual Position GetPosition(int neurone_index) = 0;

/**
* Find the Best Matching Unit (BMU) for a given vector
* @param input_vector Input vector to find BMU
* @return Index of the neurone
*/
int FindBMU(vector<float> input_vector);


/**
* Get distance between two neurones
* @param neurone1 Index of the neurone 1
* @param neurone2 Index of the neurone 2
* @return Distance between two neurones
*/
static double CalculateDistance(Position pos1, Position pos2);
static float CalculateDistance(Position pos1, Position pos2);


};


Expand Down
10 changes: 2 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@ int main() {
trainer trainer;

// Load dataset
trainer.input_vector = load_csv(INPUT_PATH);
trainer.input_space = load_csv(INPUT_PATH);

// Create new grid
trainer.grid = new Rectangular(10, 10, 3);
trainer.grid = new Rectangular(3, 3, 3);

// Initialize random weights
initialize_random_weights(trainer.grid);


for (vector<int> neu : trainer.grid->neurones){
for (int w : neu){
cout << w << endl;
}
}

std::cout << "Program is running..." << std::endl;
return 0;
}
10 changes: 5 additions & 5 deletions src/utils/csv_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

using namespace std;

vector<vector<int>> load_csv(string filename){
vector<vector<float>> load_csv(string filename){
// Load file
ifstream data(filename);
string line;
vector<vector<int>> dataset;
vector<vector<float>> dataset;

// Read lines
while(std::getline(data,line))
{
stringstream line_stream(line);
string cell;
vector<int> parsed_row;
while(std::getline(line_stream, cell, ','))
vector<float> parsed_row;
while(getline(line_stream, cell, ','))
{
parsed_row.push_back(stoi(cell));
parsed_row.push_back(stof(cell));
}

dataset.push_back(parsed_row);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/csv_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
* @param filename Filename/Path of the CSV file
* @return Vector of the dataset
*/
std::vector<std::vector<int>> load_csv(std::string filename);
std::vector<std::vector<float>> load_csv(std::string filename);

#endif //SOM_CSV_PARSER_H
2 changes: 1 addition & 1 deletion src/utils/math_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

using namespace std;

double euclidean_distance(Position pos1, Position pos2) {
float euclidean_distance(Position pos1, Position pos2) {
return sqrt(pow(pos1.x - pos2.x, 2) + pow(pos1.y - pos2.y, 2));
}
2 changes: 1 addition & 1 deletion src/utils/math_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
* @param pos2 Position of neurone 2
* @return Euclidean distance between neurones
*/
double euclidean_distance(Position pos1, Position pos2);
float euclidean_distance(Position pos1, Position pos2);

#endif //SOM_MATH_HELPER_H
11 changes: 10 additions & 1 deletion src/utils/trainer_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
#include <ctime>
#include <cstdlib>
#include <vector>
#include <cmath>

#include <iostream>

void initialize_random_weights(Grid* grid, int min, int max){
srand(time(0));

for (int i = 0; i < grid->no_of_neurones; ++i){
std::vector<int> neu;
std::vector<float> neu;
for (int j = 0; j < grid->dimention; ++j){
neu.push_back(rand() % ((max - min) + 1) + min);
}
grid->neurones.push_back(neu);
}

}

float squared_euclidean_distance(vector<float> input_vector, vector<float> weight_vector){
float dist = 0;
for (int i = 0; i < weight_vector.size(); ++i) {
dist += pow((input_vector[i] - weight_vector[i]), 2);
}
return dist;
}
8 changes: 8 additions & 0 deletions src/utils/trainer_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ void initialize_random_weights(
int min = DEFAULT_MIN_WEIGHT,
int max = DEFAULT_MAX_WEIGHT);

/**
* Calculate the euclidean distance between two vectors
* @param input_vector Input vector
* @param weight_vector Weight vector
* @return Squared euclidean distance
*/
float squared_euclidean_distance(vector<float> input_vector, vector<float> weight_vector);

#endif //SOM_TRAINER_HELPER_H
19 changes: 18 additions & 1 deletion test/grid_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,25 @@ TEST(CalculateDistance, TwoPointers){
* Test Get Position of Rectangular Grid
*/
TEST(RectangularGrid, GetPosition){
Grid* grid = new Rectangular(10, 10);
Grid* grid = new Rectangular(10, 10, 3);
Position pos = grid->GetPosition(25);
ASSERT_EQ(pos.x, 5);
ASSERT_EQ(pos.y, 2);
}

/**
* Test Find BMU
*/
TEST(FindBMU, Manual){
Grid* grid = new Rectangular(2, 2, 3, false);

grid->neurones.push_back(vector<float>{10, 20, 30});
grid->neurones.push_back(vector<float>{-10, 2, 7});
grid->neurones.push_back(vector<float>{1, 2, 5});
grid->neurones.push_back(vector<float>{-5, -4, -3});

vector<float>vec = {3,4,5};
int bmu = grid->FindBMU(vec);

ASSERT_EQ(bmu, 2);
}

0 comments on commit 00a7e5b

Please sign in to comment.