-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.h
555 lines (509 loc) · 13.6 KB
/
nn.h
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#ifndef NN_H_
#define NN_H_
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRINT_T(m) print_tensor(m, #m)
#define VALUE_AT(t, i, j) (t).es[(i) * (t).cols + (j)]
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define RELU_PARAM 0.01f
typedef struct
{
/*define a matrix
* with arbitary size
*/
size_t rows;
size_t cols;
float *es; // pointer to the memory that hold the float(values)
} Tensor;
typedef enum
{
SIGMOID,
RELU,
NONE,
} Activation;
typedef struct
{
size_t net_size;
Tensor *weights; // array of weights for the network
Tensor *biases; // array of biases for the network
Tensor *grad_weights;
Tensor *grad_biases;
Tensor *activations; // outputs from each layer
Tensor *grad_activations;
Activation *activation_funs;
} Net;
Tensor new_tensor(size_t, size_t);
float float_rand(void);
float _sigmoid(float val);
float _relu(float val);
Tensor mse(Tensor y_true, Tensor y_pred);
// Tensor initializations
void rand_tensor(Tensor src, int min, int max);
void zeros_tensor(Tensor src);
void ones_tensor(Tensor src);
void set_tensor(Tensor src, int rows, int cols, float (*val)[cols]);
void copy_tensor(Tensor dst, Tensor src);
void normalize_tensor(Tensor tensor);
Tensor get_row(Tensor src, size_t row, size_t cols);
Tensor *get_batch(Tensor x, Tensor y, size_t idx,size_t n_inputs, size_t n_outputs, size_t batch_size);
// Tensor operation
void matmul(Tensor dst, Tensor a, Tensor b);
void matadd(Tensor dst, Tensor src);
void matdiff(Tensor dst, Tensor src);
void threshold(Tensor src, float T);
// derivatives of activation function
float d_act_fun(float val, Activation act_fun);
void zero_grad(Net nn);
// Basic nn
Net fully_connected_layer(size_t *l_sizes, size_t count,
Activation *layer_activations);
Tensor _forward(Net nn, Tensor input);
void _backward(Net nn, Tensor target, Tensor pred);
void _update(Net nn, float lr, size_t batch);
void normalize(Tensor grad, size_t batch);
void grad_diff(Tensor param, Tensor grad, float lr);
void print_tensor(Tensor t, char *name);
void print_network(Net nn, char *name);
void free_neural_net(Net *);
#endif // NN_H_
#ifdef NN_IMPLEMENTATION
float float_rand(void) { return (float)rand() / (float)RAND_MAX; }
Tensor new_tensor(size_t rows, size_t cols)
{
// allocate an object of type Tensor on the stack
Tensor m;
m.rows = rows;
m.cols = cols;
// calloc so values will be initialized to zero by default
m.es = calloc(rows * cols, sizeof(*m.es));
assert(m.es != NULL);
return m;
}
void rand_tensor(Tensor m, int min, int max)
{
for (size_t i = 0; i < m.rows; ++i)
{
for (size_t j = 0; j < m.cols; ++j)
{
VALUE_AT(m, i, j) = float_rand() * (max - min) + min;
}
}
return;
}
void zeros_tensor(Tensor m)
{
for (size_t i = 0; i < m.rows; ++i)
{
for (size_t j = 0; j < m.cols; ++j)
{
VALUE_AT(m, i, j) = 0.0f;
}
}
return;
}
void ones_tensor(Tensor m)
{
for (size_t i = 0; i < m.rows; ++i)
{
for (size_t j = 0; j < m.cols; ++j)
{
VALUE_AT(m, i, j) = 1.0;
}
}
return;
}
void set_tensor(Tensor m, int rows, int cols, float (*val)[cols])
{
// set the value
for (size_t i = 0; i < m.rows; ++i)
{
for (size_t j = 0; j < m.cols; ++j)
{
VALUE_AT(m, i, j) = val[i][j];
}
}
}
void copy_tensor(Tensor dst, Tensor src)
{
assert(dst.rows == src.rows);
assert(dst.cols == src.cols);
for (size_t i = 0; i < dst.rows; ++i)
{
for (size_t j = 0; j < dst.cols; ++j)
{
VALUE_AT(dst, i, j) = VALUE_AT(src, i, j);
}
}
}
Tensor get_row(Tensor src, size_t row, size_t cols)
{
assert(row < src.rows);
Tensor ROW = new_tensor(1, cols);
for (size_t i = 0; i < cols; ++i)
{
VALUE_AT(ROW, 0, i) = VALUE_AT(src, row, i);
}
return ROW;
}
Tensor* get_batch(Tensor x, Tensor y, size_t idx,size_t n_inputs, size_t n_outputs, size_t batch_size)
{
assert(x.rows == y.rows);
static size_t index = 0;
// assert if x.rows == y.rows
Tensor x_batch = new_tensor(batch_size, x.cols);
Tensor y_batch = new_tensor(batch_size, y.cols);
for (size_t i = 0; i < batch_size; ++i)
{
for (size_t j = 0; j < n_inputs; ++j)
{
VALUE_AT(x_batch, i, j) = VALUE_AT(x, i + index + idx, j);
}
for (size_t k = 0; k < n_outputs; ++k)
{
VALUE_AT(y_batch, i, k) = VALUE_AT(y, i + index + idx, k);
}
}
index += batch_size;
// reset when at the end
if (index + idx >= x.rows)
index = 0;
Tensor batch_tensor[] = {x_batch, y_batch};
return batch_tensor;
}
void normalize_tensor(Tensor tensor)
{
float max_value = 1e-6; // Initialize to minimum float value
// Find the maximum value in the array
for (size_t i = 0; i < tensor.rows; ++i)
{
for (size_t j = 0; j < tensor.cols; ++j)
{
if (VALUE_AT(tensor, i, j) > max_value)
max_value = VALUE_AT(tensor, i, j);
}
}
// Normalize each element
for (size_t i = 0; i < tensor.rows; ++i)
{
for (size_t j = 0; j < tensor.cols; ++j)
{
VALUE_AT(tensor, i, j) /= max_value;
}
}
}
void matmul(Tensor dst, Tensor a, Tensor b)
{
/* Tensor multiplication between to matrices
* Args:
* a(Tensor): firs matrice
* b(Tensor): second matrice
* a.cols == b.rows
* Returns:
* Tensor
* */
assert(a.cols == b.rows);
assert(dst.rows == a.rows);
assert(dst.cols == b.cols);
// multiply ith row of matrice a with jth col of matrice b
for (size_t i = 0; i < dst.rows; ++i)
{
for (size_t j = 0; j < dst.cols; ++j)
{
VALUE_AT(dst, i, j) = 0;
for (size_t k = 0; k < a.cols; ++k)
{
VALUE_AT(dst, i, j) += VALUE_AT(a, i, k) * VALUE_AT(b, k, j);
}
}
}
}
void matadd(Tensor dst, Tensor src)
{
/* Elementwise summation of two matrices*/
assert(dst.rows == src.rows);
assert(dst.cols == src.cols);
for (size_t i = 0; i < dst.rows; ++i)
{
for (size_t j = 0; j < dst.cols; ++j)
{
VALUE_AT(dst, i, j) += VALUE_AT(src, i, j);
}
}
}
void matdiff(Tensor dst, Tensor src)
{
/* Elementwise summation of two matrices*/
assert(dst.rows == src.rows);
assert(dst.cols == src.cols);
for (size_t i = 0; i < dst.rows; ++i)
{
for (size_t j = 0; j < dst.cols; ++j)
{
VALUE_AT(dst, i, j) -= VALUE_AT(src, i, j);
}
}
}
/*Operations*/
float _sigmoid(float x)
{
return 1 / (1 + expf(-x));
}
float _relu(float x)
{
return x > 0 ? x : x * RELU_PARAM;
}
float activate(float val, Activation activation_type)
{
switch (activation_type)
{
case SIGMOID:
return _sigmoid(val);
break;
case RELU:
return _relu(val);
case NONE:
return val;
break;
}
}
float d_act_fun(float val, Activation act_fun)
{
switch (act_fun)
{
case SIGMOID:
return val * (1 - val);
break;
case RELU:
return val >= 0 ? 1 : RELU_PARAM;
break;
case NONE:
return 1.0f;
}
return 0;
}
void threshold(Tensor src, float T)
{
for (size_t i = 0; i < src.rows; ++i)
{
for (size_t j = 0; j < src.cols; ++j)
{
VALUE_AT(src, i, j) = VALUE_AT(src, i, j) > T ? 1. : 0.;
}
}
}
Tensor mse(Tensor y_true, Tensor y_pred)
{
Tensor cost_t = new_tensor(y_true.rows, y_true.cols);
for (size_t i = 0; i < y_true.rows; ++i)
{
for (size_t j = 0; j < y_true.cols; ++j)
{
VALUE_AT(cost_t, i, j) =
pow((VALUE_AT(y_pred, i, j) - VALUE_AT(y_true, i, j)), 2);
}
}
return cost_t;
}
void zero_grad(Net nn)
{
for (size_t i = 0; i < nn.net_size; ++i)
{
zeros_tensor(nn.grad_activations[0]);
zeros_tensor(nn.grad_weights[i]);
zeros_tensor(nn.grad_biases[i]);
}
zeros_tensor(nn.grad_activations[nn.net_size]);
}
Net fully_connected_layer(size_t *l_sizes, size_t count,
Activation *layer_activations)
{
size_t hidden_layer_count = count - 1;
// initialize
Net nn = {.net_size = hidden_layer_count,
.weights = calloc(hidden_layer_count, sizeof(Tensor)),
.biases = calloc(hidden_layer_count, sizeof(Tensor)),
.grad_weights = calloc(hidden_layer_count, sizeof(Tensor)),
.grad_biases = calloc(hidden_layer_count, sizeof(Tensor)),
.activations = calloc(count, sizeof(Tensor)),
.grad_activations = calloc(count, sizeof(Tensor)),
.activation_funs = layer_activations};
/*initialize each tensors in weights & biases*/
// initialize the input activation, it will have a row 1, and col of
// l_sizes[0]
nn.activations[0] = new_tensor(1, l_sizes[0]);
nn.grad_activations[0] = new_tensor(1, l_sizes[0]);
// initialize weights
for (size_t i = 0; i < hidden_layer_count; ++i)
{
// weights
nn.weights[i] = new_tensor(nn.activations[i].cols, l_sizes[i + 1]);
nn.biases[i] = new_tensor(1, l_sizes[i + 1]);
// randomize weights and biases
rand_tensor(nn.weights[i], -1, 1);
rand_tensor(nn.biases[i], -1, 1);
nn.grad_weights[i] = new_tensor(nn.activations[i].cols, l_sizes[i + 1]);
nn.grad_biases[i] = new_tensor(1, l_sizes[i + 1]);
// zero grads
zeros_tensor(nn.grad_weights[i]);
zeros_tensor(nn.grad_biases[i]);
// next_activation
nn.activations[i + 1] = new_tensor(1, l_sizes[i + 1]);
nn.grad_activations[i + 1] = new_tensor(1, l_sizes[i + 1]);
}
return nn;
}
Tensor _forward(Net nn, Tensor input)
{
// Perform matmul between input and weight
// add bias
// activation function
// set the initial activation to the value fo the input tenosr
// free already alocated tensor memory
// free(nn.activations[0].es);
// copy input tensor to nn input
copy_tensor(nn.activations[0], input);
for (size_t l = 0; l < nn.net_size; ++l)
{
matmul(nn.activations[l + 1], nn.activations[l], nn.weights[l]);
matadd(nn.activations[l + 1], nn.biases[l]);
// activate each layers activation with an the layers activation functioin
size_t rows = nn.activations[l + 1].rows;
size_t cols = nn.activations[l + 1].cols;
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < cols; ++j)
{
float val = VALUE_AT(nn.activations[l + 1], i, j);
VALUE_AT(nn.activations[l + 1], i, j) = activate(val, nn.activation_funs[l]);
}
}
}
return nn.activations[nn.net_size]; // last activation is output
}
void _backward(Net nn, Tensor target,Tensor pred)
{
for (size_t l = 0; l <= nn.net_size; ++l)
{
zeros_tensor(nn.grad_activations[l]);
}
for (size_t j = 0; j < target.cols; ++j)
{
//VALUE_AT(nn.grad_activations[nn.net_size], 0, j) = 2 * (VALUE_AT(nn.activations[nn.net_size], 0, j) - VALUE_AT(target, 0, j));
VALUE_AT(nn.grad_activations[nn.net_size], 0, j) = 2 * (VALUE_AT(pred,0,j) - VALUE_AT(target, 0, j));
}
for (size_t l = nn.net_size; l > 0; --l)
{
for (size_t i = 0; i < nn.activations[l].cols; ++i)
{
float _act = VALUE_AT(nn.activations[l], 0, i);
float _e = VALUE_AT(nn.grad_activations[l], 0, i);
float _d_act = d_act_fun(_act, nn.activation_funs[l - 1]);
// gradient of the bias
VALUE_AT(nn.grad_biases[l - 1], 0, i) += 2 * _e * _d_act;
for (size_t k = 0; k < nn.activations[l - 1].cols; ++k)
{
float _prev_act = VALUE_AT(nn.activations[l - 1], 0, k);
// printf("_prev_act = %f\n", _prev_act);
float _w = VALUE_AT(nn.weights[l - 1], k, i);
// calculate the gradients
VALUE_AT(nn.grad_weights[l - 1], k, i) += 2 * _e * _d_act * _prev_act;
VALUE_AT(nn.grad_activations[l - 1], 0, k) += 2 * _e * _d_act * _w;
}
}
}
}
void _update(Net nn, float lr, size_t batch)
{
for (size_t n = 0; n < nn.net_size; ++n)
{
normalize(nn.grad_weights[n], batch);
normalize(nn.grad_biases[n], batch);
// update the weights
grad_diff(nn.weights[n], nn.grad_weights[n], lr);
grad_diff(nn.biases[n], nn.grad_biases[n], lr);
}
}
void normalize(Tensor grad, size_t batch)
{
for (size_t i = 0; i < grad.rows; ++i)
{
for (size_t j = 0; j < grad.cols; ++j)
{
VALUE_AT(grad, i, j) /= batch;
}
}
}
void grad_diff(Tensor param, Tensor grad, float lr)
{
assert(param.rows == grad.rows);
assert(param.cols == grad.cols);
for (size_t i = 0; i < param.rows; ++i)
{
for (size_t j = 0; j < param.cols; ++j)
{
VALUE_AT(param, i, j) -= lr * VALUE_AT(grad, i, j);
}
}
}
void free_neural_net(Net *nn)
{
free(nn->activations[nn->net_size].es);
free(nn->grad_activations[nn->net_size].es);
for (size_t i = 0; i < nn->net_size; ++i)
{
free(nn->weights[i].es);
free(nn->biases[i].es);
free(nn->grad_weights[i].es);
free(nn->grad_biases[i].es);
free(nn->activations[i].es);
free(nn->grad_activations[i].es);
}
free(nn->weights);
free(nn->biases);
free(nn->grad_weights);
free(nn->grad_biases);
free(nn->activations);
free(nn->grad_activations);
}
void print_tensor(Tensor m, char *name)
{
printf("%s = [\n", name);
for (size_t i = 0; i < m.rows; ++i)
{
printf("\t[");
for (size_t j = 0; j < m.cols; j++)
{
printf("%f,", VALUE_AT(m, i, j));
}
printf("],\n");
}
printf("]\n");
return;
}
void print_network(Net nn, char *name)
{
printf("Network Summary for network %s.... \n", name);
char buf[256];
print_tensor(nn.activations[0], "Input");
for (size_t i = 0; i < nn.net_size; ++i)
{
sprintf(buf, "w_%lu", i);
print_tensor(nn.weights[i], buf);
sprintf(buf, "gw_%lu", i);
print_tensor(nn.grad_weights[i], buf);
sprintf(buf, "b_%lu", i);
print_tensor(nn.biases[i], buf);
sprintf(buf, "gb_%lu", i);
print_tensor(nn.grad_biases[i], buf);
sprintf(buf, "a_%lu", i + 1);
print_tensor(nn.activations[i + 1], buf);
sprintf(buf, "ga_%lu", i + 1);
print_tensor(nn.grad_activations[i + 1], buf);
}
}
#endif // NN_IMPLEMENTATION