-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuralnetwork.cpp
158 lines (137 loc) · 4.29 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
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
#include "neuralnetwork.h"
#include <iostream>
NeuralNetwork::NeuralNetwork(int layers[],int numLayersInNetwork,int numInputByNeuron)
{
numLayers = numLayersInNetwork;
typeActivation currentTyp = RELU;
std::cout << "nb size layers" << numLayers << std::endl;
for (int currentLayer = 0 ; currentLayer < numLayers ; currentLayer++ )
{
for(int n = 0; n < layers[currentLayer] ; n++)
{
int numInputs;
if(currentLayer == 0)
{
numInputs = numInputByNeuron;
}
else
{
numInputs = layers[currentLayer - 1];
}
currentTyp = RELU;
if(currentLayer == numLayers-1)
{
//currentTyp = SIGMOID;
}
neuron* newNeuron = new neuron(currentLayer,n,currentTyp);
newNeuron->defineWeight(numInputs); // define number of weight for the neurone
network.push_back(newNeuron);
}
}
}
void NeuralNetwork::deleteNetwork()
{
while(network.size() != 0)
{
delete network[0];
network.erase(network.begin(),network.begin()+1);
}
}
void NeuralNetwork::trainNetwork(std::vector<float> errors,float learningRate)
{
initDelta(errors);
changeWeights(learningRate);
}
void NeuralNetwork::initDelta(std::vector<float> errors)
{
bool finnish;
for(int i = 0;i < (int)errors.size();i++) // delta de la dernière couche
{
finnish = false;
for( auto n = network.end()-1;n != network.begin() && !finnish; --n)
{
if((*n)->layerID == numLayers-1 && (*n)->neuronIdLayer == i) // si neurone voulu de la dernière couche
{
(*n)->delta = errors[i];
finnish = true;
}
}
}
int currentLayer = numLayers - 2 ;
// calcule gradient des couches cachés
for(int i = network.size()-1; i> -1 ; i--)
{
if(network[i]->layerID == currentLayer)
{
// fait la somme des gradients de la couche supérieur
float sumGradient = 0;
for(int j = i+1; j < (int)network.size(); j++ )
{
if(network[j]->layerID == currentLayer+1)
{
sumGradient+=network[j]->delta * network[j]->getWeightWithIndex(network[i]->neuronIdLayer) ;
//sumGradient+=network[j]->delta;
}
}
network[i]->delta = sumGradient;
}
}
}
void NeuralNetwork::changeWeights(float leaningRate)
{
for(int i = 0; i < (int)network.size(); i++)
{
network[i]->learn(leaningRate);
}
}
std::vector<float> NeuralNetwork::propagation(std::vector<std::vector<float>> inputs)
{
for(neuron* n : network) // vide les inputs et outputs déjà existant
{
n->emptyInputsOutput();
}
int cpt = 0 ;
std::list<float> lastLayer;
std::list<float> newLayer;
for(auto it= inputs.begin(); it != inputs.end(); ++it)
{
network[cpt]->setInputs(*it);
network[cpt]->propagate();
lastLayer.push_back(network[cpt]->output);
if(network[cpt]->layerID != 0)
{
std::cerr << "Erreur propagationNetwork code 1 la valeur est : "
<< network[cpt]->layerID
<< std::endl;
}
cpt++;
}
int layerId = 1;
while(cpt < (int)network.size())
{
if(network[cpt]->layerID != layerId)
{
if(layerId > network[cpt]->layerID)
{
std::cerr << "Erreur propagation code 2" << std::endl;
} // try if error where neuron is not order
layerId = network[cpt]->layerID;
lastLayer = newLayer;
newLayer.clear();
}
std::vector<float> result;
for(auto it = lastLayer.begin();it != lastLayer.end();++it)
{
result.push_back(*it);
}
network[cpt]->setInputs(result);
newLayer.push_back(network[cpt]->propagate()); // add neuron in the newLayer which be the lastLayer
cpt++;
}
std::vector<float> resultLastLayer; // get output of last layer
for(auto it = newLayer.begin();it != newLayer.end();++it)
{
resultLastLayer.push_back(*it);
}
return resultLastLayer;
}