This repository has been archived by the owner on Feb 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers_baseline.c
356 lines (299 loc) · 11.6 KB
/
layers_baseline.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
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "layers.h"
#include "volume.h"
conv_layer_t* make_conv_layer(int input_width, int input_height, int input_depth, int filter_width, int num_filters,
int stride, int pad) {
conv_layer_t* l = (conv_layer_t*)malloc(sizeof(conv_layer_t));
l->output_depth = num_filters;
l->filter_width = filter_width;
l->input_depth = input_depth;
l->input_width = input_width;
l->input_height = input_height;
l->filter_height = l->filter_width;
l->stride = stride;
l->pad = pad;
l->output_width = (l->input_width + l->pad * 2 - l->filter_width) /
l->stride + 1;
l->output_height = (l->input_height + l->pad * 2 - l->filter_height) /
l->stride + 1;
l->filters = malloc(sizeof(volume_t*) * num_filters);
for (int i = 0; i < num_filters; i++) {
l->filters[i] = make_volume(l->filter_width, l->filter_height,
l->input_depth, 0.0);
}
l->bias = 0.0;
l->biases = make_volume(1, 1, l->output_depth, l->bias);
return l;
}
// Performs the forward pass for a convolutional layer by convolving each one
// of the filters with a particular input, and placing the result in the output
// array.
//
// One way to think about convolution in this case is that we have one of the
// layer's filters (a 3D array) that is superimposed on one of the layer's
// inputs (a second 3D array) that has been implicitly padded with zeros. Since
// convolution is a sum of products (described below), we don't actually have
// to add any zeros to the input volume since those terms will not contribute
// to the convolution. Instead, for each position in the filter, we just make
// sure that we are in bounds for the input volume.
//
// Essentially, the filter is "sliding" across the input, in both the x and y
// directions, where we increment our position in each direction by using the
// stride parameter.
//
// At each position, we compute the sum of the elementwise product of the filter
// and the part of the array it's covering. For instance, let's consider a 2D
// case, where the filter (on the left) is superimposed on some part of the
// input (on the right).
//
// Filter Input
// -1 0 1 1 2 3
// -1 0 1 4 5 6
// -1 0 1 7 8 9
//
// Here, the sum of the elementwise product is:
// Filter[0][0] * Input[0][0] + Filter[0][1] * Input[0][1] + ...
// = -1 * 1 + 0 * 2 + ... + 0 * 8 + 1 * 9
// = 6
//
// The 3D case is essentially the same, we just have to sum over the other
// dimension as well. Also, since volumes are internally represented as 1D
// arrays, we must use the volume_get and volume_set commands to access elements
// at a coordinate (x, y, d). Finally, we add the corresponding bias for the
// filter to the sum before putting it into the output volume.
void conv_forward(conv_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end) {
for (int i = start; i <= end; i++) {
volume_t* in = inputs[i];
volume_t* out = outputs[i];
int stride = l->stride;
for (int f = 0; f < l->output_depth; f++) {
volume_t* filter = l->filters[f];
int y = -l->pad;
for (int out_y = 0; out_y < l->output_height; y += stride, out_y++) {
int x = -l->pad;
for (int out_x = 0; out_x < l->output_width; x += stride, out_x++) {
// Take sum of element-wise product
double sum = 0.0;
for (int fy = 0; fy < filter->height; fy++) {
int in_y = y + fy;
for (int fx = 0; fx < filter->width; fx++) {
int in_x = x + fx;
if (in_y >= 0 && in_y < in->height && in_x >= 0 && in_x < in->width) {
for (int fd = 0; fd < filter->depth; fd++) {
sum += volume_get(filter, fx, fy, fd) * volume_get(in, in_x, in_y, fd);
}
}
}
}
sum += l->biases->weights[f];
volume_set(out, out_x, out_y, f, sum);
}
}
}
}
}
void conv_load(conv_layer_t* l, const char* file_name) {
int filter_width, filter_height, depth, filters;
FILE* fin = fopen(file_name, "r");
fscanf(fin, "%d %d %d %d", &filter_width, &filter_height, &depth, &filters);
assert(filter_width == l->filter_width);
assert(filter_height == l->filter_height);
assert(depth == l->input_depth);
assert(filters == l->output_depth);
for (int f = 0; f < filters; f++) {
for (int x = 0; x < filter_width; x++) {
for (int y = 0; y < filter_height; y++) {
for (int d = 0; d < depth; d++) {
double val;
fscanf(fin, "%lf", &val);
volume_set(l->filters[f], x, y, d, val);
}
}
}
}
for (int d = 0; d < l->output_depth; d++) {
double val;
fscanf(fin, "%lf", &val);
volume_set(l->biases, 0, 0, d, val);
}
fclose(fin);
}
relu_layer_t* make_relu_layer(int input_width, int input_height, int input_depth) {
relu_layer_t* l = (relu_layer_t*)malloc(sizeof(relu_layer_t));
l->input_depth = input_depth;
l->input_width = input_width;
l->input_height = input_height;
l->output_width = l->input_width;
l->output_height = l->input_height;
l->output_depth = l->input_depth;
return l;
}
// Applies the Rectifier Linear Unit (ReLU) function to the input, which sets
// output(x, y, d) to max(0.0, input(x, y, d)).
void relu_forward(relu_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end) {
for (int i = start; i <= end; i++) {
for (int x = 0; x < l->input_width; x++) {
for (int y = 0; y < l->input_height; y++) {
for (int d = 0; d < l->input_depth; d++) {
double value = (volume_get(inputs[i], x, y, d) < 0.0) ? 0.0 : volume_get(inputs[i], x, y, d);
volume_set(outputs[i], x, y, d, value);
}
}
}
}
}
pool_layer_t* make_pool_layer(int input_width, int input_height, int input_depth, int pool_width, int stride) {
pool_layer_t* l = (pool_layer_t*)malloc(sizeof(pool_layer_t));
l->pool_width = pool_width;
l->input_depth = input_depth;
l->input_width = input_width;
l->input_height = input_height;
l->pool_height = l->pool_width;
l->stride = stride;
l->pad = 0;
l->output_depth = input_depth;
l->output_width = floor((l->input_width + l->pad * 2 - l->pool_width) / l->stride + 1);
l->output_height = floor((l->input_height + l->pad * 2 - l->pool_height) / l->stride + 1);
return l;
}
// This is like the convolutional layer in that we are sliding across the input
// volume, but instead of having a filter that we use to find the sum of an
// elementwise product, we instead just output the max value of some part of
// the image. For instance, if we consider a 2D case where the following is the
// part of the input that we are considering:
//
// 1 3 5
// 4 2 1
// 2 2 2
//
// then the value of the corresponding element in the output is 5 (since that
// is the maximum element). This effectively compresses the input.
void pool_forward(pool_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end) {
for (int i = start; i <= end; i++) {
volume_t* in = inputs[i];
volume_t* out = outputs[i];
int n = 0;
for (int d = 0; d < l->output_depth; d++) {
int x = -l->pad;
for (int out_x = 0; out_x < l->output_width; x += l->stride, out_x++) {
int y = -l->pad;
for (int out_y = 0; out_y < l->output_height; y += l->stride, out_y++) {
double max = -INFINITY;
for (int fx = 0; fx < l->pool_width; fx++) {
for (int fy = 0; fy < l->pool_height; fy++) {
int in_y = y + fy;
int in_x = x + fx;
if (in_x >= 0 && in_x < in->width && in_y >= 0 && in_y < in->height) {
double v = volume_get(in, in_x, in_y, d);
if (v > max) {
max = v;
}
}
}
}
n++;
volume_set(out, out_x, out_y, d, max);
}
}
}
}
}
fc_layer_t* make_fc_layer(int input_width, int input_height, int input_depth, int num_neurons) {
fc_layer_t* l = (fc_layer_t*)malloc(sizeof(fc_layer_t));
l->output_depth = num_neurons;
l->input_depth = input_depth;
l->input_width = input_width;
l->input_height = input_height;
l->num_inputs = l->input_width * l->input_height * l->input_depth;
l->output_width = 1;
l->output_height = 1;
l->filters = (volume_t**)malloc(sizeof(volume_t*) * num_neurons);
for (int i = 0; i < l->output_depth; i++) {
l->filters[i] = make_volume(1, 1, l->num_inputs, 0.0);
}
l->bias = 0.0;
l->biases = make_volume(1, 1, l->output_depth, l->bias);
return l;
}
// Computes the dot product (i.e. the sum of the elementwise product) of the
// input's weights with each of the filters. Note that these filters are not
// the same as the filters for the convolutional layer.
void fc_forward(fc_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end) {
for (int j = start; j <= end; j++) {
volume_t* in = inputs[j];
volume_t* out = outputs[j];
for (int i = 0; i < l->output_depth; i++) {
double dot = 0.0;
for (int d = 0; d < l->num_inputs; d++) {
dot += in->weights[d] * l->filters[i]->weights[d];
}
dot += l->biases->weights[i];
out->weights[i] = dot;
}
}
}
void fc_load(fc_layer_t* l, const char* filename) {
FILE* fin = fopen(filename, "r");
int num_inputs;
int output_depth;
fscanf(fin, "%d %d", &num_inputs, &output_depth);
assert(output_depth == l->output_depth);
assert(num_inputs == l->num_inputs);
for (int i = 0; i < l->output_depth; i++) {
for (int j = 0; j < l->num_inputs; j++) {
fscanf(fin, "%lf", &(l->filters[i]->weights[j]));
}
}
for (int i = 0; i < l->output_depth; i++) {
fscanf(fin, "%lf", &(l->biases->weights[i]));
}
fclose(fin);
}
softmax_layer_t* make_softmax_layer(int input_width, int input_height, int input_depth) {
softmax_layer_t* l = (softmax_layer_t*)malloc(sizeof(softmax_layer_t));
l->input_depth = input_depth;
l->input_width = input_width;
l->input_height = input_height;
l->output_width = 1;
l->output_height = 1;
l->output_depth = l->input_width * l->input_height * l->input_depth;
l->likelihoods = (double*)malloc(sizeof(double) * l->output_depth);
return l;
}
// This function converts an input's weights array into a probability
// distribution by using the following formula:
//
// likelihood[i] = exp(in->weights[i]) / sum(exp(in->weights))
//
// To increase the numerical stability of taking the exponential of a value, we
// subtract the maximum input weights from each weight before taking the
// exponential. This yields exactly the same results as the expression above,
// but is more resilient to floating point errors.
void softmax_forward(softmax_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end) {
double likelihoods[l->output_depth];
for (int j = start; j <= end; j++) {
volume_t* in = inputs[j];
volume_t* out = outputs[j];
// Compute max activation (used to compute exponentials)
double amax = in->weights[0];
for (int i = 1; i < l->output_depth; i++) {
if (in->weights[i] > amax) {
amax = in->weights[i];
}
}
// Compute exponentials in a numerically stable way
double total = 0.0;
for (int i = 0; i < l->output_depth; i++) {
double e = exp(in->weights[i] - amax);
total += e;
likelihoods[i] = e;
}
// Normalize and output to sum to one
for (int i = 0; i < l->output_depth; i++) {
out->weights[i] = likelihoods[i] / total;
}
}
}