-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNeuralNet.cpp
More file actions
325 lines (252 loc) · 9.44 KB
/
NeuralNet.cpp
File metadata and controls
325 lines (252 loc) · 9.44 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "NeuralNet.h"
//---------------------------------------------------------------Neuron---------------------------------------------------------------
Neuron::Neuron(unsigned numInputs)
{
for(unsigned i=0; i<numInputs+1; i++)
{
m_weights.push_back(randomValue());
}
}
double Neuron::randomValue()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dist(-1, 1);
return dist(gen);
}
double Neuron::operator* (const std::vector<double> & inputs)
{
double result = 0;
auto weight = m_weights.cbegin();
for(auto i = inputs.cbegin(); i != inputs.cend(); i++)
{
result += (*i) * (*weight);
weight++;
}
result += *weight;
result = sigmoid(result);
m_output = result;
return result;
}
double Neuron::sigmoid(double x) const
{
return 1.0 / (1.0 + std::exp(-x));
}
std::ostream& operator<< (std::ostream & out, const Neuron & neuron)
{
out << "[";
for(auto i = neuron.m_weights.cbegin(); i != neuron.m_weights.cend(); i++)
out << " " << *i;
out << " ]";
return out;
}
//----------------------------------------------------------------NeuralNet---------------------------------------------------------------
NeuralNet::NeuralNet(const std::vector<unsigned>& topology)
{
for(auto i = topology.cbegin()+1; i != topology.cend(); i++)
{
m_layers.push_back(Layer());
for(unsigned j=0; j < *i; j++)
{
m_layers.back().push_back(Neuron(*(i-1)));
}
}
}
std::vector<double> NeuralNet::propagate(const std::vector<double>& inputs)
{
if(inputs.size() != m_layers[0][0].m_weights.size()-1) throw std::invalid_argument("Wrong number of inputs!");
return *this * inputs;
}
std::vector<double> NeuralNet::operator* (const std::vector<double> & inputs)
{
std::vector<double> outputs = std::move(inputs);
std::vector<double> neuronOutput;
for(auto i = m_layers.begin(); i != m_layers.end(); i++)
{
for(auto j = (*i).begin(); j != (*i).end(); j++)
{
neuronOutput.push_back(*j * outputs);
}
outputs = std::move(neuronOutput);
}
return outputs;
}
void NeuralNet::backProp(const std::vector<TrainingExample> & examples, double tol, double alpha)
{
std::vector<double> Error;
do
{
Error.clear();
//calculate error for all examples
for(auto ex = examples.cbegin(); ex != examples.cend(); ex++)
{
std::vector<double> inputs = (*ex).m_inputs;
std::vector<double> outputs = propagate(inputs);
double error = 0;
auto target = (*ex).m_outputs.cbegin();
for(auto actual = outputs.cbegin(); actual != outputs.cend(); actual++)
{
error += 0.5 * std::pow((*target - *actual), 2);
target++;
}
Error.push_back(error);
//calculate deltas
std::vector<double> deltaWeight;
std::vector<double> delta;
std::vector<double> prevDelta;
target = (*ex).m_outputs.cbegin();
//for all layers
for(int layer = m_layers.size() - 1; layer >= 0; --layer)
{
prevDelta = std::move(delta);
//for all neurons in current layer
for(unsigned neuron = 0; neuron < m_layers[layer].size(); neuron++)
{
//for output layer
if(layer == (int)m_layers.size()-1)
{
double d = (m_layers[layer][neuron].m_output - *target) * m_layers[layer][neuron].m_output * (1 - m_layers[layer][neuron].m_output);
target++;
delta.push_back(d);
}
else //for hidden layer
{
double sum = 0.0;
int count = 0;
for(auto deltaIter = prevDelta.cbegin(); deltaIter != prevDelta.cend(); deltaIter++)
{
sum += *deltaIter * m_layers[layer+1][count].m_weights[neuron];
count++;
}
double d = sum * m_layers[layer][neuron].m_output * (1 - m_layers[layer][neuron].m_output);
delta.push_back(d);
}
//for every weight in current neuron
unsigned n = m_layers[layer][neuron].m_weights.size();
for(unsigned weight = 0; weight < n; weight++)
{
if(layer > 0)//not first hidden
{
double w = delta.back() * (weight < n-1 ? m_layers[layer - 1][weight].m_output : 1);
deltaWeight.push_back(w);
}
else //first hidden
{
double w = delta.back() * (weight < n-1 ? (*ex).m_inputs[weight] : 1);
deltaWeight.push_back(w);
}
}
}
}
//update weights
auto iter = deltaWeight.cbegin();
for(int layer = m_layers.size() - 1; layer >= 0; layer--)
for(unsigned neuron = 0; neuron < m_layers[layer].size(); neuron++)
for(unsigned weight = 0; weight < m_layers[layer][neuron].m_weights.size(); weight++)
m_layers[layer][neuron].m_weights[weight] -= alpha * *iter++;
}
}
while(std::accumulate(Error.cbegin(), Error.cend(), 0.0) / examples.size() > tol);
}
void NeuralNet::updateNNWeights(const std::vector<double> & new_weights)
{
if(new_weights.size() != size()) throw std::invalid_argument("Too few of weights!");
unsigned count = 0;
for(unsigned i = 0; i < m_layers.size(); i++)
{
for(auto j = m_layers[i].begin(); j != m_layers[i].end(); j++)
{
for(unsigned k=0; k < (*j).m_weights.size(); k++)
(*j).m_weights[k] = new_weights[count++];
}
}
}
unsigned NeuralNet::WeightsCount() const
{
return size();
}
unsigned NeuralNet::size() const
{
unsigned count = 0;
for(unsigned i = 0; i < m_layers.size(); i++)
{
for(auto j = m_layers[i].cbegin(); j != m_layers[i].cend(); j++)
{
count += (*j).m_weights.size();
}
}
return count;
}
void NeuralNet::serialize(const std::string & file_path, const NeuralNet & net)
{
std::ofstream out(file_path);
if(out.is_open())
{
std::vector<unsigned> topology;
topology.push_back(net.m_layers[0][0].m_weights.size()-1);
for(auto layer = net.m_layers.cbegin(); layer != net.m_layers.cend(); layer++)
{
topology.push_back((*layer).size());
for(auto neuron = (*layer).cbegin(); neuron != (*layer).cend(); neuron++)
{
for(auto weight = (*neuron).m_weights.cbegin(); weight != (*neuron).m_weights.cend(); weight++)
out << *weight << " ";
out << std::endl;
}
}
out << "[ ";
for(auto i = topology.cbegin(); i != topology.cend(); i++)
{
out << *i << " ";
}
out << "]" << std::endl;
}
else throw std::invalid_argument("Unable to open file!");
}
NeuralNet NeuralNet::deserialize(const std::string & file_path)
{
std::ifstream in(file_path);
if(in.is_open())
{
std::vector<unsigned> topology;
std::vector<double> weights;
std::vector<std::string> numbers;
while(in)
{
numbers.push_back("");
std::getline(in, numbers.back(), ' ');
}
bool ind = true;
for(auto i = numbers.cbegin(); i != numbers.cend(); i++)
{
if(*i == "\n["){
ind = false;
continue;
}
if(*i == "]\n") break;
if(ind)
weights.push_back(std::stod(*i));
else topology.push_back(std::stoul(*i));
}
NeuralNet net(topology);
net.updateNNWeights(weights);
return net;
}
else throw std::invalid_argument("Unable to open file!");
}
std::ostream& operator<< (std::ostream& out, const NeuralNet & net)
{
for(unsigned i=0; i < net.m_layers.size(); i++)
{
out << "########################Layer" << i << "########################" << std::endl;
for(auto j = net.m_layers[i].cbegin(); j != net.m_layers[i].cend(); j++)
out << *j << std::endl;
out << "######################################################" << std::endl;
}
return out;
}
//---------------------------------------------------------------TrainingExample---------------------------------------------------------------
TrainingExample::TrainingExample(std::vector<double> && inputs, std::vector<double> && outputs)
:m_inputs(inputs),
m_outputs(outputs)
{}