Skip to content

Commit e926ecb

Browse files
authored
Added python implementation for GeLU (#1154)
* average callback and optimizers * fixed links * added SWA * removed update, changed alias, 4 spaces * added _gelu_py, test * Delete average_optimizers_callback.ipynb
1 parent d9858a3 commit e926ecb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tensorflow_addons/activations/gelu.py

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# ==============================================================================
1515

1616
import tensorflow as tf
17+
import math
1718

1819
from tensorflow_addons.utils import types
1920
from tensorflow_addons.utils.resource_loader import LazySO
@@ -49,3 +50,13 @@ def _gelu_grad(op, grad):
4950
return _activation_so.ops.addons_gelu_grad(
5051
grad, op.inputs[0], op.get_attr("approximate")
5152
)
53+
54+
55+
def _gelu_py(x: types.TensorLike, approximate: bool = True) -> tf.Tensor:
56+
x = tf.convert_to_tensor(x)
57+
if approximate:
58+
pi = tf.cast(math.pi, x.dtype)
59+
coeff = tf.cast(0.044715, x.dtype)
60+
return 0.5 * x * (1.0 + tf.tanh(tf.sqrt(2.0 / pi) * (x + coeff * tf.pow(x, 3))))
61+
else:
62+
return 0.5 * x * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0), x.dtype)))

tensorflow_addons/activations/gelu_test.py

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919
import tensorflow as tf
2020
from tensorflow_addons.activations import gelu
21+
from tensorflow_addons.activations.gelu import _gelu_py
2122
from tensorflow_addons.utils import test_utils
2223

2324

@@ -51,6 +52,25 @@ def test_theoretical_gradients(self, dtype):
5152
)
5253
self.assertAllCloseAccordingToType(theoretical, numerical, atol=1e-4)
5354

55+
@parameterized.named_parameters(("float32", np.float32), ("float64", np.float64))
56+
def test_same_as_py_func(self, dtype):
57+
np.random.seed(100)
58+
for _ in range(20):
59+
self.verify_funcs_are_equivalent(dtype)
60+
61+
def verify_funcs_are_equivalent(self, dtype):
62+
x_np = np.random.uniform(-10, 10, size=(4, 4)).astype(dtype)
63+
x = tf.convert_to_tensor(x_np)
64+
for approximate in [True, False]:
65+
with tf.GradientTape(persistent=True) as t:
66+
t.watch(x)
67+
y_native = gelu(x, approximate=approximate)
68+
y_py = _gelu_py(x, approximate=approximate)
69+
self.assertAllCloseAccordingToType(y_native, y_py, atol=1e-4)
70+
grad_native = t.gradient(y_native, x)
71+
grad_py = t.gradient(y_py, x)
72+
self.assertAllCloseAccordingToType(grad_native, grad_py, atol=1e-4)
73+
5474

5575
if __name__ == "__main__":
5676
tf.test.main()

0 commit comments

Comments
 (0)