Skip to content

Commit 86a7bf8

Browse files
committed
Implement classify_perceptrons_BPG()
1 parent 6cd7a1a commit 86a7bf8

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

Algorithm.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ void Algorithm::classify_perceptrons_MSE(Eigen::MatrixXd weights) {
284284
}
285285
}
286286

287+
/* Taken from https://stackoverflow.com/questions/2704521/generate-random-double-numbers-in-c */
287288
double fRand(double fMin, double fMax)
288289
{
289290
double f = (double)rand() / RAND_MAX;
@@ -365,13 +366,42 @@ void Algorithm::train_perceptrons_BPG(Eigen::MatrixXd &weights) {
365366
}
366367

367368
void Algorithm::classify_perceptrons_BPG(Eigen::MatrixXd weights) {
368-
369+
std::cout << "\t -> Classifying..." << std::endl;
370+
371+
/* Build the testing elements matrix */
372+
int n = 0;
373+
Eigen::MatrixXd testing_elements_matrix(input_data->getVectorSize(), input_data->getTestingElements().size());
374+
for (auto const &testing_element : input_data->getTestingElements()) {
375+
testing_elements_matrix.col(n) = testing_element.data;
376+
n++;
377+
}
378+
379+
/* Augment the data */
380+
Eigen::MatrixXd augmented_test_elements(testing_elements_matrix);
381+
augmented_test_elements.conservativeResize(augmented_test_elements.rows() + 1, Eigen::NoChange);
382+
augmented_test_elements.row(augmented_test_elements.rows() - 1).setZero();
383+
384+
/* Compute classification */
385+
Eigen::MatrixXd computed_elements(weights.transpose() * augmented_test_elements);
386+
n = 0;
387+
for (auto const &training_class : input_data->getTrainingElements()) {
388+
for (int i = 0; i < computed_elements.cols(); i++) {
389+
if (computed_elements.row(n)(i) > 0)
390+
input_data->getTestingElements().at(i).given_class = training_class.first;
391+
392+
}
393+
n++;
394+
}
369395
}
370396

371397
void Algorithm::perceptronBPG() {
372398
std::cout << "* Running a neural network of perceptrons using Back-Propagation..." << std::endl;
373399

374400
Eigen::MatrixXd weights;
401+
train_perceptrons_BPG(weights);
402+
classify_perceptrons_BPG(weights);
403+
404+
std::cout << std::endl << "* Done ! => Accuracy: " << calculateAccuracy() * 100 << "%" << std::endl << std::endl;
375405
}
376406

377407

Main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ int main(int argc, char **argv) {
3636
algoA.nearestSubClassCentroid(3);
3737
algoA.nearestSubClassCentroid(5);
3838
algoA.threadedNearestNeighbour();*/
39-
algoA.perceptronMSE();
39+
//algoA.perceptronMSE();
40+
algoA.perceptronBPG();
4041
/*
4142
int ch = std::cin.get();
4243
@@ -66,7 +67,8 @@ int main(int argc, char **argv) {
6667
algoB.nearestSubClassCentroid(3);
6768
algoB.nearestSubClassCentroid(5);
6869
algoB.threadedNearestNeighbour();*/
69-
algoB.perceptronMSE();
70+
//algoB.perceptronMSE();
71+
algoB.perceptronBPG();
7072

7173

7274
return 0;

0 commit comments

Comments
 (0)