include string include algorithm include random include perceptron inc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <string>
#include <algorithm>
#include <random>
#include "perceptron.h"
#include "tools.h"
#define INPUT_FILE "./datasets/2_4_0.txt"
#define OUTPUT_FILE "results.txt"
int main()
{
int train_percent = 80;
std::vector<Sample> samples;
tools::loadTxt_Sharp(INPUT_FILE, samples);
// normalization (if we need this)
double max = 0.0;
for (Sample& sampl : samples)
{
for (double& coord : sampl.point)
if (coord > max)
max = coord;
}
for (Sample& sampl : samples)
for (double& coord : sampl.point)
coord /= max;
auto engine = std::default_random_engine{};
std::shuffle(std::begin(samples), std::end(samples), engine);
std::vector<Sample> trainSet, testSet;
//for demonstration of overfitting
/*for (Sample sampl : samples)
{
if (sampl.point[0] > 0.0 && sampl.point[0] < 20.0 && sampl.point[1] > -10.0 && sampl.point[1] < 10.0)
testSet.push_back(sampl);
else
trainSet.push_back(sampl);
}
std::cout << "fsdf " << testSet[0].point[0] << " " << trainSet[0].point[1] << std::endl;*/
int ind_border = train_percent/100.0 * samples.size();
for (int i = 0; i < ind_border; ++i) trainSet.push_back(samples[i]);
for (int i = ind_border; i < samples.size(); ++i) testSet.push_back(samples[i]);
std::cout.width(2);
std::cout << "TOTAL NUMBER OF SAMPLES: " << samples.size() << " (100%)" << std::endl;
std::cout << "Number of samples for TRAIN: " << trainSet.size() << " (" << train_percent << "%)" << std::endl;
std::cout << "Number of samples for TEST: " << testSet.size() << " (" << 100 - train_percent << "%)" << std::endl;
std::vector<std::vector<double>> test_plot;
std::vector<std::vector<double>> train_plot;
std::vector<int> nNeurons;
nNeurons.push_back(10);
//nNeurons.push_back(15);
/* Perceptron(int nInputs, int nOutputs, int nLayers, std::vector<int>& nNeurons); */
Perceptron nn(2,4,1, nNeurons);
// startLearning(std::vector<Sample>& samples, int nEpochs, double learningSpeed, double quality);
nn.startLearning(trainSet, 1000, 0.1, 0.01, testSet, train_plot, test_plot, 50);
std::cout << "\ntest begins..." << std::endl;
nn.startTest(testSet);
tools::writePointsToTXT("train.txt", train_plot);
tools::writePointsToTXT("test.txt", test_plot);
std::vector<Sample> net_answers = samples;
for (Sample& samp : net_answers)
samp.classMark = 0;
nn.getAnswers(net_answers);
std::sort(net_answers.begin(), net_answers.end(), compareSamples());
tools::writeSamplesToTxt(OUTPUT_FILE, net_answers);
std::cout << "Done!" << std::endl;
return 0;
}