-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdense_layer.h
79 lines (69 loc) · 2.15 KB
/
dense_layer.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
struct DenseLayer {
int input_size;
int output_size;
double weights[1000][1000];
double bias[1][1000];
double input[1000];
double output[1000];
};
double *forward_propogation(struct DenseLayer* DL,double input_data[]){
for(int i=0;i<DL->input_size;i++) {
DL->input[i]=input_data[i];
}
double *ret = (double *)malloc(sizeof(double *) * DL->output_size);
for(int i=0;i<DL->output_size;i++){
double sum = 0;
for(int j=0;j<DL->input_size;j++){
sum += DL->weights[i][j]*DL->input[j];
}
DL->output[i] = sum + DL->bias[0][i];
}
for(int i=0;i<DL->output_size;i++){
ret[i] = DL->output[i];
}
return ret;
}
double *backward_probogation(struct DenseLayer* DL,double output_error[], double learning_rate){
int input_size = DL->input_size;
int output_size = DL->output_size;
double *input_error = (double *)malloc(sizeof(double *) * input_size);
double weights_error[input_size][output_size];
for(int i=0; i< input_size; i++){
double sum = 0;
for(int j=0;j<output_size;j++){
sum += DL->weights[i][j]*output_error[j];
}
input_error[i] = sum;
}
for(int i=0;i<input_size;i++){
for(int j=0;j<output_size; j++){
weights_error[i][j] = DL->input[i]*output_error[j];
}
}
for(int i=0;i<output_size;i++){
for(int j=0;j<input_size;j++){
DL->weights[i][j] -= learning_rate*weights_error[j][i];
}
}
for(int i=0;i<output_size;i++){
DL->bias[0][i] -= learning_rate*output_error[i];
}
return input_error;
}
void init_DenseLayer(struct DenseLayer* DL, int input_size, int output_size){
DL->input_size = input_size;
DL->output_size = output_size;
for(int i=0; i<output_size; i++){
for(int j=0; j<input_size; j++){
DL->weights[i][j] = (rand() / (double)RAND_MAX)-0.5;
if(i==0){
DL->bias[0][j] = (rand() / (double)RAND_MAX)-0.5;
}
}
}
}