|
| 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 math |
| 23 | + |
| 24 | +import numpy as np |
| 25 | +import tensorflow as tf |
| 26 | +from tensorflow_addons.activations import gelu |
| 27 | +from tensorflow_addons.utils import test_utils |
| 28 | + |
| 29 | + |
| 30 | +def _ref_gelu(x, approximate=True): |
| 31 | + x = tf.convert_to_tensor(x) |
| 32 | + if approximate: |
| 33 | + pi = tf.cast(math.pi, x.dtype) |
| 34 | + coeff = tf.cast(0.044715, x.dtype) |
| 35 | + return 0.5 * x * ( |
| 36 | + 1.0 + tf.tanh(tf.sqrt(2.0 / pi) * (x + coeff * tf.pow(x, 3)))) |
| 37 | + else: |
| 38 | + return 0.5 * x * ( |
| 39 | + 1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0), x.dtype))) |
| 40 | + |
| 41 | + |
| 42 | +@test_utils.run_all_in_graph_and_eager_modes |
| 43 | +class GeluTest(tf.test.TestCase, parameterized.TestCase): |
| 44 | + @parameterized.named_parameters(("float16", np.float16), |
| 45 | + ("float32", np.float32), |
| 46 | + ("float64", np.float64)) |
| 47 | + def test_gelu(self, dtype): |
| 48 | + x = np.random.rand(2, 3, 4).astype(dtype) |
| 49 | + self.assertAllCloseAccordingToType(gelu(x), _ref_gelu(x)) |
| 50 | + self.assertAllCloseAccordingToType(gelu(x, False), _ref_gelu(x, False)) |
| 51 | + |
| 52 | + @parameterized.named_parameters(("float16", np.float16), |
| 53 | + ("float32", np.float32), |
| 54 | + ("float64", np.float64)) |
| 55 | + def test_gradients(self, dtype): |
| 56 | + x = tf.constant([1.0, 2.0, 3.0], dtype=dtype) |
| 57 | + |
| 58 | + for approximate in [True, False]: |
| 59 | + with self.subTest(approximate=approximate): |
| 60 | + with tf.GradientTape(persistent=True) as tape: |
| 61 | + tape.watch(x) |
| 62 | + y_ref = _ref_gelu(x, approximate) |
| 63 | + y = gelu(x, approximate) |
| 64 | + grad_ref = tape.gradient(y_ref, x) |
| 65 | + grad = tape.gradient(y, x) |
| 66 | + self.assertAllCloseAccordingToType(grad, grad_ref) |
| 67 | + |
| 68 | + @parameterized.named_parameters(("float32", np.float32), |
| 69 | + ("float64", np.float64)) |
| 70 | + def test_theoretical_gradients(self, dtype): |
| 71 | + # Only test theoretical gradients for float32 and float64 |
| 72 | + # because of the instability of float16 while computing jacobian |
| 73 | + x = tf.constant([1.0, 2.0, 3.0], dtype=dtype) |
| 74 | + |
| 75 | + for approximate in [True, False]: |
| 76 | + with self.subTest(approximate=approximate): |
| 77 | + theoretical, numerical = tf.test.compute_gradient( |
| 78 | + lambda x: gelu(x, approximate=approximate), [x]) |
| 79 | + self.assertAllCloseAccordingToType( |
| 80 | + theoretical, numerical, atol=1e-4) |
| 81 | + |
| 82 | + def test_unknown_shape(self): |
| 83 | + fn = gelu.get_concrete_function( |
| 84 | + tf.TensorSpec(shape=None, dtype=tf.float32)) |
| 85 | + |
| 86 | + for shape in [(1,), (1, 2), (1, 2, 3), (1, 2, 3, 4)]: |
| 87 | + x = tf.ones(shape=shape, dtype=tf.float32) |
| 88 | + self.assertAllClose(fn(x), gelu(x)) |
| 89 | + |
| 90 | + def test_serialization(self): |
| 91 | + ref_fn = gelu |
| 92 | + config = tf.keras.activations.serialize(ref_fn) |
| 93 | + fn = tf.keras.activations.deserialize(config) |
| 94 | + self.assertEqual(fn, ref_fn) |
| 95 | + |
| 96 | + def test_serialization_with_layers(self): |
| 97 | + layer = tf.keras.layers.Dense(3, activation=gelu) |
| 98 | + config = tf.keras.layers.serialize(layer) |
| 99 | + deserialized_layer = tf.keras.layers.deserialize(config) |
| 100 | + self.assertEqual(deserialized_layer.__class__.__name__, |
| 101 | + layer.__class__.__name__) |
| 102 | + self.assertEqual(deserialized_layer.activation.__name__, "gelu") |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + tf.test.main() |
0 commit comments