-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron.cpp
More file actions
144 lines (133 loc) · 5.07 KB
/
perceptron.cpp
File metadata and controls
144 lines (133 loc) · 5.07 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "perceptron.h"
#include <QColor>
Perceptron::Perceptron(int in, int nHidden, int nH_Neurons, int out)
{
// создание S слоя со связями S-A
for (int j = 0; j < in; ++j)
this->sensors << Sensor(nH_Neurons);
// жуть 0...
// this->layers << Layer(SensorType,in,nH_Neurons);
for(int i = 0; i < nHidden - 1; ++i)
this->layers << Layer(AssociativeType,nH_Neurons,nH_Neurons);
this->layers << Layer(AssociativeType,nH_Neurons,out);
// this->layers << Layer(ResultingType,out,0);
// создание R слоя
for (int j = 0; j < out; ++j)
this->resultings << Resulting(0);
}
Perceptron::~Perceptron(){}
void Perceptron::train(const QList<Pattern> trSet)
{
foreach (Pattern pat, trSet) {
// предъявление образа
this->recognize(pat.img);
// вычисление реакций нейронов A слоя
this->calcReactionA();
//
int actual;
do {
// вычисление реакций нейронов R слоя
this->calcReactionR();
//
actual = this->argMaxR();
// правила Хебба
if(pat.type != actual){
this->overestimateAR(pat.type,1); // pat.type - actual?
this->overestimateAR(actual,-1);
}
}while(pat.type != actual);
}
}
void Perceptron::overestimateAR(int rIdx, int value)
{
foreach (Neuron* n, this->layers.back().neurons) {
if(n->thresholdFunc() == 1)
{
int wht = n->getLinks()[rIdx];
dynamic_cast<Associative*>(n)->setLinkWeight(rIdx, wht + value);
}
}
}
// жуть 1...
void Perceptron::calcReactionA()
{
// обнуление значений нейронов A слоя
foreach (Neuron *aNeuro, this->layers.first().neurons) {
aNeuro->setValue(0);
}
// проход по всем сенсорам
foreach (Sensor sNeuro, this->sensors) {
// проход по всем соответствующим нейрону выходам в A слой
for(int j = 0; j < sNeuro.getLinks().size(); j++)
// вычисление значения на входе нейрона A
this->layers.first().neurons[j]->addValue(sNeuro.getValue() * sNeuro.getLinks()[j]);
}
//
for(int i = 1; i < this->layers.size(); ++i)
{
// обнуление значений нейронов текущего слоя
layers[i].setValue(0);
// проход по всем нейронам предыдущего слоя
foreach (Neuron* n, this->layers.at(i-1).neurons) {
// проход по всем соответствующим нейрону выходам в A слой
for(int j = 0; j < n->getLinks().size(); j++)
// вычисление значения на входе нейрона A
layers[i].neurons[j]->addValue(n->thresholdFunc() * n->getLinks()[j]);
}
}
}
void Perceptron::calcReactionR()
{
// обнуление значений нейронов R слоя
for(int i = 0; i < this->resultings.size(); i++)
this->resultings[i].setValue(0);
// проход по всем нейронам A слоя
foreach(Neuron *aNeuro, this->layers.back().neurons) {
// проход по всем соответствующим нейрону выходам в R слой
for(int j = 0; j < aNeuro->getLinks().size(); j++)
// вычисление значения на входе нейрона R
this->resultings[j].addValue(aNeuro->thresholdFunc() * aNeuro->getLinks()[j]);
}
}
int Perceptron::argMaxR() const
{
int max_idx = 0; // индекс нейрона-"победителя"
// поиск max отклика
for(int i = 1; i < this->resultings.size(); ++i)
if(resultings[i].getValue() > resultings[max_idx].getValue())
max_idx = i;
return max_idx;
}
int Perceptron::classify(const QImage &image)
{
this->recognize(image);
//
this->calcReactionA();
//
this->calcReactionR();
//
return this->argMaxR();
}
void Perceptron::recognize(const QImage &image)
{
const int cm = image.width();
const int cn = image.height();
// Инициализация сенсоров S
for(int i = 0; i < cn; i++)
{
// вычисление значения строкового сенсора
this->sensors[i].setValue(0);
for(int j = 0; j < cm; j++)
if(QColor(image.pixel(j, i)) == QColor(Qt::black))
this->sensors[i].incValue();
}
for(int i = cn; i < cn + cm; i++)
{
// вычисление значения столбцового сенсора
this->sensors[i].setValue(0);
for (int j = 0; j < cn; j++)
if(QColor(image.pixel(i - cn, j)) == QColor(Qt::black))
this->sensors[i].incValue();
}
}
Pattern::Pattern(const QImage &image, int type) : img(image), type(type){}