-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneuralnetwork.c
419 lines (373 loc) · 12 KB
/
neuralnetwork.c
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "matrix.h"
#include "neuralnetwork.h"
#include "image.h"
#include <assert.h>
NeuralNetwork *nn_init(int *layerSizes, int layerCount)
{
NeuralNetwork *nn = malloc(sizeof(NeuralNetwork));
nn->layerCount = layerCount;
nn->layerSizes = layerSizes;
// Allocate the size of the Matrix arrays. One matrix per layer.
nn->biaises = malloc(sizeof(Matrix *) * layerCount);
nn->weights = malloc(sizeof(Matrix *) * layerCount);
nn->activations = malloc(sizeof(Matrix *) * layerCount);
nn->z = malloc(sizeof(Matrix *) * layerCount);
nn->errors = malloc(sizeof(Matrix *) * layerCount);
nn->dBiaises = malloc(sizeof(Matrix *) * layerCount);
nn->dWeights = malloc(sizeof(Matrix *) * layerCount);
// Allocate the size of a Matrix for the expected result of the last layer.
nn->y = m_init(layerSizes[layerCount - 1], 1);
// Go through all layers to initialize the matrices.
for (int l = 0; l < layerCount; l++)
{
nn->activations[l] = m_init(layerSizes[l], 1);
if (l != 0) // Initializing biaises, weights and errors for the first layer is useless
{
nn->biaises[l] = m_init(layerSizes[l], 1);
nn->dBiaises[l] = m_init(layerSizes[l], 1);
nn->dWeights[l] = m_init(layerSizes[l], layerSizes[l - 1]);
nn->z[l] = m_init(layerSizes[l], 1);
nn->errors[l] = m_init(layerSizes[l], 1);
nn->weights[l] = m_init(layerSizes[l], layerSizes[l - 1]);
}
else
{
nn->biaises[l] = NULL;
nn->dBiaises[l] = NULL;
nn->z[l] = NULL;
nn->errors[l] = NULL;
nn->weights[l] = NULL;
}
}
return nn;
}
void nn_delete(NeuralNetwork *nn)
{
for (int l = 0; l < nn->layerCount; l++)
{
// Delete all matrices of the lists
m_delete(nn->activations[l]);
if (l != 0)
{
m_delete(nn->biaises[l]);
m_delete(nn->dBiaises[l]);
m_delete(nn->z[l]);
m_delete(nn->errors[l]);
m_delete(nn->weights[l]);
}
}
// Delete all lists
free(nn->biaises);
free(nn->dBiaises);
free(nn->weights);
free(nn->activations);
free(nn->z);
free(nn->errors);
free(nn->y);
free(nn); // Delete the struct
}
/**
* Sets all parameters randomly using a Gaussian distribution
*/
void nn_setupRandom(NeuralNetwork *nn)
{
// Initialize each biais and weights with random values
for (int l = 1; l < nn->layerCount; l++)
{
for (int i = 0; i < nn->biaises[l]->height; i++)
{
nn->biaises[l]->content[i][0] = GaussianRand();
for (int j = 0; j < nn->weights[l]->width; j++)
{
nn->weights[l]->content[i][j] = GaussianRand();
}
}
}
}
/**
* Sets the values of the first layer from an image to process it later.
*/
void nn_initFirstLayer(NeuralNetwork *nn, double *pixels)
{
// Set each neuron's value according to the pixels
for (int j = 0; j < nn->activations[0]->height; j++)
{
nn->activations[0]->content[j][0] = pixels[j];
}
}
/**
* Process an image.
*/
void nn_compute(NeuralNetwork *nn, double *pixels)
{
/*
params:
nn: Pointer to the Neural Network
pixels: Pointer to the array of doubles representing the values of image's pixels
action:
Init the first layer (nn_initFirstLayer)
Set matrix y
Compute the activations on each layer (nn_feedForward)
*/
nn_initFirstLayer(nn, pixels);
nn_feedForward(nn);
}
char nn_getResult(NeuralNetwork *nn)
{
/* Return the output of the network */
// Get the maximum activation
int max_index = 0;
double max_value = 0;
Matrix *result = nn->activations[nn->layerCount - 1];
for (int k = 0; k < result->height; k++)
{
if (result->content[k][0] > max_value)
{
max_value = result->content[k][0];
max_index = k;
}
}
// Cast the index of the neuron to the corresponding character
return max_index + 33;
}
double nn_getCost(NeuralNetwork *nn)
{
// Compute a - y
Matrix *sub = m_sub(nn->activations[nn->layerCount - 1], nn->y);
// Compute (a - y)^2
Matrix *hadamard = m_hadamard(sub, sub);
// Compute the sum
double cost = m_sum(hadamard);
m_delete(sub);
m_delete(hadamard);
return cost;
}
void nn_feedForward(NeuralNetwork *nn)
{
// Compute the activation on each layer
for (int l = 1; l < nn->layerCount; l++)
{
// Free z and activation
m_delete(nn->z[l]);
m_delete(nn->activations[l]);
// Store the product m*a in a temporary matrix
Matrix *product = m_mult(nn->weights[l], nn->activations[l - 1]);
nn->z[l] = m_add(product, nn->biaises[l]);
if (l == nn->layerCount - 1 && 0)
{
// Compute a softmax for the last layer
// Softmax formula : Aj = exp(Zj) / Sum_over_k(exp(Zk))
Matrix *exp = m_exp(nn->z[l]);
double sum = m_sum(exp);
nn->activations[l] = m_div(exp, sum);
//printf("%lf\n", m_sum(nn->activations[l]));
m_delete(exp);
}
else
{
nn->activations[l] = m_sigmoid(nn->z[l]);
}
// Free the temporary matrix
m_delete(product);
}
}
void nn_backProp(NeuralNetwork *nn, char label, int apply, int count)
{
assert(label >= 33);
m_delete(nn->y);
// Set the matrix of expected results
nn->y = m_init(nn->activations[nn->layerCount - 1]->height, 1);
nn->y->content[label - 33][0] = 1.0;
// Compute the errors and each layer, then compute the gradient's component
// Free previous errors
for (int l = 1; l < nn->layerCount; l++)
{
m_delete(nn->errors[l]);
// m_delete(nn->dWeights[l]);
}
// Compute the error on the output layer
Matrix *sum = m_sub(nn->activations[nn->layerCount - 1], nn->y);
Matrix *costPrime = m_mult_num(sum, 1.0 / count);
Matrix *zSigmoidPrime = m_sigmoid_prime(nn->z[nn->layerCount - 1]);
// Matrix *zSigmoidPrime = m_softmax_prime(nn->z[nn->layerCount - 1]);
nn->errors[nn->layerCount - 1] = m_hadamard(costPrime, zSigmoidPrime);
// Free temporary matrices
m_delete(costPrime);
m_delete(zSigmoidPrime);
m_delete(sum);
for (int l = nn->layerCount - 2; l > 0; l--)
{
// Compute intermediate matrices
Matrix *transposed = m_transpose(nn->weights[l + 1]);
Matrix *product = m_mult(transposed, nn->errors[l + 1]);
m_delete(transposed);
zSigmoidPrime = m_sigmoid_prime(nn->z[l]);
// Compute the error
nn->errors[l] = m_hadamard(product, zSigmoidPrime);
m_delete(product);
m_delete(zSigmoidPrime);
}
// Compute the gradient's components
for (int l = 1; l < nn->layerCount; l++)
{
// nn->dWeights[l] = m_init(nn->weights[l]->height, nn->weights[l]->width);
Matrix *oldDBiaises = nn->dBiaises[l];
nn->dBiaises[l] = m_add(nn->dBiaises[l], nn->errors[l]);
m_delete(oldDBiaises);
for (int j = 0; j < nn->weights[l]->height; j++)
{
for (int i = 0; i < nn->weights[l]->width; i++)
{
// dC/dW = activation(previous layer) * error(current layer
nn->dWeights[l]->content[j][i] += nn->activations[l - 1]->content[i][0] * nn->errors[l]->content[j][0];
}
}
}
if (apply)
{
// m_print(nn->dBiaises[1]);
// Apply modifiers
for (int l = 1; l < nn->layerCount; l++)
{
Matrix *oldBiaises = nn->biaises[l];
Matrix *oldWeights = nn->weights[l];
// Apply learning rate
Matrix *biaisDelta = m_mult_num(nn->dBiaises[l], 0.25);
Matrix *weightsDelta = m_mult_num(nn->dWeights[l], 0.25);
nn->biaises[l] = m_sub(nn->biaises[l], biaisDelta);
nn->weights[l] = m_sub(nn->weights[l], weightsDelta);
m_delete(biaisDelta);
m_delete(weightsDelta);
m_delete(oldBiaises);
m_delete(oldWeights);
m_delete(nn->dBiaises[l]);
m_delete(nn->dWeights[l]);
nn->dBiaises[l] = m_init(nn->layerSizes[l], 1);
nn->dWeights[l] = m_init(nn->layerSizes[l], nn->layerSizes[l - 1]);
}
}
}
double GaussianRand()
{
double r1 = 1.0 - (double)rand() / RAND_MAX;
double r2 = 1.0 - (double)rand() / RAND_MAX;
double randStdNormal = sqrt(-2.0 * log(r1)) * sin(2.0 * M_PI * r2);
return randStdNormal;
}
void nn_saveBinary(NeuralNetwork *nn, char *filepath)
{
printf("Saving in binary mode...\n");
FILE *file = fopen(filepath, "wb");
if (file == NULL)
{
printf("Error while saving the network\n");
exit(1);
}
// Write number of layer
fwrite(&nn->layerCount, sizeof(int), 1, file);
// Write layer sizes
for (int i = 0; i < nn->layerCount; i++)
{
fwrite(&nn->layerSizes[i], sizeof(int), 1, file);
}
// Write biaises
for (int l = 1; l < nn->layerCount; l++)
{
for (int n = 0; n < nn->activations[l]->height; n++)
{
fwrite(&nn->biaises[l]->content[n][0], sizeof(double), 1, file);
}
}
// Write weights
for (int l = 1; l < nn->layerCount; l++)
{
for (int y = 0; y < nn->weights[l]->height; y++)
{
for (int x = 0; x < nn->weights[l]->width; x++)
{
fwrite(&nn->weights[l]->content[y][x], sizeof(double), 1, file);
}
// fwrite(&nn->weights[l]->content[y], sizeof(double)*nn->weights[l]->width, 1, file);
}
}
printf("Neural network saved at : %s\n", filepath);
fclose(file);
return;
}
/**
* Loads a saved neural network from the filepath.
*/
NeuralNetwork *nn_load(char *filepath)
{
printf("Loading the network...\n");
FILE *file = fopen(filepath, "rb");
if (file == NULL)
{
printf("Error while loading the network\n");
exit(1);
}
// Reading layer count
int layerCount;
fread(&layerCount, sizeof(int), 1, file);
int *layerSizes = malloc(sizeof(int) * layerCount);
fread(layerSizes, sizeof(int) * layerCount, 1, file);
printf("Creating the neural network...\n");
NeuralNetwork *nn = nn_init(layerSizes, layerCount);
for (int l = 1; l < layerCount; l++)
{
for (int y = 0; y < nn->biaises[l]->height; y++)
{
fread(nn->biaises[l]->content[y], sizeof(double), 1, file);
}
}
for (int l = 1; l < layerCount; l++)
{
for (int y = 0; y < nn->weights[l]->height; y++)
{
fread(nn->weights[l]->content[y], sizeof(double), nn->weights[l]->width, file);
}
}
printf("Neural network loaded !\n");
fclose(file);
return nn;
}
/**
* Train a neural network from a set of images.
*/
void train(NeuralNetwork *nn, Img **images, int images_count, int cycles, int count)
{
/*
Train the neural network with the given set of images
*/
if (cycles == 0)
return;
printf("Training...\n");
fputs("\e[?25l", stdout); /* hide the cursor */
double sum = 0;
double accuracy = 0;
for (int i = 0; i < cycles * count; i++)
{
if (i % 1000 == 0)
{
// Stop training when accuracy is 100 or
double tmp = (sum / 1000) * 100;
// if (tmp < accuracy || tmp == 100)
// break;
accuracy = tmp;
sum = 0;
}
printf("\r%d / %d, accuracy = %lf", i + 1, cycles * count, accuracy);
unsigned int index = rand() % images_count;
Img *img = images[index];
// print_image(img);
// printf("%c %d\n", img->label, img->label);
nn_compute(nn, img->pixels);
nn_backProp(nn, (int)img->label, i % count == 0, count);
sum += (nn_getResult(nn) == img->label) ? 1.0 : 0.0;
}
fputs("\e[?25h", stdout); /* show the cursor */
printf("\n");
}