-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdropout.py
266 lines (216 loc) · 9.62 KB
/
dropout.py
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
# -*- coding: utf-8 -*-
"""
.. invisible:
_ _ _____ _ _____ _____
| | | | ___| | | ___/ ___|
| | | | |__ | | | |__ \ `--.
| | | | __|| | | __| `--. \
\ \_/ / |___| |___| |___/\__/ /
\___/\____/\_____|____/\____/
Created on Apr 25, 2014
A dropout layer. It is a signal repeater with some repeating channels set to 0.
Inputs to be disabled are randomly selected each forward proparation.
Detailed description given in article by Krizhevsky, Sutskever and Hinton:
"ImageNet Classification with Deep Convolutional Neural Networks" (sec. 4.2).
███████████████████████████████████████████████████████████████████████████████
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
███████████████████████████████████████████████████████████████████████████████
"""
from __future__ import division
import numpy
from zope.interface import implementer
from veles.accelerated_units import AcceleratedUnit, IOpenCLUnit, ICUDAUnit, \
INumpyUnit
from veles.distributable import IDistributable, TriviallyDistributable
from veles.memory import eq_addr, ravel, Array
import veles.prng as random_generator
from veles.units import IUnit, Unit
from veles.znicz.nn_units import Forward, GradientDescentBase
class Dropout(AcceleratedUnit, TriviallyDistributable):
hide_from_registry = True
"""
A base class for forward and backward units of local
response normalization.
"""
def __init__(self, workflow, **kwargs):
super(Dropout, self).__init__(workflow, **kwargs)
self.dropout_ratio = kwargs.get("dropout_ratio")
def init_unpickled(self):
super(Dropout, self).init_unpickled()
self.sources_["dropout"] = {}
@property
def dropout_ratio(self):
""" Gets the relative amount of weights to disable.
"""
return self._dropout_ratio
@dropout_ratio.setter
def dropout_ratio(self, value):
""" Sets the relative amount of weights to disable.
"""
assert value is None or 0. < value < 1.
self._dropout_ratio = value
@implementer(IOpenCLUnit, ICUDAUnit, INumpyUnit)
class DropoutForward(Forward, Dropout):
"""
Forward propagation of dropout layer.
"""
__id__ = "c4117362-3c89-41bf-ba7d-a6b1bb0d8331"
MIN_RANDOM_STATE = 0
MAX_RANDOM_STATE = 0x100000000
MAPPING = {"dropout"}
def __init__(self, workflow, **kwargs):
super(DropoutForward, self).__init__(workflow, **kwargs)
self.mask = Array() # dropout mask
self.states = Array()
self.rand = random_generator.get()
self.demand("minibatch_class")
@Dropout.dropout_ratio.setter
def dropout_ratio(self, value):
Dropout.dropout_ratio.fset(self, value)
if hasattr(self, "input") and self.input is not None:
self.calc_mask()
def initialize(self, device, **kwargs):
super(DropoutForward, self).initialize(device=device, **kwargs)
self.mask.mem = numpy.empty_like(self.input.mem)
self.states.mem = self.rand.randint(
low=DropoutForward.MIN_RANDOM_STATE,
high=DropoutForward.MAX_RANDOM_STATE,
size=self.input.size * 4).astype(numpy.uint32)
if self.output:
assert self.output.shape[1:] == self.input.shape[1:]
if not self.output or self.output.shape[0] != self.input.shape[0]:
self.output.reset(numpy.zeros_like(self.input.mem))
self.init_vectors(self.input, self.output, self.states, self.mask)
def _gpu_init(self):
self._threshold_arg_ = numpy.empty(1, dtype=numpy.uint64)
self._pass_arg_ = numpy.empty(1, dtype=self.input.dtype)
self.build_program({"OUTPUT_SIZE": self.input.size}, "%s_%s" %
(self.__class__.__name__,
"x".join(str(x) for x in self.input.shape)),
dtype=self.input.dtype)
self.assign_kernel("dropout_forward")
self.set_args(self.input, self.device.skip(2), self.states, self.mask,
self.output)
def ocl_init(self):
self._gpu_init()
self._global_size = (self.input.size,)
self._local_size = None
def cuda_init(self):
self._gpu_init()
block_size = self.device.suggest_block_size(self._kernel_)
self._global_size = (
int(numpy.ceil(self.input.size / block_size)), 1, 1)
self._local_size = (block_size, 1, 1)
def calc_mask(self):
leave_ratio = 1.0 - self.dropout_ratio
self.rand.fill(self.mask.mem, -self.dropout_ratio, leave_ratio)
numpy.maximum(self.mask.mem, 0, self.mask.mem)
numpy.ceil(self.mask.mem, self.mask.mem)
self.mask.mem[:] = (self.mask.mem.astype(self.input.dtype) /
leave_ratio)
def numpy_run(self):
self.output.map_invalidate()
self.input.map_read()
if not self.forward_mode and self.minibatch_class == 2:
self.mask.map_invalidate()
self.calc_mask()
numpy.multiply(self.input.mem.ravel(), self.mask.mem.ravel(),
ravel(self.output.mem))
else:
self.output.mem[:] = self.input.mem
def _gpu_run(self):
self.unmap_vectors(self.input, self.output)
if self.forward_mode or self.minibatch_class < 2:
# Will copy input to output from outside (in cuda_run/ocl_run).
return True
self.unmap_vectors(self.states, self.mask)
self._threshold_arg_[0] = ((1 << 64) - 1.0) * self.dropout_ratio
self._pass_arg_[0] = 1.0 / (1.0 - self.dropout_ratio)
self.set_arg(1, self._threshold_arg_)
self.set_arg(2, self._pass_arg_)
self.execute_kernel(self._global_size, self._local_size)
return False
def ocl_run(self):
if self._gpu_run():
self.device.queue_.copy_buffer(
self.input.devmem, self.output.devmem, 0, 0,
self.output.nbytes, need_event=False)
def cuda_run(self):
if self._gpu_run():
self.output.devmem.from_device_async(self.input.devmem)
@implementer(IOpenCLUnit, ICUDAUnit, INumpyUnit, IDistributable)
class DropoutBackward(GradientDescentBase, Dropout):
"""
Backward propagation of droupout layer.
"""
MAPPING = {"dropout"}
def __init__(self, workflow, **kwargs):
self.mask = None # dropout mask (should be given from forward unit)
super(DropoutBackward, self).__init__(workflow, **kwargs)
self.undemand("input")
def initialize(self, device, **kwargs):
if not hasattr(self, "input"):
self.input = self.err_output
super(DropoutBackward, self).initialize(device=device, **kwargs)
def _gpu_init(self):
self.build_program({"OUTPUT_SIZE": self.err_output.size}, "%s_%s" %
(self.__class__.__name__,
"x".join(str(x) for x in self.err_input.shape)),
dtype=self.err_output.mem.dtype)
self.assign_kernel("dropout_backward")
self.set_args(self.mask, self.err_output, self.err_input)
def ocl_init(self):
self._gpu_init()
self._global_size = (self.err_output.size,)
self._local_size = None
def cuda_init(self):
self._gpu_init()
block_size = self.device.suggest_block_size(self._kernel_)
self._global_size = (
int(numpy.ceil(self.err_output.size / block_size)), 1, 1)
self._local_size = (block_size, 1, 1)
def numpy_run(self):
if eq_addr(self.err_input.mem, self.err_output.mem):
self.err_output.map_write()
else:
self.err_output.map_read()
self.err_input.map_invalidate()
self.mask.map_read()
numpy.multiply(self.err_output.mem.ravel(), self.mask.mem.ravel(),
ravel(self.err_input.mem))
def _gpu_run(self):
self.unmap_vectors(self.err_output, self.err_input, self.mask)
self.execute_kernel(self._global_size, self._local_size)
def ocl_run(self):
self._gpu_run()
def cuda_run(self):
self._gpu_run()
@implementer(IUnit)
class DropoutFixer(Unit):
# TODO: This is temporary fix. Need to remove it after fixing Dropout
# TODO: for real
def __init__(self, workflow, **kwargs):
super(DropoutFixer, self).__init__(workflow, **kwargs)
self.drops_ = None
def initialize(self, **kwargs):
self.drops_ = []
for u in self.workflow:
if isinstance(u, DropoutForward):
self.drops_.append(u)
def run(self):
mode = (not self.workflow.loader.minibatch_class == 2)
for u in self.drops_:
u.forward_mode = mode