-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.h
More file actions
286 lines (256 loc) · 8.89 KB
/
nn.h
File metadata and controls
286 lines (256 loc) · 8.89 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
#include "nn_utils.h"
#include "time.h"
#include <assert.h>
#ifndef NN_H
#define NN_H
#define MODEL_OUTPUT(m) (model_assert(m), (m).ls[(m).hidden_count - 1].ac)
#define MODEL_INPUT(m) (model_assert(m), (m).input)
typedef struct
{
size_t neuron_count;
Mat ws;
Mat gws;
Mat bs;
Mat gbs;
Mat ac;
Mat gac;
AF af;
WI wi;
} Layer;
typedef struct
{
size_t neuron_count;
AF af;
WI wi;
} CreateLayerInput;
Layer layer_alloc(size_t input_features, size_t neuron_count, AF af, WI wi);
typedef struct
{
size_t hidden_count; // size of hidden layers
Mat input; // wights matrix arrays
Mat output;
Layer *ls;
} Model;
Layer layer_alloc(size_t input_features, size_t neuron_count, AF af, WI wi)
{
assert(neuron_count > 0 && "layer muct contain at least one neuron");
assert(input_features > 0 && "at lest one input deafture is required");
Layer l;
l.neuron_count = neuron_count;
l.ws = mat_alloc(input_features, l.neuron_count);
l.gws = mat_alloc(input_features, l.neuron_count);
l.bs = mat_alloc(1, l.neuron_count);
l.gbs = mat_alloc(1, l.neuron_count);
l.ac = mat_alloc(1, l.neuron_count);
l.gac = mat_alloc(1, l.neuron_count);
l.af = af;
l.wi = wi;
return l;
}
Model create_model(size_t input_features, CreateLayerInput *arch, size_t hidden_layers_count);
void model_assert(Model m);
void model_init(Model m);
void model_print(Model m);
void model_grad_zero(Model m);
void model_forward(Model m, Mat input);
float model_cost(Model m, Mat input, Mat output);
void model_backprop(Model m, Mat input, Mat output);
void model_train(Model m, Mat input, Mat output, float lrate, size_t eps);
void model_test(Model m, Mat input, Mat output);
Model create_model(size_t input_features, CreateLayerInput *arch, size_t hidden_layers_count)
{
assert(input_features > 0 && "at least one input featrue is requred");
Model m;
m.hidden_count = hidden_layers_count;
m.input = mat_alloc(1, input_features);
m.ls = (Layer *)malloc(sizeof(*m.ls) * m.hidden_count);
assert(m.ls != NULL && "memory allocation failed for array hidden layers");
size_t ifts = input_features;
for (size_t i = 0; i < hidden_layers_count; i++)
{
m.ls[i] = layer_alloc(ifts, arch[i].neuron_count, arch[i].af, arch[i].wi);
ifts = arch[i].neuron_count;
}
model_init(m);
return m;
}
void model_assert(Model m)
{
mat_assert(m.input);
assert(m.ls != NULL && "model not initialized");
}
void model_print(Model m)
{
model_assert(m);
printf("model:=[");
for (size_t l = 0; l < m.hidden_count; l++)
{
printf("\n%*slayer [%d]:=[", 5, "", l);
MAT_PRINT(m.ls[l].ws, 10);
MAT_PRINT(m.ls[l].bs, 10);
printf("\n%*s]", 5, "");
}
printf("\n]");
}
void model_init(Model m)
{
srand(time(0));
srand(44);
for (size_t l = 0; l < m.hidden_count; l++)
{
for (int j = 0; j < m.ls[l].ws.rows; j++)
for (int k = 0; k < m.ls[l].ws.cols; k++)
MAT_AT(m.ls[l].ws, j, k) = weights_init(l == 0 ? m.input.cols : m.ls[l - 1].ac.cols, m.ls[l].ac.cols, m.ls[l].wi);
for (int j = 0; j < m.ls[l].bs.rows; j++)
for (int k = 0; k < m.ls[l].bs.cols; k++)
MAT_AT(m.ls[l].bs, j, k) = 0;
}
}
void model_grad_zero(Model m)
{
for (size_t l = 0; l < m.hidden_count; l++)
{
mat_fill(m.ls[l].gws, 0);
mat_fill(m.ls[l].gbs, 0);
mat_fill(m.ls[l].gac, 0);
}
}
void model_forward(Model m, Mat input)
{
model_assert(m);
mat_copy(m.input, input);
for (size_t l = 0; l < m.hidden_count; l++)
{
// if first layer then get action form input matrix else get activation form previous layer
l == 0 ? mat_dot(m.ls[l].ac, m.input, m.ls[l].ws) : mat_dot(m.ls[l].ac, m.ls[l - 1].ac, m.ls[l].ws);
mat_sum(m.ls[l].ac, m.ls[l].bs);
mat_act(m.ls[l].ac, m.ls[l].af);
}
}
float model_cost(Model m, Mat input, Mat output)
{
model_assert(m);
assert(input.rows == output.rows && "input rows and ouput rows are not equal");
assert(input.cols == MODEL_INPUT(m).cols && "input cols are not equal to input layer cols");
assert(output.cols == MODEL_OUTPUT(m).cols && "output cols are not equal output layer cols");
Mat in = mat_alloc(1, input.cols);
float cost = 0.f;
for (size_t tr = 0; tr < input.rows; tr++)
{
row_copy(in, input, tr);
model_forward(m, in);
float diff = 0.f;
for (size_t j = 0; j < MODEL_OUTPUT(m).cols; j++)
{
// diff = MAT_AT(nn.as[nn.count], 0, j) - MAT_AT(output, tr, j);
diff = MAT_AT(MODEL_OUTPUT(m), 0, j) - MAT_AT(output, tr, j);
cost += diff * diff;
}
}
cost /= input.rows;
return cost;
}
void model_backprop(Model m, Mat input, Mat output)
{
model_assert(m);
assert(input.rows == output.rows && "input rows and ouput rows are not equal");
assert(input.cols == MODEL_INPUT(m).cols && "input cols are not equal to input layer cols");
assert(output.cols == MODEL_OUTPUT(m).cols && "output cols are not equal output layer cols");
size_t n = input.rows;
model_grad_zero(m);
Mat in = mat_alloc(1, MODEL_INPUT(m).cols);
for (size_t tr = 0; tr < n; tr++)
{
row_copy(in, input, tr);
model_forward(m, in);
for (size_t l = 0; l < m.hidden_count; l++)
{
mat_fill(m.ls[l].gac, 0);
}
for (size_t j = 0; j < MODEL_OUTPUT(m).cols; j++)
{
MAT_AT(m.ls[m.hidden_count - 1].gac, 0, j) = 2 * (MAT_AT(m.ls[m.hidden_count - 1].ac, 0, j) - MAT_AT(output, tr, j));
}
for (int l = m.hidden_count - 1; l >= 0; l--)
{
for (size_t j = 0; j < m.ls[l].ac.cols; j++)
{
float da = MAT_AT(m.ls[l].gac, 0, j);
float a = MAT_AT(m.ls[l].ac, 0, j);
float qa = dact(a, m.ls[l].af);
MAT_AT(m.ls[l].gbs, 0, j) += da * qa;
int s = l == 0 ? m.input.cols : m.ls[l - 1].ac.cols;
for (int k = 0; k < s; k++)
{
float pa = l == 0 ? MAT_AT(m.input, 0, k) : MAT_AT(m.ls[l - 1].ac, 0, k);
float w = MAT_AT(m.ls[l].ws, k, j);
MAT_AT(m.ls[l].gws, k, j) += da * qa * pa;
if (l > 0)
MAT_AT(m.ls[l - 1].gac, 0, k) += da * qa * w;
}
}
}
}
for (size_t l = 0; l < m.hidden_count; l++)
{
for (size_t j = 0; j < m.ls[l].gws.rows; j++)
for (size_t k = 0; k < m.ls[l].gws.cols; k++)
MAT_AT(m.ls[l].gws, j, k) /= n;
for (size_t j = 0; j < m.ls[l].gbs.rows; j++)
for (size_t k = 0; k < m.ls[l].gbs.cols; k++)
MAT_AT(m.ls[l].gbs, j, k) /= n;
}
}
void model_train(Model m, Mat input, Mat output, float lrate, size_t eps)
{
model_assert(m);
assert(input.rows == output.rows && "input rows and ouput rows are not equal");
for (size_t ep = 0; ep < eps; ep++)
{
model_backprop(m, input, output);
for (size_t l = 0; l < m.hidden_count; l++)
{
for (size_t j = 0; j < m.ls[l].ws.rows; j++)
for (size_t k = 0; k < m.ls[l].ws.cols; k++)
MAT_AT(m.ls[l].ws, j, k) -= lrate * MAT_AT(m.ls[l].gws, j, k);
for (size_t j = 0; j < m.ls[l].bs.rows; j++)
for (size_t k = 0; k < m.ls[l].bs.cols; k++)
MAT_AT(m.ls[l].bs, j, k) -= lrate * MAT_AT(m.ls[l].gbs, j, k);
}
}
}
void model_test(Model m, Mat input, Mat output)
{
model_assert(m);
assert(input.rows == output.rows && "input rows and ouput rows are not equal");
assert(input.cols == MODEL_INPUT(m).cols && "input cols are not equal to input layer cols");
assert(output.cols == MODEL_OUTPUT(m).cols && "output cols are not equal output layer cols");
Mat in = mat_alloc(1, input.cols);
for (size_t tr = 0; tr < input.rows; tr++)
{
row_copy(in, input, tr);
model_forward(m, in);
printf("\nInput:=[");
for (size_t j = 0; j < input.cols; j++)
{
printf(" %f", MAT_AT(in, 0, j));
}
printf("]");
printf("Output:=[");
for (size_t j = 0; j < MODEL_OUTPUT(m).cols; j++)
{
printf(" %f", MAT_AT(MODEL_OUTPUT(m), 0, j));
}
printf("]");
printf("expected:=[");
for (size_t j = 0; j < output.cols; j++)
{
printf(" %f", MAT_AT(output, tr, j));
}
printf("]");
}
}
#endif