-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.cpp
332 lines (301 loc) · 11 KB
/
layers.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
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
#include <iostream>
#include <vector>
using namespace std;
#define ALPHA 0.01
#define LAMBDA 0.01
class Layer {
public:
Layer(int depth, int height, int width, int spatialExtent, int stride,
int zeroPadding, float_t alpha, float_t lambda, Layer *prev) {
_depth = depth;
_height = height;
_width = width;
_spatialExtent = spatialExtent;
_stride = stride;
_zeroPadding = zeroPadding;
_alpha = alpha;
_lambda = lambda;
_prev = prev;
_output.resize(depth * width * height);
}
virtual void feedForward() = 0;
virtual void backProp(const vector<float_t> &nextErrors) = 0;
int _width, _height, _depth, _spatialExtent, _stride, _zeroPadding;
float_t _alpha, _lambda;
vector<float_t> _output;
Layer *_prev;
vector<float_t> _errors;
protected:
vector<float_t> _weight;
vector<float_t> _bias;
vector<float_t> _deltaW;
int getIndex(int d, int h, int w) {
return d * (_height * _width) + h * _width + w;
}
float_t activationFunction(float_t v) {
return 1.0 / (1.0 + exp(-v));
}
float_t activationDerivativeFunction(float_t v) {
return v * (1.0 - v);
}
};
class Input: public Layer {
public:
Input(int depth, int height, int width): Layer(depth, height, width, 0, 0, 0, 0, 0, NULL) {}
void setOutput(const vector<float_t> &output) {
_output = output;
}
void feedForward(){}
void backProp(const vector<float_t> &nextErrors){}
};
class ConvolutionalLayer: public Layer {
public:
ConvolutionalLayer(int depth, int spatialExtent, int stride, int zeroPadding, Layer *prev):
Layer(depth, (prev->_height - spatialExtent + 2 * zeroPadding)/stride + 1,
(prev->_width - spatialExtent + 2 * zeroPadding)/stride + 1,
spatialExtent, stride, zeroPadding, ALPHA, LAMBDA, prev) {
_weight.resize(spatialExtent * spatialExtent * prev->_depth * _depth);
_deltaW.resize(spatialExtent * spatialExtent * prev->_depth * _depth);
_bias.resize(_depth * _height * _width);
initWeight();
}
void feedForward() {
// CPU feedforward
for (int out = 0; out < _depth; out++) {
for (int h = 0; h < _height; h++) {
for (int w = 0; w < _width; w++) {
float_t result = 0;
for (int in = 0; in < _prev->_depth; in++) {
result += sumWeight(in, out, h, w);
}
int index = getIndex(out, h, w);
_output[index] = activationFunction(result + _bias[index]);
}
}
}
}
void backProp(const vector<float_t> &nextErrors) {
int inWidth = _prev->_width, inHeight = _prev->_height, inDepth = _prev->_depth;
int F = _spatialExtent;
_errors.clear();
_errors.resize(inWidth * inHeight * inDepth);
// calculate error term
//for (int out = 0; out < _depth; out++) {
//for (int in = 0; in < inDepth; in++) {
//for (int w = 0; w < _width; w++) {
//for (int h = 0; h < _height; h++) {
//for (int y = 0; y < _spatialExtent; y++) {
//for (int x = 0; x < _spatialExtent; x++) {
//int index = in * inWidth * inHeight + (h + y) * inWidth + (x + w);
//int weightIndex = in * _depth * F * F + out * F * F + (F - y - 1) * F + (F - x - 1);
//_errors[index] += nextErrors[out * _width * _height + h * _width + w]
//* _weight[weightIndex] * activationDerivativeFunction(_prev->_output[index]);
//}
//}
//}
//}
//}
//}
for (int out = 0; out < _depth; out++) {
for (int h = 0; h < _height; h++) {
for (int w = 0; w < _width; w++) {
int inH = h * _stride;
int inW = w * _stride;
for (int in = 0; in < inDepth; in++) {
for (int y = 0; y < _spatialExtent; y++) {
for (int x = 0; x < _spatialExtent; x++) {
int index = in * inWidth * inHeight + (h + y) * inWidth + (x + w);
//int weightIndex = in * _depth * F * F + out * F * F + (F - 1 - y) * F + (F - 1 - x);
int weightIndex = in * _depth * F * F + out * F * F + y * F + x;
_errors[index] += nextErrors[out * _height * _width + h * _width + w]
* _weight[weightIndex] * activationDerivativeFunction(_prev->_output[index]);
}
}
}
}
}
}
// update weight
for (int out = 0; out < _depth; out++) {
for (int h = 0; h < _height; h++) {
for (int w = 0; w < _width; w++) {
int outIndex = out * _width * _height + h * _width + w;
for (int in = 0; in < inDepth; in++) {
for (int y = 0; y < F; y++) {
for (int x = 0; x < F; x++) {
//int target = in * _depth * F * F + out * F * F + (F - y - 1) * F + (F - x - 1);
int target = in * _depth * F * F + out * F * F + y * F + x;
int inH = h * _stride + y;
int inW = w * _stride + x;
float_t input = _prev->_output[in * inHeight * inWidth + inH * inWidth + inW];
float_t delta = _alpha * input * nextErrors[outIndex] + _lambda * _deltaW[target];
_weight[target] -= delta;
// update momentum
_deltaW[target] = delta;
}
}
_bias[outIndex] -= _alpha * nextErrors[outIndex];
}
}
}
}
}
void initWeight() {
for (int i = 0; i < _weight.size(); i++) {
_weight[i] = 1.0 * (rand() - rand())/RAND_MAX;
}
for (int i = 0; i < _bias.size(); i++) {
_bias[i] = 1.0 * (rand() - rand())/RAND_MAX;
}
}
private:
vector<float_t> _weight;
vector<float_t> _bias;
float_t sumWeight(int in, int out, int h, int w) {
int startH = h * _stride;
int startW = w * _stride;
int inHeight = _prev->_height;
int inWidth = _prev->_width;
float_t result = 0;
int F = _spatialExtent;
for (int i = 0; i < F; i++) {
for (int j = 0; j < F; j++) {
int index = in * (inHeight * inWidth) + (startH + i) * inWidth + (startW + j); // row startH + i, col startW + j
float_t input = _prev->_output[index];
int inDepth = _prev->_depth;
int indexWeight = in * _depth * F * F + out * F * F + i * F + j;
result += input * _weight[indexWeight];
}
}
return result;
}
};
class MaxPoolingLayer: public Layer {
public:
MaxPoolingLayer(int spatialExtent, int stride, Layer *prev):
Layer(prev->_depth, (prev->_height - spatialExtent)/stride + 1,
(prev->_width - spatialExtent)/stride + 1,
spatialExtent, stride, 0, 0, 0, prev) {
_maxIndex.resize(_depth * _height * _width);
}
void feedForward() {
for (int d = 0; d < _depth; d++) {
for (int h = 0; h < _height; h++) {
for (int w = 0; w < _width; w++) {
int index = getIndex(d, h, w);
_output[index] = getMax(d, h, w, index);
}
}
}
}
void backProp(const vector<float_t> &nextErrors) {
_errors.clear();
_errors.resize(_prev->_depth * _prev->_height * _prev->_width);
for (int i = 0; i < _maxIndex.size(); i++) {
_errors[_maxIndex[i]] = nextErrors[i];
}
}
void initWeight() {}
private:
vector<int> _maxIndex;
float_t getMax(int d, int h, int w, int outIndex) {
int startH = h * _stride;
int startW = w * _stride;
int H = _prev->_height;
int W = _prev->_width;
float_t result = -1000000000;
for (int i = startH; i < startH + _spatialExtent; i++) {
for (int j = startW; j < startW + _spatialExtent; j++) {
int index = d * (H * W) + i * W + j;
if (_prev->_output[index] > result) {
result = _prev->_output[index];
_maxIndex[outIndex] = index;
}
}
}
return result;
}
};
class FullyConnectedLayer: public Layer {
public:
FullyConnectedLayer(int depth, Layer *prev): Layer(depth, 1, 1, 0, 0, 0, ALPHA, LAMBDA, prev) {
_weight.resize(depth * prev->_depth);
_bias.resize(depth);
_deltaW.resize(depth * prev->_depth);
initWeight();
}
void feedForward() {
int inDepth = _prev->_depth;
for (int out = 0; out < _depth; out++) {
float_t result = 0;
for (int in = 0; in < inDepth; in++) {
result += _weight[out * inDepth + in] * _prev->_output[in];
}
_output[out] = activationFunction(result + _bias[out]);
}
}
void backProp(const vector<float_t> &nextErrors) {
// calculate the error term
// equal to (next layer error term) x (transpose of weight matrix to next layer) * (activationDerivative of input)
_errors.resize(_prev->_depth);
int inDepth = _prev->_depth;
for (int in = 0; in < inDepth; in++) {
float_t result = 0;
for (int out = 0; out < _depth; out++) {
result += nextErrors[out] * _weight[inDepth * out + in];
}
_errors[in] = result * activationDerivativeFunction(_prev->_output[in]);
}
for (int out = 0; out < _depth; out++) {
for (int in = 0; in < inDepth; in++) {
// learning rate *
int index = out * inDepth + in;
float_t delta = _alpha * _prev->_output[in] * nextErrors[out] + _lambda * _deltaW[index];
_weight[index] -= delta;
_deltaW[index] = delta;
}
_bias[out] -= _alpha * nextErrors[out];
}
}
void initWeight() {
for (int i = 0; i < _weight.size(); i++) {
_weight[i] = 1.0 * (rand() - rand())/RAND_MAX;
}
for (int i = 0; i < _bias.size(); i++) {
_bias[i] = 1.0 * (rand() - rand())/RAND_MAX;
}
}
};
class OutputLayer: public Layer {
public:
OutputLayer(Layer *prev): Layer(prev->_depth, 1, 1, 0, 0, 0, 0, 0, prev) { }
void setLabel(int label) {
_label = label;
}
void feedForward() {
_output = _prev->_output;
}
float_t getError() {
float_t err = 0;
for (int i = 0; i < _depth; i++) {
int expected = (i == _label) ? 1 : 0;
err += 0.5 * (_output[i] - expected) * (_output[i] - expected);
}
return err;
}
int getPredict() {
int index = 0;
for (int i = 1; i < _depth; i++) if (_output[i] > _output[index]) index = i;
//for (int i = 0; i < _depth; i++) cout << _output[i] << ' ';
return index;
}
void backProp(const vector<float_t> &nextErrors) {
_errors.clear();
_errors.resize(_depth);
for (int i = 0; i < _depth; i++) {
int expected = (i == _label) ? 1 : 0;
_errors[i] = (_output[i] - expected) * activationDerivativeFunction(_prev->_output[i]);
}
}
int _label;
};