-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
355 lines (267 loc) · 7.47 KB
/
main.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// code is graciously reconstructed from http://www.ai-junkie.com/ann/evolved/nnt1.html
#include <iostream>
#include <vector>
#include <random>
#include <time.h>
#include <math.h> //abs
using namespace std;
//-------------------------------------used for the neural network
static int iNumInputs = 3;
static int iNumHidden = 2;
static int iNeuronsPerHiddenLayer = 3;
static int iNumOutputs = 3;
//for tweeking the sigmoid function
static double dActivationResponse = 1;
//bias value
static double dBias = 1;
//-------------------------------------used for the neural network
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
struct SNeuron
{
//the number of inputs into the neuron
int m_NumInputs;
//the weights for each input
vector<double> m_vecWeight;
//constructor
SNeuron(int NumInputs);
};
SNeuron::SNeuron(int NumInputs): m_NumInputs(NumInputs+1)
{
//we need an additional weight for the bias hence the +1
for (int i=0; i<NumInputs+1; ++i)
{
double temp = fRand( -1.0, 1.0);
//set up the weights with an initial random value
m_vecWeight.push_back(temp);
//cout << endl << i << ": " << temp << endl;
}
}
struct SNeuronLayer
{
//the number of neurons in this layer
int m_NumNeurons;
//the layer of neurons
vector<SNeuron> m_vecNeurons;
SNeuronLayer(int NumNeurons, int NumOnputsPerNeuron);
};
SNeuronLayer::SNeuronLayer(int NumNeurons,
int NumInputsPerNeuron): m_NumNeurons(NumNeurons)
{
for (int i=0; i<NumNeurons; ++i)
m_vecNeurons.push_back(SNeuron(NumInputsPerNeuron));
}
class CNeuralNet
{
private:
int m_NumInputs;
int m_NumOutputs;
int m_NumHiddenLayers;
int m_NeuronsPerHiddenLyr;
//storage for each layer of neurons including the output layer
vector<SNeuronLayer> m_vecLayers;
public:
CNeuralNet();
//have a guess... ;
void CreateNet();
//gets weights from the NN
vector<double> GetWeights()const;
//returns the total number of weights in the net
int GetNumberOfWeights()const;
//replaces the weights with new ones
void PutWeights(vector<double> &weights);
//calculates the outputs from a set of inputs
vector<double> Update(vector<double> &inputs);
//sigmoid response curve
inline double Sigmoid(double activation, double response);
};
//************************ methods forCNeuralNet ************************
//------------------------------default ctor ----------------------------
//
// creates a ANN based on the default values in params.ini
//-----------------------------------------------------------------------
CNeuralNet::CNeuralNet()
{
m_NumInputs = iNumInputs;
m_NumOutputs = iNumOutputs;
m_NumHiddenLayers = iNumHidden;
m_NeuronsPerHiddenLyr = iNeuronsPerHiddenLayer;
CreateNet();
}
//------------------------------createNet()------------------------------
//
// this method builds the ANN. The weights are all initially set to
// random values -1 < w < 1
//------------------------------------------------------------------------
void CNeuralNet::CreateNet()
{
//create the layers of the network
if (m_NumHiddenLayers > 0)
{
//create first hidden layer
m_vecLayers.push_back(SNeuronLayer(m_NeuronsPerHiddenLyr, m_NumInputs));
for (int i=0; i<m_NumHiddenLayers-1; ++i)
{
m_vecLayers.push_back(SNeuronLayer(m_NeuronsPerHiddenLyr,
m_NeuronsPerHiddenLyr));
}
//create output layer
m_vecLayers.push_back(SNeuronLayer(m_NumOutputs, m_NeuronsPerHiddenLyr));
}
else
{
//create output layer
m_vecLayers.push_back(SNeuronLayer(m_NumOutputs, m_NumInputs));
}
}
vector<double> CNeuralNet::GetWeights() const
{
//this will hold the weights
vector<double> weights;
//for each layer
for (int i=0; i<m_NumHiddenLayers + 1; ++i)
{
//for each neuron
for (int j=0; j<m_vecLayers[i].m_NumNeurons; ++j)
{
//for each weight
for (int k=0; k<m_vecLayers[i].m_vecNeurons[j].m_NumInputs; ++k)
{
weights.push_back(m_vecLayers[i].m_vecNeurons[j].m_vecWeight[k]);
}
}
}
return weights;
}
void CNeuralNet::PutWeights(vector<double> &weights)
{
int cWeight = 0;
//for each layer
for (int i=0; i<m_NumHiddenLayers + 1; ++i)
{
//for each neuron
for (int j=0; j<m_vecLayers[i].m_NumNeurons; ++j)
{
//for each weight
for (int k=0; k<m_vecLayers[i].m_vecNeurons[j].m_NumInputs; ++k)
{
m_vecLayers[i].m_vecNeurons[j].m_vecWeight[k] = weights[cWeight++];
}
}
}
return;
}
int CNeuralNet::GetNumberOfWeights() const
{
int weights = 0;
//for each layer
for (int i=0; i<m_NumHiddenLayers + 1; ++i)
{
//for each neuron
for (int j=0; j<m_vecLayers[i].m_NumNeurons; ++j)
{
//for each weight
for (int k=0; k<m_vecLayers[i].m_vecNeurons[j].m_NumInputs; ++k)
weights++;
}
}
return weights;
}
double CNeuralNet::Sigmoid(double netinput, double response)
{
return (netinput / (1+(abs(netinput))));
//return ( 1 / ( 1 + exp(-netinput / response)));
}
vector<double> CNeuralNet::Update(vector<double> &inputs)
{
//stores the resultant outputs from each layer
vector<double> outputs;
int cWeight = 0;
//first check that we have the correct amount of inputs
if (inputs.size() != m_NumInputs)
{
//just return an empty vector if incorrect.
return outputs;
}
//For each layer....
for (int i=0; i<m_NumHiddenLayers + 1; ++i)
{
if ( i > 0 )
{
inputs = outputs;
}
outputs.clear();
cWeight = 0;
//for each neuron sum the (inputs * corresponding weights).Throw
//the total at our sigmoid function to get the output.
for (int j=0; j<m_vecLayers[i].m_NumNeurons; ++j)
{
double netinput = 0;
int NumInputs = m_vecLayers[i].m_vecNeurons[j].m_NumInputs;
//for each weight
for (int k=0; k<NumInputs - 1; ++k)
{
//sum the weights x inputs
netinput += m_vecLayers[i].m_vecNeurons[j].m_vecWeight[k] *
inputs[cWeight++];
}
//add in the bias
netinput += m_vecLayers[i].m_vecNeurons[j].m_vecWeight[NumInputs-1] *
dBias;
//we can store the outputs from each layer as we generate them.
//The combined activation is first filtered through the sigmoid
//function
outputs.push_back(Sigmoid(netinput,
dActivationResponse));
cWeight = 0;
}
}
return outputs;
}
int main()
{
srand(time(NULL));
/*
for (int i=0; i<10;++i)
{
double temp = fRand(-1.0, 1.0);
cout << "number: " << temp << endl;
}
*/
CNeuralNet * testNet = new CNeuralNet;
vector<double> inputs;
vector<double> outputs;
inputs.push_back(1);
inputs.push_back(0);
inputs.push_back(1);
outputs = testNet->Update(inputs);
cout << endl << "outputs: " << endl;
for (int i = 0; i< outputs.size(); i++)
{
cout << endl << outputs[i] << endl;
}
inputs.clear();
inputs.push_back(0);
inputs.push_back(1);
inputs.push_back(0);
outputs = testNet->Update(inputs);
cout << endl << "outputs: " << endl;
for (int i = 0; i< outputs.size(); i++)
{
cout << endl << outputs[i] << endl;
}
inputs.clear();
inputs.push_back(1);
inputs.push_back(0);
inputs.push_back(1);
outputs = testNet->Update(inputs);
cout << endl << "outputs: " << endl;
for (int i = 0; i< outputs.size(); i++)
{
cout << endl << outputs[i] << endl;
}
return 0;
}