-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.c
More file actions
243 lines (204 loc) · 5.89 KB
/
Copy pathmodel.c
File metadata and controls
243 lines (204 loc) · 5.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
#include "model.h"
#include "activation.h"
#include "dense.h"
#include "dropout.h"
#include "layer.h"
#include "matrix.h"
#include "optimizer.h"
#include "util.h"
#include <float.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
Model* model_init()
{
Model* n = malloc(sizeof(Model));
n->last = n->first = NULL;
n->layer_count = 0;
n->batch_size = 0;
return n;
}
void model_free(Model* p)
{
if(!p) return;
Layer* current = p->last;
while(current)
{
Layer* prev = current->prev;
current->free(current);
current = prev;
}
free(p);
}
void model_insert_layer(Model* m, Layer* l)
{
++m->layer_count;
if(!m->first)
{
m->first = m->last = l;
m->first->next = m->first->prev = NULL;
return;
}
m->last->next = l;
l->prev = m->last;
l->next = NULL;
m->last = m->last->next;
}
void model_add_dense(Model* m, size_t neuron_count, Activation* activation, cell_t dropout_probability)
{
DenseLayer* dl = dense_init(NULL, NULL, neuron_count, activation, dropout_probability);
model_insert_layer(m, (Layer*)dl);
}
void model_add_dropout(Model* m, cell_t dropout_probability)
{
Dropout* d = dropout_init(NULL, NULL, dropout_probability);
model_insert_layer(m, (Layer*)d);
}
void model_print_info(Model* m, FILE* stream)
{
fprintf(stream, "A model with %zu layers.\n\n", m->layer_count);
size_t i = 0;
size_t total_params = 0;
Layer* current = m->first;
while(current)
{
total_params += current->print_info(current, stdout);
current = current->next;
++i;
}
fprintf(stream, "Total parameters: %zu\n\n", total_params);
}
void model_compile(Model* m, Optimizer* optimizer, lossfunc_t loss_func, lossfunc_t loss_derivative, size_t batch_size)
{
Layer* current = m->last;
while(current)
{
current->init_layer_neurons(current, batch_size, 1);
layer_set_optimizer(current, optimizer);
current = current->prev;
}
m->batch_size = batch_size;
m->loss_func = loss_func;
m->loss_func_derivative = loss_derivative;
}
Matrix* __model_predict(Model* m, Matrix* in)
{
Layer* current = m->first;
Matrix* neurons = current->get_neuron_matrix(current);
matrix_copy(neurons, in);
Matrix* out = in;
while(current)
{
Matrix* temp = current->forward_pass(current);
if(temp)
{
out = temp;
}
current = current->next;
}
return out;
}
void model_predict(Model* m, Matrix* in, Matrix* out)
{
model_update_before_predict(m, 1);
Layer* current = m->first;
while(current)
{
current->init_layer_neurons(current, in->rows, 0);
current = current->next;
}
Matrix* result = __model_predict(m, in);
matrix_copy(out, result);
}
void model_fit(Model* m, Matrix* inputs, Matrix* outputs, size_t epochs)
{
assert(inputs->rows == outputs->rows);
model_update_before_predict(m, 0);
int has_allocated = 0;
size_t predictions_cols = m->last->get_neuron_matrix(m->last)->cols;
if(outputs->cols == 1 && predictions_cols != 1)
{
Matrix* new_outputs = matrix_initn(outputs->rows, predictions_cols, 0);
for(size_t y = 0; y < outputs->rows; ++y)
{
matrix_set(new_outputs, y, matrix_at(outputs, y, 0), 1);
}
outputs = new_outputs;
has_allocated = 1;
}
size_t batch_size = m->batch_size;
Matrix* batch = matrix_init(batch_size, inputs->cols);
Matrix* batch_outputs = matrix_init(batch_size, outputs->cols);
Matrix* loss_matrix = matrix_init(batch_size, 1);
Matrix* gradients = matrix_initn(batch_size, outputs->cols, 1);
size_t input_size = inputs->rows;
size_t indices_array_size = input_size;
if(input_size % batch_size != 0)
{
indices_array_size = (input_size / batch_size + 1) * batch_size;
}
size_t* indices = malloc(sizeof(size_t) * indices_array_size);
for(size_t i = 0; i < input_size; ++i)
{
indices[i] = i;
}
shuffle(indices, input_size);
for(size_t i = input_size; i < indices_array_size; ++i)
{
indices[i] = rand() % input_size;
}
size_t steps = indices_array_size / batch_size;
for(size_t i = 0; i < epochs; ++i)
{
printf("Epoch: %zu ", i + 1);
fflush(stdout);
cell_t mean_loss = 0;
for (size_t j = 0; j < steps; ++j)
{
for(size_t k = 0; k < batch_size; ++k)
{
size_t row = indices[j * batch_size + k];
matrix_copy_row(batch, inputs, k, row);
matrix_copy_row(batch_outputs, outputs, k, row);
}
Matrix* predictions = __model_predict(m, batch);
m->loss_func(predictions, batch_outputs, loss_matrix);
mean_loss = matrix_col_mean(loss_matrix, 0);
m->loss_func_derivative(predictions, batch_outputs, gradients);
m->last->backward_pass(m->last, gradients);
printf("\rEpoch: %zu. [%zu/%zu] Average loss: %lf ", i + 1, j + 1, steps, mean_loss);
fflush(stdout);
}
printf("\n");
}
matrix_free(batch);
matrix_free(batch_outputs);
matrix_free(gradients);
matrix_free(loss_matrix);
if(has_allocated)
{
matrix_free(outputs);
}
free(indices);
}
void model_save(Model* m, const char* const filename)
{
// I was going to implement this but I am running out of time
FILE* stream = fopen(filename, "wb");
if(!stream)
{
fprintf(stderr, "Couldn't open file \"%s\"\n", filename);
return;
}
fclose(stream);
}
void model_update_before_predict(Model* m, int is_predicting)
{
Layer* current = m->first;
while(current)
{
current->is_predicting = is_predicting;
current = current->next;
}
}