/
pool_layer.cc
executable file
·176 lines (139 loc) · 4.44 KB
/
pool_layer.cc
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
#include "pool_layer.h"
#include "iostream"
PoolLayer::PoolLayer(
int breadth_neuron,
int num_channels,
int stride,
int breadth_filter,
ActivationFunction *f )
: neuron_connected_(false),
propagated_(false),
breadth_neuron_(breadth_neuron),
stride_(stride),
breadth_filter_(breadth_filter),
num_channels_(num_channels),
Layer(f) {
int num_input;
int num_output;
breadth_output_ = (breadth_neuron-1)/stride + 1;
num_input = breadth_neuron * breadth_neuron;
assert(num_input / breadth_neuron == breadth_neuron);
num_input *= num_channels;
assert(num_input / num_channels == breadth_neuron * breadth_neuron);
num_input_ = num_input;
num_output =breadth_output_ * breadth_output_;
assert(num_output / breadth_output_ == breadth_output_);
num_output *= num_channels;
assert(num_output / num_channels == breadth_output_ * breadth_output_);
num_output_ = num_output;
}
void PoolLayer::CheckInputUnits(vector<struct Neuron> const &units) {
assert(units.size() == num_input_);
}
void PoolLayer::ArrangeOutputUnits(vector<struct Neuron> &units) {
units.resize(num_output_);
}
void PoolLayer::ConnectNeurons(
vector<struct Neuron> const &input,
vector<struct Neuron> const &output) {
assert(!neuron_connected_);
assert(input.size() == num_input_);
assert(output.size() == num_output_);
maxid.resize(num_output_);
neuron_connected_ = true;
}
void PoolLayer::CalculateOutputUnits(vector<struct Neuron> &units) {
assert(units.size() == num_output_);
double outputmax = -1000;
double outputmin = 1000;
for (int i=0; i<num_output_; i++) {
units[i].z = f_->Calculate(units[i].u, units);
outputmax = max( outputmax , units[i].z );
outputmin = min( outputmin , units[i].z );
}
#if debug
printf( "poolsig : %lf %lf\n" , outputmax , outputmin );
#endif
}
void PoolLayer::Propagate(
vector<struct Neuron> const &input,
vector<struct Neuron> &output) {
int area_output = breadth_output_ * breadth_output_;
int area_input = breadth_neuron_ * breadth_neuron_;
assert(input.size() == num_input_);
assert(output.size() == num_output_);
double outputmax = -1000;
double outputmin = 1000;
for (int i=0; i<num_output_; i++) {
output[i].u = 0.0;
}
assert(maxid.size() == num_output_);
for (int i=0; i<breadth_output_; i++) {
for (int j=0; j<breadth_output_; j++) {
for (int k=0; k<num_channels_; k++) {
int output_idx = k*area_output + i*breadth_output_ + j;
double maxv = -numeric_limits<double>::max();
assert(j*stride_ < breadth_neuron_);
assert(i*stride_ < breadth_neuron_);
for (int p=0; p<breadth_filter_; p++) {
for (int q=0; q<breadth_filter_; q++) {
int x = j*stride_+q;
int y = i*stride_+p;
double z = -numeric_limits<double>::max();
int input_idx = k*area_input + y*breadth_neuron_ + x;
if (x < breadth_neuron_ && y < breadth_neuron_) {
z = input[input_idx].z;
}
if (maxv < z) {
maxv = z;
maxid[output_idx] = input_idx;
}
}
}
assert(maxv != -numeric_limits<double>::max());
output[output_idx].u = maxv;
outputmax = max( outputmax , maxv );
outputmin = min( outputmin , maxv );
}
}
}
#if DEBUG
printf( "pool : %lf %lf\n" , outputmax , outputmin );
#endif
propagated_ = true;
}
void PoolLayer::BackPropagate(
vector<struct Neuron> const &input,
vector<double> const &next_delta,
ActivationFunction *f,
vector<double> &delta) {
assert(propagated_);
assert(input.size() == num_input_);
assert(next_delta.size() == num_output_);
assert(maxid.size() == num_output_);
delta.resize(num_input_);
for (int i=0; i<num_input_; i++) {
delta[i] = 0.0;
}
double deltamax = -1000;
double deltamin = 1000;
for (int i=0; i<num_output_; i++) {
delta[maxid[i]] += next_delta[i] * f->CalculateDerivative(input[maxid[i]].u);
deltamax = max( deltamax , delta[maxid[i]] );
deltamin = min( deltamin , delta[maxid[i]] );
}
#if DEBUG
printf( "pooldelta : %lf %lf\n" , deltamax , deltamin );
#endif
propagated_ = false;
}
void PoolLayer::UpdateLazySubtrahend(
vector<struct Neuron> const &input,
const vector<double> &next_delta) {
assert(input.size() == num_input_);
assert(next_delta.size() == num_output_);
// do nothing
}
void PoolLayer::ApplyLazySubtrahend() {
// do nothing
}