|
26 | 26 |
|
27 | 27 | @test_utils.run_all_in_graph_and_eager_modes |
28 | 28 | class WeightNormalizationTest(tf.test.TestCase): |
29 | | - def test_weightnorm_dense_train(self): |
30 | | - model = tf.keras.models.Sequential() |
31 | | - model.add( |
32 | | - wrappers.WeightNormalization( |
33 | | - tf.keras.layers.Dense(2), input_shape=(3, 4))) |
34 | | - model.compile( |
35 | | - optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.001), |
36 | | - loss='mse') |
37 | | - model.fit( |
38 | | - np.random.random((10, 3, 4)), |
39 | | - np.random.random((10, 3, 2)), |
40 | | - epochs=3, |
41 | | - batch_size=10) |
42 | | - self.assertTrue(hasattr(model.layers[0], 'g')) |
43 | | - |
44 | | - def test_weightnorm_dense_train_notinit(self): |
45 | | - model = tf.keras.models.Sequential() |
46 | | - model.add( |
47 | | - wrappers.WeightNormalization( |
48 | | - tf.keras.layers.Dense(2), input_shape=(3, 4), data_init=False)) |
49 | | - |
50 | | - model.compile( |
51 | | - optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.001), |
52 | | - loss='mse') |
53 | | - model.fit( |
54 | | - np.random.random((10, 3, 4)), |
55 | | - np.random.random((10, 3, 2)), |
56 | | - epochs=3, |
57 | | - batch_size=10) |
58 | | - self.assertTrue(hasattr(model.layers[0], 'g')) |
59 | | - |
60 | | - def test_weightnorm_conv2d(self): |
61 | | - model = tf.keras.models.Sequential() |
62 | | - model.add( |
63 | | - wrappers.WeightNormalization( |
64 | | - tf.keras.layers.Conv2D(5, (2, 2), padding='same'), |
65 | | - input_shape=(4, 4, 3))) |
66 | | - |
67 | | - model.add(tf.keras.layers.Activation('relu')) |
68 | | - model.compile( |
69 | | - optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.001), |
70 | | - loss='mse') |
71 | | - model.fit( |
72 | | - np.random.random((2, 4, 4, 3)), |
73 | | - np.random.random((2, 4, 4, 5)), |
74 | | - epochs=3, |
75 | | - batch_size=10) |
76 | | - |
77 | | - self.assertTrue(hasattr(model.layers[0], 'g')) |
78 | | - |
79 | | - def test_weightnorm_applylayer(self): |
80 | | - images = tf.random.uniform((2, 4, 4, 3)) |
81 | | - wn_wrapper = wrappers.WeightNormalization( |
82 | | - tf.keras.layers.Conv2D(32, [2, 2]), input_shape=(4, 4, 3)) |
83 | | - wn_wrapper.apply(images) |
84 | | - self.assertTrue(hasattr(wn_wrapper, 'g')) |
85 | | - |
86 | | - def test_weightnorm_nonlayer(self): |
87 | | - images = tf.random.uniform((2, 4, 43)) |
88 | | - with self.assertRaises(AssertionError): |
89 | | - wrappers.WeightNormalization(images) |
90 | | - |
91 | | - def test_weightnorm_nokernel(self): |
92 | | - with self.assertRaises(ValueError): |
93 | | - wrappers.WeightNormalization(tf.keras.layers.MaxPooling2D( |
94 | | - 2, 2)).build((2, 2)) |
95 | | - |
96 | | - def test_weightnorm_keras(self): |
97 | | - input_data = np.random.random((10, 3, 4)).astype(np.float32) |
| 29 | + def test_weightnorm(self): |
| 30 | + test_utils.layer_test( |
| 31 | + wrappers.WeightNormalization, |
| 32 | + kwargs={ |
| 33 | + 'layer': tf.keras.layers.Conv2D(5, (2, 2)), |
| 34 | + }, |
| 35 | + input_shape=(2, 4, 4, 3)) |
| 36 | + |
| 37 | + def _check_data_init(self, data_init, input_data, expected_output): |
| 38 | + layer = tf.keras.layers.Dense( |
| 39 | + input_data.shape[-1], |
| 40 | + activation=None, |
| 41 | + kernel_initializer='identity', |
| 42 | + bias_initializer='zeros') |
98 | 43 | test_utils.layer_test( |
99 | 44 | wrappers.WeightNormalization, |
100 | 45 | kwargs={ |
101 | | - 'layer': tf.keras.layers.Dense(2), |
102 | | - 'input_shape': (3, 4) |
| 46 | + 'layer': layer, |
| 47 | + 'data_init': data_init, |
103 | 48 | }, |
104 | | - input_data=input_data) |
| 49 | + input_data=input_data, |
| 50 | + expected_output=expected_output) |
| 51 | + |
| 52 | + def test_weightnorm_with_data_init_is_false(self): |
| 53 | + input_data = np.array([[[-4, -4], [4, 4]]], dtype=np.float32) |
| 54 | + self._check_data_init( |
| 55 | + data_init=False, input_data=input_data, expected_output=input_data) |
| 56 | + |
| 57 | + def test_weightnorm_with_data_init_is_true(self): |
| 58 | + input_data = np.array([[[-4, -4], [4, 4]]], dtype=np.float32) |
| 59 | + self._check_data_init( |
| 60 | + data_init=True, |
| 61 | + input_data=input_data, |
| 62 | + expected_output=input_data / 4) |
| 63 | + |
| 64 | + def test_weightnorm_non_layer(self): |
| 65 | + images = tf.random.uniform((2, 4, 43)) |
| 66 | + with self.assertRaises(AssertionError): |
| 67 | + wrappers.WeightNormalization(images) |
| 68 | + |
| 69 | + def test_weightnorm_non_kernel_layer(self): |
| 70 | + images = tf.random.uniform((2, 2, 2)) |
| 71 | + with self.assertRaisesRegexp(ValueError, 'contains a `kernel`'): |
| 72 | + non_kernel_layer = tf.keras.layers.MaxPooling2D(2, 2) |
| 73 | + wn_wrapper = wrappers.WeightNormalization(non_kernel_layer) |
| 74 | + wn_wrapper(images) |
105 | 75 |
|
106 | 76 |
|
107 | 77 | if __name__ == "__main__": |
|
0 commit comments