-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.cpp
358 lines (286 loc) · 10.4 KB
/
source.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
356
357
358
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define sizeofArr(arr) ((sizeof(arr))/ sizeof(arr[0]))
//Learning rate, lower is slower, [0.0, 1.0]
#define ETA 0.1f
//Momentum, lower is less, [0.0, n]
#define ALPHA 0.6f
struct Connection{
float weight;
float dweight;
};
struct Neuron{
Connection* connections;
int numConnections;
int index;
float output;
float gradient;
};
static float RandomWeight(){
return (rand() / (float) RAND_MAX);
}
//Hyperbolic Tangent range [-1.0, 1.0]
static float TransferFunction(float x){
return tanh(x);
}
//Hyperbolic Tangent derivative
static float TransferFunctionDerivative(float x){
//tanh derivative
return 1.0 - (tanh(x) * tanh(x));
}
void CreateNeuron(Neuron* neuron, int index, int numConnections){
neuron->numConnections = numConnections;
neuron->connections = (Connection*)malloc(sizeof(Connection) * numConnections);
for(int i = 0; i < numConnections; i++){
neuron->connections[i].weight = RandomWeight();
}
neuron->index = index;
}
float SumDOW(Neuron* neuron, Neuron* layer, int sizeOfLayer){
float sum = 0;
//Sum out contributions of the errors at the nodes we feed
for(int i = 0; i < sizeOfLayer; i++){
sum += neuron->connections[i].weight * layer[i].gradient;
}
return sum;
}
void NeuronFeedForward(Neuron* neuron, Neuron* previouslayer, int sizeOfPreviousLayer){
float sum = 0.0f;
//Sum previous layer's outputs
for(int i = 0; i < sizeOfPreviousLayer; i++){
sum += previouslayer[i].output * previouslayer[i].connections[neuron->index].weight;
}
neuron->output = TransferFunction(sum);
}
void CalcOutputGradients(Neuron* neuron, float target){
float delta = target - neuron->output;
neuron->gradient = delta * TransferFunctionDerivative(neuron->output);
}
void CalcHiddenGradients(Neuron* neuron, Neuron* nextLayer, int sizeOfNextLayer){
float dow = SumDOW(neuron, nextLayer, sizeOfNextLayer);
neuron->gradient = dow * TransferFunctionDerivative(neuron->output);
}
void UpdateInputWeights(Neuron* neuron, Neuron* previouslayer, int sizeOfPreviousLayer, float eta, float alpha){
for(int i = 0; i < sizeOfPreviousLayer; i++){
Neuron* prevNeuron = &previouslayer[i];
float oldDweight = prevNeuron->connections[neuron->index].dweight;
float newDweight =
//Individual input, magnified bythe gradient and train rate
eta * prevNeuron->output * neuron->gradient
//Also add momentum = a fraction of the previous delta weight
+ alpha * oldDweight;
prevNeuron->connections[neuron->index].dweight = newDweight;
prevNeuron->connections[neuron->index].weight += newDweight;
}
}
struct Net321{
Neuron inputLayer[4];
Neuron hiddenLayer0[3];
Neuron outputLayer[2];
float error;
float recentAverageError;
float recentAverageSmoothingError;
};
void CreateNet321(Net321* net){
for(int i = 0; i < sizeofArr(net->inputLayer); i++){
CreateNeuron(&(net->inputLayer[i]), i, sizeofArr(net->hiddenLayer0) - 1);
}
net->inputLayer[sizeofArr(net->inputLayer) - 1].output = 1.0f;
for(int i = 0; i < sizeofArr(net->hiddenLayer0); i++){
CreateNeuron(&(net->hiddenLayer0[i]), i, sizeofArr(net->outputLayer) - 1);
}
net->hiddenLayer0[sizeofArr(net->hiddenLayer0) - 1].output = 1.0f;
for(int i = 0; i < sizeofArr(net->outputLayer); i++){
CreateNeuron(&(net->outputLayer[i]), i, 0);
}
net->outputLayer[sizeofArr(net->outputLayer) - 1].output = 1.0f;
}
int GetNumWeights(Net321* net){
int result = 0;
result = (sizeofArr(net->inputLayer) - 1) * (sizeofArr(net->hiddenLayer0) - 1);
result += (sizeofArr(net->hiddenLayer0) - 1) * (sizeofArr(net->outputLayer) - 1);
return result;
}
void GetWeights(Net321* net, float* weights){
int i = 0;
for(i = 0; i < sizeofArr(net->inputLayer) - 1; i++){
for(int j = 0; j < sizeofArr(net->hiddenLayer0) - 1; j++){
weights[j + i * (sizeofArr(net->hiddenLayer0) - 1)] = net->inputLayer[i].connections[j].weight;
printf("[0,%d,%d] = %f\n", i, j, weights[j + i * (sizeofArr(net->hiddenLayer0) - 1)]);
}
}
for(int k = i + sizeofArr(net->hiddenLayer0), i = 0; i < sizeofArr(net->hiddenLayer0) - 1; i++){
for(int j = 0; j < sizeofArr(net->outputLayer) - 1; j++){
weights[j + k + i * (sizeofArr(net->outputLayer) - 1)] = net->hiddenLayer0[i].connections[j].weight;
printf("[1,%d,%d] = %f\n", i, j, weights[j + k + i * (sizeofArr(net->outputLayer) - 1)]);
}
}
}
void SetWeights(Net321* net, float* weights){
int i = 0;
for(i = 0; i < sizeofArr(net->inputLayer) - 1; i++){
for(int j = 0; j < sizeofArr(net->hiddenLayer0) - 1; j++){
net->inputLayer[i].connections[j].weight = weights[j + i * (sizeofArr(net->hiddenLayer0) - 1)];
printf("[0,%d,%d] = %f\n", i, j, net->inputLayer[i].connections[j].weight);
}
}
for(int k = i + sizeofArr(net->hiddenLayer0), i = 0; i < sizeofArr(net->hiddenLayer0) - 1; i++){
for(int j = 0; j < sizeofArr(net->outputLayer) - 1; j++){
net->hiddenLayer0[i].connections[j].weight = weights[j + k + i * (sizeofArr(net->outputLayer) - 1)];
printf("[1,%d,%d] = %f\n", i, j, net->inputLayer[i].connections[j].weight);
}
}
}
void FeedForward321(Net321* net, float* inputs, int sizeOfInputs){
//Latch the inputs values into the neurons
for(int i = 0; i < sizeOfInputs; i++){
net->inputLayer[i].output = inputs[i];
}
//Forward propagate
for(int i = 0; i < sizeofArr(net->hiddenLayer0) - 1; i++){
NeuronFeedForward(&(net->hiddenLayer0[i]), net->inputLayer, sizeofArr(net->inputLayer));
}
for(int i = 0; i < sizeofArr(net->outputLayer) - 1; i++){
NeuronFeedForward(&(net->outputLayer[i]), net->hiddenLayer0, sizeofArr(net->outputLayer));
}
}
void BackPropagation321(Net321* net, float* targets, int sizeOfTargets){
//Calculate overall net error (Root Mean Square Error of output neuron errors)
Neuron* outputLayer = net->outputLayer;
int outputLayerSize = sizeofArr(net->outputLayer) - 1;
net->error = 0.0f;
for(int i = 0; i < sizeOfTargets; i++){
float delta = targets[i] - outputLayer[i].output;
net->error += delta * delta;
}
net->error /= outputLayerSize; //Average
net->error = sqrtf(net->error); //RMS
//Implement a recent average measurement
net->recentAverageError = (net->recentAverageError * net->recentAverageSmoothingError + net->error)
/ (net->recentAverageSmoothingError + 1.0f);
//Calculate output layer gradients
for(int i = 0; i < sizeOfTargets; i++){
CalcOutputGradients(&outputLayer[i], targets[i]);
}
//Calculate gradients on hidden layers
Neuron* hiddenLayer0 = net->hiddenLayer0;
Neuron* nextLayer = net->outputLayer;
for(int i = 0; i < sizeofArr(net->hiddenLayer0) - 1; i++){
CalcHiddenGradients(&hiddenLayer0[i], nextLayer, sizeofArr(net->outputLayer) - 1);
}
//For all layers from outputs to first hidden layer, update connection weights
{
Neuron* layer = net->outputLayer;
Neuron* prevLayer = net->hiddenLayer0;
for(int i = 0; i < sizeofArr(net->outputLayer) - 1; i++){
UpdateInputWeights(&layer[i], prevLayer, sizeofArr(net->hiddenLayer0), ETA, ALPHA);
}
}
{
Neuron* layer = net->hiddenLayer0;
Neuron* prevLayer = net->inputLayer;
for(int i = 0; i < sizeofArr(net->hiddenLayer0) - 1; i++){
UpdateInputWeights(&layer[i], prevLayer, sizeofArr(net->inputLayer), ETA, ALPHA);
}
}
}
void GetResults321(Net321* net, float* results, int sizeOfResults){
for(int i = 0; i < sizeOfResults; i++){
results[i] = net->outputLayer[i].output;
}
}
void Solve321(Net321* net, float* inputs, int sizeOfInputs, float* results, int sizeOfResults){
FeedForward321(net, inputs, sizeOfInputs);
GetResults321(net, results, sizeOfResults);
}
bool EpsilonEquals(float a, float b, float epsilon){
return fabs(b - a) <= epsilon;
}
int main(int argc, char** argv){
srand(time(NULL));
Net321 net = {};
CreateNet321(&net);
#if 1
char* filename = "learningCase.lc";
FILE* learningCase = fopen(filename, "r");
if(learningCase){
int numCases = 0;
int numInputs = 0;
int numOutputs = 0;
fscanf(learningCase, "%d", &numCases);
fscanf(learningCase, "%d", &numInputs);
fscanf(learningCase, "%d", &numOutputs);
float inputList[numInputs * numCases];
float targetList[numOutputs * numCases];
float results[numOutputs];
for(int i = 0; i < numCases; i++){
for(int j = 0; j < numInputs; j++){
fscanf(learningCase, "%f", &inputList[j + i * numInputs]);
}
for(int j = 0; j < numOutputs; j++){
fscanf(learningCase, "%f", &targetList[j + i * numOutputs]);
}
}
fclose(learningCase);
float avg = 0.0f;
float lastValue = 0.0f;
int convergeTime = 0;
for(int i = 0; i < 200000; i++){
float sum = 0.0f;
for(int j = 0; j < numCases; j++){
float inputs[numInputs];
float targets[numOutputs];
for(int k = 0; k < numInputs; k++){
inputs[k] = inputList[k + j * numCases];
//printf("%f ", inputs[k]);
}
//printf("\n");
for(int k = 0; k < numOutputs; k++){
targets[k] = targetList[k + j * numCases];
//printf("%f\n", targets[k]);
}
FeedForward321(&net, inputs, sizeofArr(inputs));
BackPropagation321(&net, targets, sizeofArr(targets));
GetResults321(&net, results, sizeofArr(results));
lastValue = results[0];
sum += net.recentAverageError;
//printf("%f ||||| %f\n", net.recentAverageError, lastValue;
}
//printf("\n");
avg = sum / numCases;
convergeTime++;
//printf("|||%d|||\n", convergeTime);
if(avg <= 0.0001f){
break;
}
}
printf("Steps: %d Average: %f\n", convergeTime, avg);
float weights[GetNumWeights(&net)];
GetWeights(&net, weights);
char* filename = "storedWeights.wt";
FILE* storedWeights = fopen(filename, "w");
for(int i = 0; i < sizeofArr(weights); i++){
fprintf(storedWeights, "%f\n", weights[i]);
}
fclose(storedWeights);
}
#else
float weights[GetNumWeights(&net)];
char* filename = "storedWeights.wt";
FILE* storedWeights = fopen(filename, "r");
for(int i = 0; i < sizeofArr(weights); i++){
fscanf(storedWeights, "%f", &weights[i]);
}
SetWeights(&net, weights);
fclose(storedWeights);
float inputs[3];
float results[1];
for(int i = 0; i < sizeofArr(inputs); i++){
scanf("%f", &inputs[i]);
}
Solve321(&net, inputs, sizeofArr(inputs), results, sizeofArr(results));
printf("%f\n", results[0]);
#endif
}