-
Notifications
You must be signed in to change notification settings - Fork 2
/
average_pooling_layer.h
88 lines (70 loc) · 2.6 KB
/
average_pooling_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
80
81
82
83
84
85
86
87
88
#pragma once
#include "layer.h"
#include "util.h"
namespace con {
class AveragePoolingLayer : public Layer {
public:
AveragePoolingLayer(const string &name, const int &kernel, const int &stride, Layer *prev) :
Layer(
name,
prev->num,
ceilDiv(prev->width - kernel, stride) + 1,
ceilDiv(prev->height - kernel, stride) + 1,
prev->depth,
prev),
kernel(kernel), stride(stride) {}
const int kernel;
const int stride;
void forward() {
for (int n = 0; n < num; n++) {
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[n][index] = getSum(n, d, h, w, index);
}
}
}
}
}
Real getSum(const int &n, const int &d, const int &h, const int &w, const int &outIndex) {
const int startH = h * stride;
const int startW = w * stride;
const int endH = std::min(startH + kernel, inHeight);
const int endW = std::min(startW + kernel, inWidth);
const int area = (endH - startH) * (endW - startW);
Real sum = 0 ;
for (int i = startH; i < endH; i++) {
for (int j = startW; j < endW; j++) {
const int inputIndex = d * inHeight * inWidth + i * inWidth + j;
sum += prev->output[n][inputIndex];
}
}
return sum / area;
}
void backProp(const vector<Vec> &nextErrors) {
clear(&errors);
for (int n = 0; n < num; n++) {
for (int out = 0; out < depth; out++) {
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
const int outputIndex = out * width * height + h * width + w;
const int startH = h * stride;
const int startW = w * stride;
const int endH = std::min(startH + kernel, inHeight);
const int endW = std::min(startW + kernel, inWidth);
const int area = (endH - startH) * (endW - startW);
for (int i = startH; i < endH; i++) {
for (int j = startW; j < endW; j++) {
const int inputIndex = out * inHeight * inWidth + i * inWidth + j;
errors[n][inputIndex] += nextErrors[n][outputIndex] / area;
}
}
}
}
}
}
}
void applyUpdate(const Real &lr, const Real &momentum, const Real &decay) {}
};
}