-
Notifications
You must be signed in to change notification settings - Fork 199
/
DropoutLayer.cpp
244 lines (234 loc) · 7.93 KB
/
DropoutLayer.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
// Copyright Hugh Perkins 2015 hughperkins at gmail
//
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include <iostream>
#include "net/NeuralNet.h"
#include "layer/Layer.h"
#include "dropout/DropoutLayer.h"
#include "dropout/DropoutMaker.h"
#include "dropout/DropoutForward.h"
#include "dropout/DropoutBackward.h"
#include "util/RandomSingleton.h"
#include "clmath/MultiplyBuffer.h"
//#include "test/PrintBuffer.h"
using namespace std;
#undef VIRTUAL
#define VIRTUAL
#undef STATIC
#define STATIC
DropoutLayer::DropoutLayer(EasyCL *cl, Layer *previousLayer, DropoutMaker *maker) :
Layer(previousLayer, maker),
numPlanes (previousLayer->getOutputPlanes()),
inputSize(previousLayer->getOutputSize()),
dropRatio(maker->_dropRatio),
outputSize(previousLayer->getOutputSize()),
random(RandomSingleton::instance()),
cl(cl),
masks(0),
output(0),
gradInput(0),
maskWrapper(0),
outputWrapper(0),
gradInputWrapper(0),
// outputCopiedToHost(false),
// gradInputCopiedToHost(false),
batchSize(0),
allocatedSize(0) {
if(inputSize == 0){
// maker->net->print();
throw runtime_error("Error: Dropout layer " + toString(layerIndex) + ": input image size is 0");
}
if(outputSize == 0){
// maker->net->print();
throw runtime_error("Error: Dropout layer " + toString(layerIndex) + ": output image size is 0");
}
dropoutForwardImpl = DropoutForward::instance(cl, numPlanes, inputSize, dropRatio);
dropoutBackwardImpl = DropoutBackward::instance(cl, numPlanes, inputSize, dropRatio);
multiplyBuffer = new MultiplyBuffer(cl);
}
VIRTUAL DropoutLayer::~DropoutLayer() {
delete multiplyBuffer;
delete dropoutForwardImpl;
delete dropoutBackwardImpl;
if(maskWrapper != 0) {
delete maskWrapper;
}
if(outputWrapper != 0) {
delete outputWrapper;
}
if(masks != 0) {
delete[] masks;
}
if(output != 0) {
delete[] output;
}
if(gradInputWrapper != 0) {
delete gradInputWrapper;
}
if(gradInput != 0) {
delete[] gradInput;
}
}
VIRTUAL std::string DropoutLayer::getClassName() const {
return "DropoutLayer";
}
VIRTUAL void DropoutLayer::fortesting_setRandomSingleton(RandomSingleton *random) {
this->random = random;
}
VIRTUAL void DropoutLayer::setBatchSize(int batchSize) {
// cout << "DropoutLayer::setBatchSize" << endl;
if(batchSize <= allocatedSize) {
this->batchSize = batchSize;
return;
}
if(maskWrapper != 0) {
delete maskWrapper;
}
if(outputWrapper != 0) {
delete outputWrapper;
}
if(masks != 0) {
delete[] masks;
}
if(output != 0) {
delete[] output;
}
if(gradInputWrapper != 0) {
delete gradInputWrapper;
}
if(gradInput != 0) {
delete[] gradInput;
}
this->batchSize = batchSize;
this->allocatedSize = batchSize;
masks = new unsigned char[ getOutputNumElements() ];
generateMasks();
maskWrapper = cl->wrap(getOutputNumElements(), masks);
output = new float[ getOutputNumElements() ];
outputWrapper = cl->wrap(getOutputNumElements(), output);
outputWrapper->createOnDevice();
gradInput = new float[ previousLayer->getOutputNumElements() ];
gradInputWrapper = cl->wrap(previousLayer->getOutputNumElements(), gradInput);
gradInputWrapper->createOnDevice();
}
VIRTUAL int DropoutLayer::getOutputNumElements() {
return batchSize * numPlanes * outputSize * outputSize;
}
VIRTUAL float *DropoutLayer::getOutput() {
if(outputWrapper->isDeviceDirty()) {
outputWrapper->copyToHost();
// outputCopiedToHost = true;
}
return output;
}
VIRTUAL bool DropoutLayer::needsBackProp() {
return previousLayer->needsBackProp(); // seems highly unlikely that we wouldnt have to backprop
// but anyway, we dont have any weights ourselves
// so just depends on upstream
}
VIRTUAL int DropoutLayer::getOutputNumElements() const {
// int outputSize = inputSize / dropoutSize;
return batchSize * numPlanes * outputSize * outputSize;
}
VIRTUAL int DropoutLayer::getOutputSize() const {
return outputSize;
}
VIRTUAL int DropoutLayer::getOutputPlanes() const {
return numPlanes;
}
VIRTUAL int DropoutLayer::getPersistSize(int version) const {
return 0;
}
VIRTUAL bool DropoutLayer::providesGradInputWrapper() const {
return true;
}
VIRTUAL CLWrapper *DropoutLayer::getGradInputWrapper() {
return gradInputWrapper;
}
VIRTUAL bool DropoutLayer::hasOutputWrapper() const {
return true;
}
VIRTUAL CLWrapper *DropoutLayer::getOutputWrapper() {
return outputWrapper;
}
VIRTUAL float *DropoutLayer::getGradInput() {
return gradInput;
}
VIRTUAL ActivationFunction const *DropoutLayer::getActivationFunction() {
return new LinearActivation();
}
//VIRTUAL void DropoutLayer::generateMasks() {
// int totalInputLinearSize = getOutputNumElements();
//// int numBytes = (totalInputLinearSize+8-1)/8;
//// unsigned char *bitsField = new unsigned char[numBytes];
// int idx = 0;
// unsigned char thisByte = 0;
// int bitsPacked = 0;
// for(int i = 0; i < totalInputLinearSize; i++) {
// //double value = ((int)random() % 10000) / 20000.0f + 0.5f;
// // 1 means we pass value through, 0 means we drop
// // dropRatio is probability that mask value is 0 therefore
// // so higher dropRatio => more likely to be 0
// unsigned char bit = random->_uniform() <= dropRatio ? 0 : 1;
//// unsigned char bit = 0;
// thisByte <<= 1;
// thisByte |= bit;
// bitsPacked++;
// if(bitsPacked >= 8) {
// masks[idx] = thisByte;
// idx++;
// bitsPacked = 0;
// }
// }
//}
VIRTUAL void DropoutLayer::generateMasks() {
int totalInputLinearSize = getOutputNumElements();
for(int i = 0; i < totalInputLinearSize; i++) {
masks[i] = random->_uniform() <= dropRatio ? 0 : 1;
}
}
VIRTUAL void DropoutLayer::forward() {
CLWrapper *upstreamOutputWrapper = 0;
if(previousLayer->hasOutputWrapper()) {
upstreamOutputWrapper = previousLayer->getOutputWrapper();
} else {
float *upstreamOutput = previousLayer->getOutput();
upstreamOutputWrapper = cl->wrap(previousLayer->getOutputNumElements(), upstreamOutput);
upstreamOutputWrapper->copyToDevice();
}
// cout << "training: " << training << endl;
if(training) {
// create new masks...
generateMasks();
maskWrapper->copyToDevice();
dropoutForwardImpl->forward(batchSize, maskWrapper, upstreamOutputWrapper, outputWrapper);
} else {
// if not training, then simply skip the dropout bit, copy the buffers directly
multiplyBuffer->multiply(getOutputNumElements(), dropRatio, upstreamOutputWrapper, outputWrapper);
}
if(!previousLayer->hasOutputWrapper()) {
delete upstreamOutputWrapper;
}
}
VIRTUAL void DropoutLayer::backward() {
// have no weights to backprop to, just need to backprop the errors
CLWrapper *gradOutputWrapper = 0;
bool weOwnErrorsWrapper = false;
if(nextLayer->providesGradInputWrapper()) {
gradOutputWrapper = nextLayer->getGradInputWrapper();
} else {
gradOutputWrapper = cl->wrap(getOutputNumElements(), nextLayer->getGradInput());
gradOutputWrapper->copyToDevice();
weOwnErrorsWrapper = true;
}
maskWrapper->copyToDevice();
dropoutBackwardImpl->backward(batchSize, maskWrapper, gradOutputWrapper, gradInputWrapper);
if(weOwnErrorsWrapper) {
delete gradOutputWrapper;
}
}
VIRTUAL std::string DropoutLayer::asString() const {
return "DropoutLayer{ dropRatio=" + toString(dropRatio) + " }";
}