11import pytest
22import numpy as np
33
4- from keras .layers import Input , Dense
5- from keras .models import Model
64from numpy .testing import assert_allclose
75from numpy .testing import assert_array_almost_equal
86from numpy .testing import assert_approx_equal
97from numpy .testing import assert_equal
10-
8+ from keras .layers import Input , Dense , Conv1D , Conv2D , Conv3D
9+ from keras .models import Model
1110from keras_contrib .wrappers import ConcreteDropout
1211
1312
1413def test_cdropout ():
15- # Data
14+ # DATA
1615 in_dim = 20
1716 init_prop = .1
1817 np .random .seed (1 )
1918 X = np .random .randn (1 , in_dim )
2019
21- # Model
20+ # MODEL
2221 inputs = Input (shape = (in_dim ,))
23- dense = Dense (1 , use_bias = True , input_shape = ( in_dim ,) )
22+ dense = Dense (1 , use_bias = True )
2423 # Model, normal
2524 cd = ConcreteDropout (dense , in_dim , prob_init = (init_prop , init_prop ))
2625 x = cd (inputs )
@@ -31,6 +30,7 @@ def test_cdropout():
3130 model_ref = Model (inputs , x_ref )
3231 model_ref .compile (loss = 'mse' , optimizer = 'rmsprop' )
3332
33+ # CHECKS
3434 # Check about correct 3rd weight (equal to initial value)
3535 W = model .get_weights ()
3636 assert_array_almost_equal (W [2 ], [np .log (init_prop )])
@@ -55,5 +55,76 @@ def sigmoid(x):
5555 assert_approx_equal (eval_loss , loss )
5656
5757
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+
58129if __name__ == '__main__' :
59130 pytest .main ([__file__ ])
0 commit comments