|
16 | 16 |
|
17 | 17 |
|
18 | 18 | import numpy as np
|
| 19 | +import pytest |
19 | 20 | from tensorflow.python import keras
|
20 |
| -from tensorflow.python.keras import testing_utils |
| 21 | +from tensorflow_addons.utils import test_utils |
21 | 22 | from tensorflow_addons.layers.noisy_dense import NoisyDense
|
22 | 23 | from tensorflow.python.framework import ops
|
23 | 24 | from tensorflow.python.keras import keras_parameterized
|
24 | 25 | from tensorflow.python.framework import tensor_spec
|
| 26 | +from tensorflow.debugging import assert_equal |
| 27 | +from tensorflow.python.keras.mixed_precision.experimental import policy |
25 | 28 |
|
| 29 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") |
| 30 | +@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64]) |
| 31 | +def test_noisy_dense(dtype): |
| 32 | + test_utils.layer_test( |
| 33 | + NoisyDense, kwargs={'units': 3, "dtype": dtype}, input_shape=(3, 2)) |
26 | 34 |
|
27 |
| -@keras_parameterized.run_all_keras_modes |
28 |
| -class NoisyDenseTest(keras_parameterized.TestCase): |
29 |
| - def test_noisy_dense(self): |
30 |
| - testing_utils.layer_test( |
31 |
| - NoisyDense, kwargs={'units': 3}, input_shape=(3, 2)) |
| 35 | + test_utils.layer_test( |
| 36 | + NoisyDense, kwargs={'units': 3, "dtype": dtype}, input_shape=(3, 4, 2)) |
32 | 37 |
|
33 |
| - testing_utils.layer_test( |
34 |
| - NoisyDense, kwargs={'units': 3}, input_shape=(3, 4, 2)) |
| 38 | + test_utils.layer_test( |
| 39 | + NoisyDense, kwargs={'units': 3, "dtype": dtype}, input_shape=(None, None, 2)) |
35 | 40 |
|
36 |
| - testing_utils.layer_test( |
37 |
| - NoisyDense, kwargs={'units': 3}, input_shape=(None, None, 2)) |
| 41 | + test_utils.layer_test( |
| 42 | + NoisyDense, kwargs={'units': 3, "dtype": dtype}, input_shape=(3, 4, 5, 2)) |
38 | 43 |
|
39 |
| - testing_utils.layer_test( |
40 |
| - NoisyDense, kwargs={'units': 3}, input_shape=(3, 4, 5, 2)) |
| 44 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") |
| 45 | +def test_noisy_dense_with_policy(): |
| 46 | + inputs = ops.convert_to_tensor_v2( |
| 47 | + np.random.randint(low=0, high=7, size=(2, 2))) |
| 48 | + layer = NoisyDense(5, dtype=policy.Policy('mixed_float16')) |
| 49 | + outputs = layer(inputs) |
| 50 | + output_signature = layer.compute_output_signature( |
| 51 | + tensor_spec.TensorSpec(dtype='float16', shape=(2, 2))) |
| 52 | + assert_equal(output_signature.dtype, dtypes.float16) |
| 53 | + assert_equal(output_signature.shape, (2, 5)) |
| 54 | + sassert_equal(outputs.dtype, 'float16') |
| 55 | + assert_equal(layer.kernel.dtype, 'float32') |
41 | 56 |
|
42 |
| - def test_noisy_dense_dtype(self): |
43 |
| - inputs = ops.convert_to_tensor_v2( |
44 |
| - np.random.randint(low=0, high=7, size=(2, 2))) |
45 |
| - layer = NoisyDense(5, dtype='float32') |
46 |
| - outputs = layer(inputs) |
47 |
| - self.assertEqual(outputs.dtype, 'float32') |
| 57 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") |
| 58 | +def test_noisy_dense_regularization(self): |
| 59 | + layer = NoisyDense( |
| 60 | + 3, |
| 61 | + kernel_regularizer=keras.regularizers.l1(0.01), |
| 62 | + bias_regularizer='l1', |
| 63 | + activity_regularizer='l2', |
| 64 | + name='noisy_dense_reg') |
| 65 | + layer(keras.backend.variable(np.ones((2, 4)))) |
| 66 | + assert_equal(3, len(layer.losses)) |
48 | 67 |
|
49 |
| - def test_noisy_dense_with_policy(self): |
50 |
| - inputs = ops.convert_to_tensor_v2( |
51 |
| - np.random.randint(low=0, high=7, size=(2, 2))) |
52 |
| - layer = NoisyDense(5, dtype=policy.Policy('mixed_float16')) |
53 |
| - outputs = layer(inputs) |
54 |
| - output_signature = layer.compute_output_signature( |
55 |
| - tensor_spec.TensorSpec(dtype='float16', shape=(2, 2))) |
56 |
| - self.assertEqual(output_signature.dtype, dtypes.float16) |
57 |
| - self.assertEqual(output_signature.shape, (2, 5)) |
58 |
| - self.assertEqual(outputs.dtype, 'float16') |
59 |
| - self.assertEqual(layer.kernel.dtype, 'float32') |
60 |
| - |
61 |
| - def test_noisy_dense_regularization(self): |
62 |
| - layer = NoisyDense( |
63 |
| - 3, |
64 |
| - kernel_regularizer=keras.regularizers.l1(0.01), |
65 |
| - bias_regularizer='l1', |
66 |
| - activity_regularizer='l2', |
67 |
| - name='noisy_dense_reg') |
68 |
| - layer(keras.backend.variable(np.ones((2, 4)))) |
69 |
| - self.assertEqual(3, len(layer.losses)) |
70 |
| - |
71 |
| - def test_noisy_dense_constraints(self): |
72 |
| - k_constraint = keras.constraints.max_norm(0.01) |
73 |
| - b_constraint = keras.constraints.max_norm(0.01) |
74 |
| - layer = NoisyDense( |
75 |
| - 3, kernel_constraint=k_constraint, bias_constraint=b_constraint) |
76 |
| - layer(keras.backend.variable(np.ones((2, 4)))) |
77 |
| - self.assertEqual(layer.kernel.constraint, k_constraint) |
78 |
| - self.assertEqual(layer.bias.constraint, b_constraint) |
| 68 | +@pytest.mark.usefixtures("maybe_run_functions_eagerly") |
| 69 | +def test_noisy_dense_constraints(self): |
| 70 | + k_constraint = keras.constraints.max_norm(0.01) |
| 71 | + b_constraint = keras.constraints.max_norm(0.01) |
| 72 | + layer = NoisyDense( |
| 73 | + 3, kernel_constraint=k_constraint, bias_constraint=b_constraint) |
| 74 | + layer(keras.backend.variable(np.ones((2, 4)))) |
| 75 | + assert_equal(layer.kernel.constraint, k_constraint) |
| 76 | + assert_equal(layer.bias.constraint, b_constraint) |
0 commit comments