-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayer.cpp
executable file
·202 lines (149 loc) · 3.86 KB
/
Layer.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "Layer.h"
Layer::Layer()
{}
Layer::~Layer()
{
delete []neuron;
}
void Layer::setLayer(int numberNeuron, int numberSynapses, bool isOutput)
{
int i;
this->numberNeuron = numberNeuron;
neuron = new Neuron[numberNeuron];
cout << "Camada criada com "<< numberNeuron << " neuronios e " << numberSynapses << " Sinapses." << endl;
if(!isOutput)
{
for(i=0; i<numberNeuron; i++)
neuron[i].initializesWeightThreshold(numberSynapses);
}
}
int Layer::getNumberNeuron()
{
return (this->numberNeuron);
}
Neuron Layer::getNeuron(int indice)
{
return (this->neuron[indice]);
}
// Precisa ser pensado como trabalhar essa input
void Layer::presentsInput(float *datas, int numberImages, int numNeuronsInput)
{
int i, j=0;
int begin = numberImages*numNeuronsInput + numberImages;
//if(begin != 0)
// begin+=numberImages;
for (i = begin; i<(begin+numNeuronsInput); i++)
{
//cout << "image " << j << " - " << datas[i] << endl;
//adaptValues(datas[i]/255, j++);
//cout << "image " << j << " - " << datas[i] << endl;
//cout << datas[i]/255 << endl;
neuron[j++].setValue(datas[i]);
}
}
int Layer::getPositionOutput(float label)
{
int i;
cout << "\n\n" << label << endl;
if (label>=0 && label<0.1)
i=0;
else
if (label>=0.1 && label<0.2)
i=1;
else
if (label>=0.2 && label<0.3)
i=2;
else
if (label>=0.3 && label<0.4)
i=3;
else
if (label>=0.4 && label<0.5)
i=4;
else
if (label>=0.5 && label<0.6)
i=5;
else
if (label>=0.6 && label<0.7)
i=6;
else
if (label>=0.7 && label<0.8)
i=7;
else
if (label>=0.8 && label<0.9)
i=8;
else
i=9;
cout << "\n\n" << i << endl;
return i;
}
void Layer::presentDesiredOutputs(float label, int numberOutputs)
{
float *DesiredOutput = new float[numberOutputs];
int i, j = (int) label;
for (i=0; i<numberOutputs; i++)
DesiredOutput[i] = 0;
DesiredOutput[j] = 1;
for (i = 0; i<numberOutputs; i++)
neuron[i].setD(DesiredOutput[i]);
//cout << label << "Escolhido " << j << "-"<< neuron[i].getD << endl;
}
float Layer::calculatesV(int numberNeuron)
{
int i;
float value = 0;
//cout << "Numero de neuronios nessa camada: " << getNumberNeuron() << endl;
for (i=0; i<getNumberNeuron(); i++)
{
// cout << "Valor antes " << i << " : " << value << " "<< neuron[i].getValue() << "*" << neuron[i].getWeight(numberNeuron)<< " = " << neuron[i].getValue()*neuron[i].getWeight(numberNeuron) << endl;
value += neuron[i].getValue() * neuron[i].getWeight(numberNeuron);
// cout << "Valor depois " << i << " : " << value << " "<< neuron[i].getValue() << "*" << " = " << neuron[i].getValue()*neuron[i].getWeight(numberNeuron) << endl;
}
//cout << "Valor de " << numberNeuron << "w: " << neuron[0].getWeight(numberNeuron) << endl;
//cout << "Valor final " << numberNeuron << " : " << value << endl;
return value;
}
void Layer::adaptValues(float value, int index)
{
//float aux;
//cout << "Index: "<< index << endl;
//cout << "Value antes: " << value << endl;
value = (1.0)/(1.0 + exp(-value+neuron[index].getThreshold()));
//cout << "Value depois: " << value << endl;
neuron[index].setValue(value);
}
void Layer::getOutputs()
{
int i;
for (i=0; i<numberNeuron; i++)
cout << "Neuron [" << i << "]: " << neuron[i].getValue() << "." << endl;
}
float Layer::getNeuronOfWeight(int j, int k)
{
return (neuron[k].getWeight(j));
}
float Layer::getNeuronOfError(int k)
{
return (neuron[k].getError());
}
int Layer::getPositionGreaterValue()
{
int i, posMax=0;
float max = 0;
for (i=0; i<getNumberNeuron(); i++)
{
if (max < neuron[i].getValue())
{
max = neuron[i].getValue();
posMax = i;
cout << neuron[i].getValue() << " - I max: " << i << endl;
}
}
return posMax;
}
void Layer::back(bool isError, bool isOutput, float sum, int indice)
{
if (isError)
neuron[indice].calculatesError(isOutput, sum);
else
neuron[indice].adjustWeight();
}