-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetwork.cpp
99 lines (82 loc) · 2.57 KB
/
NeuralNetwork.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "NeuralNetwork.h"
#include "Letter.h"
#include "Perceptron.h"
namespace irec
{
NeuralNetwork::NeuralNetwork(QObject *parent) : QObject(parent)
{
}
void NeuralNetwork::initialize(double epoch, double rate)
{
foreach (Letter letter, _letters)
{
Perceptron perceptron(36, 36, 0.2);
_perceptrons.append(perceptron);
}
for (int i = 0; i < _perceptrons.length(); ++i)
{
bool canFinsh = false;
int curEpoch = 0;
while (!canFinsh)
{
canFinsh = true;
// training to recognize the right value
if (!_perceptrons.at(i).recognize(_letters.at(i).image))
{
canFinsh = false;
while (!_perceptrons.at(i).recognize(_letters.at(i).image))
{
_perceptrons[i].learnRight(_letters.at(i).image, rate);
if (++curEpoch > epoch)
{
canFinsh = true;
break;
}
}
}
// training to not recognize wrong values
for (int j = 0; j < _perceptrons.length(); ++j)
{
if (i != j)
{
bool recognized = _perceptrons.at(i).recognize(_letters.at(j).image);
if (recognized)
{
canFinsh = false;
while (_perceptrons.at(i).recognize(_letters.at(j).image))
_perceptrons[i].learnWrong(_letters.at(j).image, rate);
}
}
}
if (++curEpoch > epoch)
break;
}
}
}
void NeuralNetwork::addLetter(const Letter &letter)
{
_letters.append(letter);
}
QString NeuralNetwork::recognizeLetter(const QList<QList<double> > &sample)
{
for (int i = 0; i < _perceptrons.length(); ++i)
{
if (_perceptrons.at(i).recognize(sample))
return _letters.at(i).name;
}
return "Unknown";
}
void NeuralNetwork::clear()
{
_perceptrons.clear();
_letters.clear();
}
QList<Letter> NeuralNetwork::letters() const
{
return _letters;
}
QList<Perceptron> NeuralNetwork::perceptrons() const
{
return _perceptrons;
}
}