-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
417 lines (341 loc) · 10.5 KB
/
matrix.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
#ifndef MATRIX_H
#define MATRIX_H
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<stdint.h>
#include<string.h>
#ifndef MATRIX_CALLOC
#include<stdlib.h>
#define MATRIX_CALLOC calloc
#endif
#ifndef MATRIX_ASSERT
#include<assert.h>
#define MATRIX_ASSERT assert
#endif
#define MATRIX_SHIFT(m,y,x) (m).elem[(y)*(m).cols + (x)]
#ifndef TYPE
#define TYPE float
#endif
#ifndef MATRIX_ACTIVATION_DEFAULT_DISABLE
#define MATRIX_ACTIVATION_SIGMOID
#endif
#ifndef T
#define T 1
#endif
typedef struct {
size_t rows; //NUM OF ROWS
size_t cols; //NUM OF COLS
size_t stride; //WHER TO SPLIT
TYPE *__restrict__ elem; //ELEMETS IN
} Mat;
//UTILITY FUNCTION
extern inline float rand_float(void);
extern inline float sigmoid(float x);
//MATRIX OPERATION
Mat matrix_alloc(size_t rows, size_t cols);
extern inline void matrix_free(Mat m);
extern inline void matrix_dot(Mat dest, Mat a, Mat b);
extern inline void matrix_sum(Mat dest, Mat a);
extern inline void matrix_dec(Mat dest, Mat a);
extern inline void matrix_mul_scalar(Mat dest, float scalar);
extern inline void matrix_add_scalar(Mat dest, float scalar);
extern inline void matrix_rand(Mat dest,float low, float high);
extern inline void matrix_activation(Mat m);
extern inline void matrix_activation_cols(Mat m,size_t num);
extern inline void matrix_feedforward(Mat *out,Mat input, Mat *weights, Mat *bias, size_t num_of_layer);
extern inline void matrix_reproduce(Mat a, Mat b);
extern inline void matrix_softmax(Mat m);
extern inline void matrix_copy(Mat dest, Mat src);
extern inline void matrix_convolution(Mat dest,Mat a, Mat kernel);
extern inline void matrix_print(Mat m,const char *name);
extern inline void matrix_print_out(Mat m, const char *name, size_t out);
#define MATRIX_PRINT(m) matrix_print((m), #m)
#ifndef LR
#define LR 0.6
#endif
#ifndef MUTATION_RATE
#define MUTATION_RATE 0.9
#endif
extern inline void matrix_mutation(Mat m);
extern inline void matrix_mutation_enable(Mat m, float DM);
extern inline void matrix_mutation_disable(Mat m,float EM);
#endif //MATRIX_H
#ifdef MATRIX_IMPLEMETATION
extern inline float rand_float(void) {
return (float)rand() / (float)RAND_MAX;
}
Mat matrix_alloc(size_t rows, size_t cols) {
Mat m;
m.cols = cols;
m.rows = rows;
m.elem = (TYPE *)MATRIX_CALLOC((rows)*(cols)+1,sizeof(*m.elem)); //(*M.elem) derefrence Changing type of element
MATRIX_ASSERT(m.elem!=NULL);
return m;
}
extern inline void matrix_free(Mat m) {
free(m.elem);
}
#define OPTIM_DOT
#ifndef OPTIM_DOT
extern inline void matrix_dot(Mat dest, Mat a, Mat b) { //a[1x2] b[2x3] c[1x3]
MATRIX_ASSERT(a.cols == b.rows);
MATRIX_ASSERT(dest.rows == a.rows);
MATRIX_ASSERT(dest.cols == b.cols);
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest,y,x) = 0;
for(size_t i = 0; i < a.cols; i++) {
MATRIX_SHIFT(dest,y,x) += MATRIX_SHIFT(a,y,i) * MATRIX_SHIFT(b,i,x);
}
}
}
}
#endif
#ifdef OPTIM_DOT
extern inline void matrix_dot(Mat dest, const Mat a, const Mat b) { //a[1x2] b[2x3] c[1x3]
MATRIX_ASSERT(a.cols == b.rows);
MATRIX_ASSERT(dest.rows == a.rows);
MATRIX_ASSERT(dest.cols == b.cols);
Mat b_T = matrix_alloc(b.cols, b.rows);
//Transpose matrix b
for(size_t y = 0; y < b_T.rows; y++) {
for(size_t x = 0; x < b_T.cols; x++) {
MATRIX_SHIFT(b_T, y, x) = MATRIX_SHIFT(b, x, y);
}
}
//Multiplay matrix with transposed
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest, y, x) = 0;
for(size_t i = 0; i < a.cols; i++) {
MATRIX_SHIFT(dest, y, x) += MATRIX_SHIFT(a, y, i) * MATRIX_SHIFT(b_T, x, i);
}
}
}
matrix_free(b_T);
}
#endif
extern inline void matrix_sum(Mat dest, Mat a) {
MATRIX_ASSERT(dest.cols == a.cols);
MATRIX_ASSERT(dest.rows == a.rows);
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest,y,x) += MATRIX_SHIFT(a,y,x);
}
}
}
extern inline void matrix_dec(Mat dest, Mat a) {
MATRIX_ASSERT(dest.cols == a.cols);
MATRIX_ASSERT(dest.rows == a.rows);
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest,y,x) -= MATRIX_SHIFT(a,y,x);
}
}
}
extern inline void matrix_add_scalar(Mat dest, float scalar) {
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest,y,x) += scalar;
}
}
}
extern inline void matrix_mul_scalar(Mat dest, float scalar) {
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest,y,x) *= scalar;
}
}
}
extern inline void matrix_rand(Mat dest,float low, float high) {
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest,y,x) = (TYPE)((rand_float()*(high - low)) + low); //IN RANGE
}
}
}
extern inline void matrix_print(Mat m, const char *name) {
//system("pause");
printf("\n\n______________________________________________________________________________\n");
printf("%s",name);
printf(" = \n[\n");
for(size_t y = 0; y < m.rows; y++) {
for(size_t x = 0; x < m.cols; x++) {
printf(" %.2f ", (float)MATRIX_SHIFT(m,y,x));
}
printf("\n\n");
}
printf("]\n");
printf("______________________________________________________________________________\n");
}
extern inline void matrix_print_out(Mat m, const char *name, size_t out) {
//system("pause");
printf("\n\n______________________________________________________________________________\n");
printf("%s",name);
printf(" = \n[\n");
for(size_t y = 0; y < m.rows; y++) {
for(size_t x = 0; x < m.cols; x++) {
printf(" %.2f ", (float)MATRIX_SHIFT(m,y,x));
if(x==out) {
break;
}
}
printf("\n\n");
}
printf("]\n");
printf("______________________________________________________________________________\n");
}
extern inline float sigmoid(float x) {
return 1.0f/(1 + expf(-x));
}
extern inline void matrix_activation_cols(Mat m,size_t num) {
for(size_t y = 0; y < m.rows; y++) {
MATRIX_SHIFT(m,y,num) = sigmoid(MATRIX_SHIFT(m,y,num));
}
}
extern inline void matrix_activation(Mat m) {
for(size_t y = 0; y < m.rows; y++)
for(size_t x = 0; x < m.cols; x++) {
#ifdef MATRIX_ACTIVATION_SIGMOID
MATRIX_SHIFT(m,y,x) = sigmoid(MATRIX_SHIFT(m,y,x));
#endif
#ifdef MATRIX_ACTIVATION_TANH
MATRIX_SHIFT(m,y,x) = tanh(MATRIX_SHIFT(m,y,x));
#endif
#ifdef MATRIX_ACTIVATION_RELU
if(MATRIX_SHIFT(m,y,x) < 0) MATRIX_SHIFT(m,y,x) = 0;
#endif
#ifdef MATRIX_ACTIVATION_LRELU
if(MATRIX_SHIFT(m,y,x) < 0) MATRIX_SHIFT(m,y,x) = -0.01*MATRIX_SHIFT(m,y,x);
#endif
#ifdef MATRIX_ACTIVATION_BINARY
if(MATRIX_SHIFT(m,y,x) > 0)
MATRIX_SHIFT(m,y,x) = 1.0f;
else
MATRIX_SHIFT(m,y,x) = 0 ;
#endif
#ifdef MATRIX_ACTIVATION_SWISH
MATRIX_SHIFT(m,y,x) *= sigmoid(MATRIX_SHIFT(m,y,x));
#endif
#ifdef MATRIX_ACTIVATION_RAND_VALUE
#ifdef MATRIX_ACTIVATION_RAND_RATE
if(rand_float() < MATRIX_ACTIVATION_RAND_RATE)
MATRIX_SHIFT(m,y,x) += 2*rand_float() * (float)MATRIX_ACTIVATION_RAND_VALUE - (float)MATRIX_ACTIVATION_RAND_VALUE;
#endif
#endif
}
}
//REWRITE IN ONE LOOP
extern inline void matrix_feedforward(Mat *out,Mat input, Mat *weights, Mat *bias,size_t num_of_layer) {
//system("pause");
matrix_dot(out[0],input,weights[0]);
matrix_sum(out[0],bias[0]);
matrix_activation(out[0]);
//system("pause");
for(size_t i = 1; i < num_of_layer; i++) {
//system("pause");
matrix_dot(out[i],out[i-1],weights[i]);
//system("pause");
matrix_sum(out[i],bias[i]);
//system("pause");
matrix_activation(out[i]);
}
}
//*/
extern inline void matrix_softmax(Mat m) {
double sum = 0.0f;
for(size_t y = 0; y < m.rows; y++) {
for(size_t x = 0; x < m.cols; x++) {
sum+=exp((double)MATRIX_SHIFT(m,y,x) / (double) T);
}
}
//printf("sum = %f",sum);
//system("pause");
for(size_t y = 0; y < m.rows; y++) {
for(size_t x = 0; x < m.cols; x++) {
MATRIX_SHIFT(m,y,x) = (double)exp((double)MATRIX_SHIFT(m,y,x) / (double)T)/(double)sum;
}
}
}
extern inline void matrix_copy(Mat dest, Mat src) {
//MATRIX_ASSERT();
for(size_t y = 0; y < dest.rows; y++) {
for(size_t x = 0; x < dest.cols; x++) {
MATRIX_SHIFT(dest,y,x) = MATRIX_SHIFT(src,y,x);
}
}
}
extern inline void matrix_convolution(Mat dest, Mat a, Mat kernel){
MATRIX_ASSERT(dest.cols <= a.cols);
MATRIX_ASSERT(dest.rows <= a.rows);
for (size_t y = 0; y < dest.cols; y++){
for (size_t x = 0; x < dest.rows; x++){
float sum = 0.0f;
for (size_t i = 0; i < kernel.rows; i++)
{
for (size_t j = 0; j < kernel.cols; j++)
{
sum+=MATRIX_SHIFT(a,y+i,x+j)*MATRIX_SHIFT(kernel,i,j);
}
}
MATRIX_SHIFT(dest,y, x) = sum;
}
}
}
extern inline void matrix_mutation(Mat m) {
for(size_t y = 0; y < m.rows; y++) {
for(size_t x = 0; x < m.cols; x++) {
if(rand_float() < MUTATION_RATE && MATRIX_SHIFT(m,y,x)!=0) {
if(rand()%2==0) {
MATRIX_SHIFT(m,y,x)+=LR*rand_float();
}
else {
MATRIX_SHIFT(m,y,x)-=LR*rand_float();
}
}
}
}
}
extern inline void matrix_mutation_disable(Mat m,float DM) {
for(size_t y = 0; y < m.rows; y++) {
for(size_t x = 0; x < m.cols; x++) {
if(rand_float() < MUTATION_RATE) {
if(rand_float() < DM) {
MATRIX_SHIFT(m,y,x) = 0.0f;
}
}
}
}
}
extern inline void matrix_mutation_enable(Mat m, float EM) {
for(size_t y = 0; y < m.rows; y++) {
for(size_t x = 0; x < m.cols; x++) {
if(rand_float() < MUTATION_RATE) {
if(rand_float() < EM && MATRIX_SHIFT(m,y,x) == 0) {
MATRIX_SHIFT(m,y,x) = rand_float()*2.0f - 1.0f;
}
}
}
}
}
extern inline void matrix_reproduce(Mat a, Mat b) {
MATRIX_ASSERT(a.rows == b.rows);
MATRIX_ASSERT(a.cols == b.cols);
for(size_t y = 0; y < a.rows; y++) {
for(size_t x = 0; x < a.cols; x++) {
int desi = rand()%2;
if(desi == 0) {
MATRIX_SHIFT(a,y,x) = MATRIX_SHIFT(b,y,x);
}
}
}
matrix_mutation(a);
#ifdef DISABLE_MUTATION
matrix_mutation_disable(a, DISABLE_MUTATION);
#endif
#ifdef ENABLE_MUTATION
matrix_mutation_enable(a, ENABLE_MUTATION);
#endif
}
#endif //MATRIX_IMPLEMENTATION