-
Notifications
You must be signed in to change notification settings - Fork 254
/
random-mask.jl
43 lines (37 loc) · 1.38 KB
/
random-mask.jl
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
@defstruct RandomMaskLayer Layer (
name :: String = "random-mask",
(ratio :: FloatingPoint = 0.5, 0 < ratio < 1),
(bottoms :: Vector{Symbol} = Symbol[], length(bottoms) > 0)
)
@characterize_layer(RandomMaskLayer,
can_do_bp => true,
is_inplace => true,
)
type RandomMaskLayerState <: LayerState
layer :: RandomMaskLayer
dropouts :: Vector{DropoutLayerState}
end
function setup(backend::Backend, layer::RandomMaskLayer, inputs::Vector{Blob}, diffs::Vector{Blob})
dropouts = Array(DropoutLayerState, length(inputs))
for i = 1:length(inputs)
dropout_layer = DropoutLayer(name="$(layer.name)-dropout-$i", auto_scale=false, ratio=layer.ratio,
bottoms=Symbol[symbol("$(layer.bottoms[i])-$i")])
dropouts[i] = setup(backend, dropout_layer, Blob[inputs[i]], Blob[diffs[i]])
end
return RandomMaskLayerState(layer, dropouts)
end
function shutdown(backend::Backend, state::RandomMaskLayerState)
for dropout_state in state.dropouts
shutdown(backend, dropout_state)
end
end
function forward(backend::Backend, state::RandomMaskLayerState, inputs::Vector{Blob})
for i = 1:length(inputs)
forward(backend, state.dropouts[i], Blob[inputs[i]])
end
end
function backward(backend::Backend, state::RandomMaskLayerState, inputs::Vector{Blob}, diffs::Vector{Blob})
for i = 1:length(inputs)
backward(backend, state.dropouts[i], Blob[inputs[i]], Blob[diffs[i]])
end
end