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 , Conv2D , Conv1D
9
+ from keras .models import Model
11
10
from keras_contrib .wrappers import ConcreteDropout
12
11
13
12
@@ -20,7 +19,7 @@ def test_cdropout():
20
19
21
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 )
@@ -55,5 +54,61 @@ def sigmoid(x):
55
54
assert_approx_equal (eval_loss , loss )
56
55
57
56
57
+ def test_cdropout_conv ():
58
+ # Data
59
+ in_dim = 20
60
+ init_prop = .1
61
+ np .random .seed (1 )
62
+ X = np .random .randn (1 , in_dim , in_dim , 1 )
63
+
64
+ # Model
65
+ inputs = Input (shape = (in_dim , in_dim , 1 ,))
66
+ conv2d = Conv2D (1 , (3 , 3 ))
67
+ # Model, normal
68
+ cd = ConcreteDropout (conv2d , in_dim , prob_init = (init_prop , init_prop ))
69
+ x = cd (inputs )
70
+ model = Model (inputs , x )
71
+ model .compile (loss = None , optimizer = 'rmsprop' )
72
+ # Model, reference w/o Dropout
73
+ x_ref = conv2d (inputs )
74
+ model_ref = Model (inputs , x_ref )
75
+ model_ref .compile (loss = None , optimizer = 'rmsprop' )
76
+
77
+ # Check about correct 3rd weight (equal to initial value)
78
+ W = model .get_weights ()
79
+ assert_array_almost_equal (W [2 ], [np .log (init_prop )])
80
+
81
+ # Check if ConcreteDropout in prediction phase is the same as no dropout
82
+ out = model .predict (X )
83
+ out_ref = model_ref .predict (X )
84
+ assert_allclose (out , out_ref , atol = 1e-5 )
85
+
86
+ # Check if ConcreteDropout has the right amount of losses deposited
87
+ assert_equal (len (model .losses ), 1 )
88
+
89
+ # Check if the loss correspons the the desired value
90
+ def sigmoid (x ):
91
+ return 1. / (1. + np .exp (- x ))
92
+ p = np .squeeze (sigmoid (W [2 ]))
93
+ kernel_regularizer = cd .weight_regularizer * np .sum (np .square (W [0 ])) / (1. - p )
94
+ dropout_regularizer = (p * np .log (p ) + (1. - p ) * np .log (1. - p ))
95
+ dropout_regularizer *= cd .dropout_regularizer * 1 # only channels are dropped
96
+ loss = np .sum (kernel_regularizer + dropout_regularizer )
97
+ eval_loss = model .evaluate (X )
98
+ assert_approx_equal (eval_loss , loss )
99
+
100
+
101
+ def test_cdropout_wrong_layertype ():
102
+ """To be replaced with a real function test, if implemented.
103
+ """
104
+ in_dim = 20
105
+ init_prop = .1
106
+ with pytest .raises (ValueError ):
107
+ inputs = Input (shape = (in_dim , in_dim ,))
108
+ ConcreteDropout (Conv1D (1 , 3 ),
109
+ in_dim ,
110
+ prob_init = (init_prop , init_prop ))(inputs )
111
+
112
+
58
113
if __name__ == '__main__' :
59
114
pytest .main ([__file__ ])
0 commit comments