5
5
from numpy .testing import assert_array_almost_equal
6
6
from numpy .testing import assert_approx_equal
7
7
from numpy .testing import assert_equal
8
+ from keras import backend as K
8
9
from keras .layers import Input , Dense , Conv1D , Conv2D , Conv3D
9
10
from keras .models import Model
10
11
from keras_contrib .wrappers import ConcreteDropout
11
12
12
13
13
- def test_cdropout ():
14
+ @pytest .fixture
15
+ def clear_session_after_test ():
16
+ """Overridden: make session cleanup manually.
17
+ """
18
+ pass
19
+
20
+
21
+ @pytest .fixture (scope = 'module' )
22
+ def dense_model ():
23
+ """Initialize to be tested dense model. Executed once.
24
+ """
14
25
# DATA
15
26
in_dim = 20
16
27
init_prop = .1
@@ -28,34 +39,74 @@ def test_cdropout():
28
39
# Model, reference w/o Dropout
29
40
x_ref = dense (inputs )
30
41
model_ref = Model (inputs , x_ref )
31
- model_ref .compile (loss = 'mse' , optimizer = 'rmsprop' )
42
+ model_ref .compile (loss = None , optimizer = 'rmsprop' )
43
+
44
+ yield {'model' : model ,
45
+ 'model_ref' : model_ref ,
46
+ 'concrete_dropout' : cd ,
47
+ 'init_prop' : init_prop ,
48
+ 'in_dim' : in_dim ,
49
+ 'X' : X }
50
+ if K .backend () == 'tensorflow' or K .backend () == 'cntk' :
51
+ K .clear_session ()
52
+
53
+
54
+ def test_cdropout_dense_3rdweight (dense_model ):
55
+ """Check about correct 3rd weight (equal to initial value)
56
+ """
57
+ model = dense_model ['model' ]
58
+ init_prop = dense_model ['init_prop' ]
32
59
33
- # CHECKS
34
- # Check about correct 3rd weight (equal to initial value)
35
60
W = model .get_weights ()
36
61
assert_array_almost_equal (W [2 ], [np .log (init_prop )])
37
62
38
- # Check if ConcreteDropout in prediction phase is the same as no dropout
63
+
64
+ def test_cdropout_dense_identity (dense_model ):
65
+ """Check if ConcreteDropout in prediction phase is the same as no dropout
66
+ """
67
+ model = dense_model ['model' ]
68
+ model_ref = dense_model ['model_ref' ]
69
+ X = dense_model ['X' ]
70
+
39
71
out = model .predict (X )
40
72
out_ref = model_ref .predict (X )
41
73
assert_allclose (out , out_ref , atol = 1e-5 )
42
74
43
- # Check if ConcreteDropout has the right amount of losses deposited
75
+
76
+ def test_cdropout_dense_loss (dense_model ):
77
+ """Check if ConcreteDropout has the right amount of losses deposited
78
+ """
79
+ model = dense_model ['model' ]
80
+
44
81
assert_equal (len (model .losses ), 1 )
45
82
46
- # Check if the loss correspons the the desired value
83
+
84
+ def test_cdropout_dense_loss_value (dense_model ):
85
+ """Check if the loss corresponds the the desired value
86
+ """
87
+ model = dense_model ['model' ]
88
+ X = dense_model ['X' ]
89
+ cd = dense_model ['concrete_dropout' ]
90
+ in_dim = dense_model ['in_dim' ]
91
+
47
92
def sigmoid (x ):
48
93
return 1. / (1. + np .exp (- x ))
94
+
95
+ W = model .get_weights ()
49
96
p = np .squeeze (sigmoid (W [2 ]))
50
97
kernel_regularizer = cd .weight_regularizer * np .sum (np .square (W [0 ])) / (1. - p )
51
98
dropout_regularizer = (p * np .log (p ) + (1. - p ) * np .log (1. - p ))
52
99
dropout_regularizer *= cd .dropout_regularizer * in_dim
53
100
loss = np .sum (kernel_regularizer + dropout_regularizer )
101
+
54
102
eval_loss = model .evaluate (X )
55
103
assert_approx_equal (eval_loss , loss )
56
104
57
105
58
- def test_cdropout_conv ():
106
+ @pytest .fixture (scope = 'module' )
107
+ def conv2d_model ():
108
+ """Initialize to be tested conv model. Executed once.
109
+ """
59
110
# DATA
60
111
in_dim = 20
61
112
init_prop = .1
@@ -75,27 +126,63 @@ def test_cdropout_conv():
75
126
model_ref = Model (inputs , x_ref )
76
127
model_ref .compile (loss = None , optimizer = 'rmsprop' )
77
128
78
- # CHECKS
79
- # Check about correct 3rd weight (equal to initial value)
129
+ yield {'model' : model ,
130
+ 'model_ref' : model_ref ,
131
+ 'concrete_dropout' : cd ,
132
+ 'init_prop' : init_prop ,
133
+ 'in_dim' : in_dim ,
134
+ 'X' : X }
135
+ if K .backend () == 'tensorflow' or K .backend () == 'cntk' :
136
+ K .clear_session ()
137
+
138
+
139
+ def test_cdropout_conv2d_3rdweight (conv2d_model ):
140
+ """Check about correct 3rd weight (equal to initial value)
141
+ """
142
+ model = conv2d_model ['model' ]
143
+ init_prop = conv2d_model ['init_prop' ]
144
+
80
145
W = model .get_weights ()
81
146
assert_array_almost_equal (W [2 ], [np .log (init_prop )])
82
147
83
- # Check if ConcreteDropout in prediction phase is the same as no dropout
148
+
149
+ def test_cdropout_conv2d_identity (conv2d_model ):
150
+ """Check if ConcreteDropout in prediction phase is the same as no dropout
151
+ """
152
+ model = conv2d_model ['model' ]
153
+ model_ref = conv2d_model ['model_ref' ]
154
+ X = conv2d_model ['X' ]
155
+
84
156
out = model .predict (X )
85
157
out_ref = model_ref .predict (X )
86
158
assert_allclose (out , out_ref , atol = 1e-5 )
87
159
88
- # Check if ConcreteDropout has the right amount of losses deposited
160
+
161
+ def test_cdropout_conv2d_loss (conv2d_model ):
162
+ """Check if ConcreteDropout has the right amount of losses deposited
163
+ """
164
+ model = conv2d_model ['model' ]
165
+
89
166
assert_equal (len (model .losses ), 1 )
90
167
91
- # Check if the loss correspons the the desired value
168
+
169
+ def test_cdropout_conv2d_loss_value (conv2d_model ):
170
+ """Check if the loss corresponds the the desired value
171
+ """
172
+ model = conv2d_model ['model' ]
173
+ X = conv2d_model ['X' ]
174
+ cd = conv2d_model ['concrete_dropout' ]
175
+
92
176
def sigmoid (x ):
93
177
return 1. / (1. + np .exp (- x ))
178
+
179
+ W = model .get_weights ()
94
180
p = np .squeeze (sigmoid (W [2 ]))
95
181
kernel_regularizer = cd .weight_regularizer * np .sum (np .square (W [0 ])) / (1. - p )
96
182
dropout_regularizer = (p * np .log (p ) + (1. - p ) * np .log (1. - p ))
97
183
dropout_regularizer *= cd .dropout_regularizer * 1 # only channels are dropped
98
184
loss = np .sum (kernel_regularizer + dropout_regularizer )
185
+
99
186
eval_loss = model .evaluate (X )
100
187
assert_approx_equal (eval_loss , loss )
101
188
0 commit comments