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