1
1
import pytest
2
2
import numpy as np
3
3
4
- from keras .layers import Input , Dense
5
- from keras .models import Model
6
4
from numpy .testing import assert_allclose
7
5
from numpy .testing import assert_array_almost_equal
8
6
from numpy .testing import assert_approx_equal
9
7
from numpy .testing import assert_equal
10
-
8
+ from keras .layers import Input , Dense , Conv1D , Conv2D , Conv3D
9
+ from keras .models import Model
11
10
from keras_contrib .wrappers import ConcreteDropout
12
11
13
12
14
13
def test_cdropout ():
15
- # Data
14
+ # DATA
16
15
in_dim = 20
17
16
init_prop = .1
18
17
np .random .seed (1 )
19
18
X = np .random .randn (1 , in_dim )
20
19
21
- # Model
20
+ # MODEL
22
21
inputs = Input (shape = (in_dim ,))
23
- dense = Dense (1 , use_bias = True , input_shape = ( in_dim ,) )
22
+ dense = Dense (1 , use_bias = True )
24
23
# Model, normal
25
24
cd = ConcreteDropout (dense , in_dim , prob_init = (init_prop , init_prop ))
26
25
x = cd (inputs )
@@ -31,6 +30,7 @@ def test_cdropout():
31
30
model_ref = Model (inputs , x_ref )
32
31
model_ref .compile (loss = 'mse' , optimizer = 'rmsprop' )
33
32
33
+ # CHECKS
34
34
# Check about correct 3rd weight (equal to initial value)
35
35
W = model .get_weights ()
36
36
assert_array_almost_equal (W [2 ], [np .log (init_prop )])
@@ -55,5 +55,76 @@ def sigmoid(x):
55
55
assert_approx_equal (eval_loss , loss )
56
56
57
57
58
+ def test_cdropout_conv ():
59
+ # DATA
60
+ in_dim = 20
61
+ init_prop = .1
62
+ np .random .seed (1 )
63
+ X = np .random .randn (1 , in_dim , in_dim , 1 )
64
+
65
+ # MODEL
66
+ inputs = Input (shape = (in_dim , in_dim , 1 ,))
67
+ conv2d = Conv2D (1 , (3 , 3 ))
68
+ # Model, normal
69
+ cd = ConcreteDropout (conv2d , in_dim , prob_init = (init_prop , init_prop ))
70
+ x = cd (inputs )
71
+ model = Model (inputs , x )
72
+ model .compile (loss = None , optimizer = 'rmsprop' )
73
+ # Model, reference w/o Dropout
74
+ x_ref = conv2d (inputs )
75
+ model_ref = Model (inputs , x_ref )
76
+ model_ref .compile (loss = None , optimizer = 'rmsprop' )
77
+
78
+ # CHECKS
79
+ # Check about correct 3rd weight (equal to initial value)
80
+ W = model .get_weights ()
81
+ assert_array_almost_equal (W [2 ], [np .log (init_prop )])
82
+
83
+ # Check if ConcreteDropout in prediction phase is the same as no dropout
84
+ out = model .predict (X )
85
+ out_ref = model_ref .predict (X )
86
+ assert_allclose (out , out_ref , atol = 1e-5 )
87
+
88
+ # Check if ConcreteDropout has the right amount of losses deposited
89
+ assert_equal (len (model .losses ), 1 )
90
+
91
+ # Check if the loss correspons the the desired value
92
+ def sigmoid (x ):
93
+ return 1. / (1. + np .exp (- x ))
94
+ p = np .squeeze (sigmoid (W [2 ]))
95
+ kernel_regularizer = cd .weight_regularizer * np .sum (np .square (W [0 ])) / (1. - p )
96
+ dropout_regularizer = (p * np .log (p ) + (1. - p ) * np .log (1. - p ))
97
+ dropout_regularizer *= cd .dropout_regularizer * 1 # only channels are dropped
98
+ loss = np .sum (kernel_regularizer + dropout_regularizer )
99
+ eval_loss = model .evaluate (X )
100
+ assert_approx_equal (eval_loss , loss )
101
+
102
+
103
+ def test_cdropout_1d_layer ():
104
+ """To be replaced with a real function test, if implemented.
105
+ """
106
+ in_dim = 20
107
+ init_prop = .1
108
+
109
+ with pytest .raises (ValueError ):
110
+ inputs = Input (shape = (in_dim , 1 ,))
111
+ ConcreteDropout (Conv1D (1 , 3 ),
112
+ in_dim ,
113
+ prob_init = (init_prop , init_prop ))(inputs )
114
+
115
+
116
+ def test_cdropout_3d_layer ():
117
+ """To be replaced with a real function test, if implemented.
118
+ """
119
+ in_dim = 20
120
+ init_prop = .1
121
+
122
+ with pytest .raises (ValueError ):
123
+ inputs = Input (shape = (in_dim , in_dim , in_dim , 1 ,))
124
+ ConcreteDropout (Conv3D (1 , 3 ),
125
+ in_dim ,
126
+ prob_init = (init_prop , init_prop ))(inputs )
127
+
128
+
58
129
if __name__ == '__main__' :
59
130
pytest .main ([__file__ ])
0 commit comments