|
| 1 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +from __future__ import absolute_import |
| 17 | +from __future__ import division |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +from absl.testing import parameterized |
| 21 | + |
| 22 | +import numpy as np |
| 23 | +import tensorflow as tf |
| 24 | +from tensorflow_addons.activations import lisht |
| 25 | +from tensorflow_addons.utils import test_utils |
| 26 | + |
| 27 | + |
| 28 | +@test_utils.run_all_in_graph_and_eager_modes |
| 29 | +class LishtTest(tf.test.TestCase, parameterized.TestCase): |
| 30 | + @parameterized.named_parameters(("float16", np.float16), |
| 31 | + ("float32", np.float32), |
| 32 | + ("float64", np.float64)) |
| 33 | + def test_lisht(self, dtype): |
| 34 | + x = tf.constant([-2.0, -1.0, 0.0, 1.0, 2.0], dtype=dtype) |
| 35 | + expected_result = tf.constant( |
| 36 | + [1.9280552, 0.7615942, 0.0, 0.7615942, 1.9280552], dtype=dtype) |
| 37 | + self.assertAllCloseAccordingToType(lisht(x), expected_result) |
| 38 | + |
| 39 | + @parameterized.named_parameters(("float32", np.float32), |
| 40 | + ("float64", np.float64)) |
| 41 | + def test_theoretical_gradients(self, dtype): |
| 42 | + # Only test theoretical gradients for float32 and float64 |
| 43 | + # because of the instability of float16 while computing jacobian |
| 44 | + x = tf.constant([-2.0, -1.0, 0.0, 1.0, 2.0], dtype=dtype) |
| 45 | + |
| 46 | + theoretical, numerical = tf.test.compute_gradient(lisht, [x]) |
| 47 | + self.assertAllCloseAccordingToType( |
| 48 | + theoretical, numerical, rtol=5e-4, atol=5e-4) |
| 49 | + |
| 50 | + def test_unknown_shape(self): |
| 51 | + fn = lisht.get_concrete_function( |
| 52 | + tf.TensorSpec(shape=None, dtype=tf.float32)) |
| 53 | + |
| 54 | + for shape in [(1,), (1, 2), (1, 2, 3), (1, 2, 3, 4)]: |
| 55 | + x = tf.ones(shape=shape, dtype=tf.float32) |
| 56 | + self.assertAllClose(fn(x), lisht(x)) |
| 57 | + |
| 58 | + def test_serialization(self): |
| 59 | + config = tf.keras.activations.serialize(lisht) |
| 60 | + fn = tf.keras.activations.deserialize(config) |
| 61 | + self.assertEqual(fn, lisht) |
| 62 | + |
| 63 | + def test_serialization_with_layers(self): |
| 64 | + layer = tf.keras.layers.Dense(3, activation=lisht) |
| 65 | + config = tf.keras.layers.serialize(layer) |
| 66 | + deserialized_layer = tf.keras.layers.deserialize(config) |
| 67 | + self.assertEqual(deserialized_layer.__class__.__name__, |
| 68 | + layer.__class__.__name__) |
| 69 | + self.assertEqual(deserialized_layer.activation.__name__, "lisht") |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + tf.test.main() |
0 commit comments