-
Notifications
You must be signed in to change notification settings - Fork 2
/
fc_layer.c
47 lines (30 loc) · 1.17 KB
/
fc_layer.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
//
// Created by vmachado on 2/11/20.
//
#include "fc_layer.h"
fc_layer* fc_alloc(int in_dim, int out_dim) {
fc_layer* layer = aalloc(sizeof(*layer));
layer->weights = matrix_alloc(in_dim, out_dim);
layer->input = NULL;
randomize(layer->weights, 0.0f, sqrtf(2.0f / (float)in_dim));
return layer;
}
void fc_free(fc_layer *layer) {
matrix_free(layer->weights);
matrix_free(layer->input);
free(layer);
}
matrix* fc_forward(fc_layer *layer, matrix *raw_input) {
matrix_free(layer->input);
layer->input = mat_copy(raw_input);
return multiply(raw_input, layer->weights, false, false,
raw_input->rows, layer->weights->columns, raw_input->columns);
}
matrix* fc_backward(fc_layer *layer, matrix *dout, float lambda_reg, float l_rate) {
matrix *dweights = multiply(layer->input, dout, true, false, layer->input->columns, dout->columns, layer->input->rows);
apply_sum(dweights, layer->weights, lambda_reg);
matrix *dinput = multiply(dout, layer->weights, false, true, dout->rows, layer->weights->rows, dout->columns);
apply_sum(layer->weights, dweights, -l_rate);
matrix_free(dweights);
return dinput;
}