-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.cpp
181 lines (134 loc) · 4.03 KB
/
layer.cpp
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
#include "layer.hpp"
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#define coordinate(x, y, w, h) (w) * (y) + (x)
#define DELAY
layer::layer() {}
layer::~layer() {}
dense::dense(layer& prev, uint w_from, uint w_to) {
this->w_from = w_from;
this->w_to = w_to;
weights = (float*)malloc(this->w_from * this->w_to * sizeof(float));
bias = (float*)malloc(this->w_to * sizeof(float));
for(int i = 0; i < this->w_from; ++i)
for(int j = 0; j < this->w_to; ++j)
weights[coordinate(i, j, this->w_from, this->w_to)] = (float)rand() / (float)RAND_MAX;
for(int i = 0; i < this->w_to; ++i)
bias[i] = (float)rand() / (float)RAND_MAX;
}
dense::~dense() {}
void dense::forward(float* prev, float* next) {
for(int i = 0; i < w_to; ++i)
next[i] = 0.0;
for(int i = 0; i < w_to; ++i)
for(int j = 0; j < w_from; ++j)
next[i] += prev[j] * weights[coordinate(j, i, w_from, w_to)];
for(int i = 0; i < w_to; ++i)
next[i] += bias[i];
#ifdef DELAY
sleep(1);
#endif
}
void dense::backward(float* next, float* prev, float* upper, float* lower) {
free(gradient);
gradient = (float*)malloc(w_from * sizeof(float));
for(int i = 0; i < w_from; ++i)
gradient[i] = 0.0;
for(int i = 0; i < w_from; ++i)
for(int j = 0; j < w_to; ++j)
gradient[i] += weights[coordinate(i, j, w_from, w_to)];
for(int i = 0; i < w_from; ++i)
lower[i] = upper[i] * gradient[i];
float* grad = (float*)malloc(w_from * w_to * sizeof(float));
for(int i = 0; i < w_to; ++i)
for(int j = 0; j < w_from; ++j) {
grad[coordinate(j, i, w_from, w_to)] = prev[j];
grad[coordinate(j, i, w_from, w_to)] *= upper[j];
}
for(int i = 0; i < w_from; ++i)
for(int j = 0; j < w_to; ++j)
weights[coordinate(i, j, w_from, w_to)] -= 0.01 * grad[coordinate(i, j, w_from, w_to)];
free(grad);
#ifdef DELAY
sleep(5);
#endif
}
relu::relu(layer& prev, uint w_size) {
this->w_size = w_size;
}
relu::~relu() {}
void relu::forward(float* prev, float* next) {
for(int i = 0; i < w_size; ++i) {
if(prev[i] >= 0)
next[i] = prev[i];
else
next[i] = 0.0;
}
#ifdef DELAY
sleep(1);
#endif
}
void relu::backward(float* next, float* prev, float* upper, float* lower) {
free(gradient);
gradient = (float*)malloc(w_size * sizeof(float));
for(int i = 0; i < w_size; ++i) {
if(prev[i] >= 0)
gradient[i] = 1;
else
gradient[i] = 0;
}
for(int i = 0; i < w_size; ++i)
lower[i] = upper[i] * gradient[i];
#ifdef DELAY
sleep(5);
#endif
}
softmax::softmax(layer& prev, uint w_size) {
this->w_size = w_size;
}
softmax::~softmax() {}
void softmax::forward(float* prev, float* next) {
float sum = 0.0;
for(int i = 0; i < w_size; ++i)
sum += expf(prev[i]);
for(int i = 0; i < w_size; ++i)
next[i] = expf(prev[i]) / sum;
#ifdef DELAY
sleep(1);
#endif
}
void softmax::backward(float* next, float* prev, float* upper, float* lower) {
free(gradient);
gradient = (float*)malloc(w_size * sizeof(float));
float sum = 0.0;
for(int i = 0; i < w_size; ++i)
sum += expf(prev[i]);
for(int i = 0; i < w_size; ++i)
gradient[i] = expf(prev[i]) / sum * (1.0 - expf(prev[i] / sum));
for(int i = 0; i < w_size; ++i)
lower[i] = upper[i] * gradient[i];
#ifdef DELAY
sleep(5);
#endif
}
src::src(uint w_size) {
this->w_size = w_size;
}
src::~src() {}
void src::forward(float* prev, float* next) {
for(int i = 0; i < w_size; ++i)
next[i] = prev[i];
}
void src::backward(float* next, float* prev) {
}
sink::sink(layer& prev, uint w_size) {
this->w_size = w_size;
}
sink::~sink() {}
void sink::forward(float* prev, float* next) {
for(int i = 0; i < w_size; ++i)
next[i] = prev[i];
}
void sink::backward(float* next, float* prev) {
}