Skip to content

Commit 9c631ff

Browse files
committed
[CIBUILD]
1 parent a724d74 commit 9c631ff

12 files changed

+195
-755
lines changed

deepray/activations/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# ==============================================================================
1515
"""Additional activation functions."""
1616

17+
from deepray.activations.gelu import gelu
1718
from deepray.activations.hardshrink import hardshrink
1819
from deepray.activations.lisht import lisht
1920
from deepray.activations.mish import mish

deepray/activations/gelu.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
import tensorflow as tf
17+
import warnings
18+
19+
from deepray.utils.types import TensorLike
20+
21+
22+
@tf.keras.utils.register_keras_serializable(package="Deepray")
23+
def gelu(x: TensorLike, approximate: bool = True) -> tf.Tensor:
24+
r"""Gaussian Error Linear Unit.
25+
26+
Computes gaussian error linear:
27+
28+
$$
29+
\mathrm{gelu}(x) = x \Phi(x),
30+
$$
31+
32+
where
33+
34+
$$
35+
\Phi(x) = \frac{1}{2} \left[ 1 + \mathrm{erf}(\frac{x}{\sqrt{2}}) \right]$
36+
$$
37+
38+
when `approximate` is `False`; or
39+
40+
$$
41+
\Phi(x) = \frac{x}{2} \left[ 1 + \tanh(\sqrt{\frac{2}{\pi}} \cdot (x + 0.044715 \cdot x^3)) \right]
42+
$$
43+
44+
when `approximate` is `True`.
45+
46+
See [Gaussian Error Linear Units (GELUs)](https://arxiv.org/abs/1606.08415)
47+
and [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805).
48+
49+
Consider using `tf.nn.gelu` instead.
50+
Note that the default of `approximate` changed to `False` in `tf.nn.gelu`.
51+
52+
Usage:
53+
54+
>>> x = tf.constant([-1.0, 0.0, 1.0])
55+
>>> dp.activations.gelu(x, approximate=False)
56+
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15865529, 0. , 0.8413447 ], dtype=float32)>
57+
>>> dp.activations.gelu(x, approximate=True)
58+
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15880796, 0. , 0.841192 ], dtype=float32)>
59+
60+
Args:
61+
x: A `Tensor`. Must be one of the following types:
62+
`float16`, `float32`, `float64`.
63+
approximate: bool, whether to enable approximation.
64+
Returns:
65+
A `Tensor`. Has the same type as `x`.
66+
"""
67+
warnings.warn(
68+
"gelu activation has been migrated to core TensorFlow, "
69+
"and will be deprecated in Addons 0.13. "
70+
"Note that the default of `approximate` changed to `False` in `tf.nn.gelu`.",
71+
DeprecationWarning,
72+
)
73+
74+
return tf.nn.gelu(x, approximate)

deepray/activations/tests/activations_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from deepray import activations
1919

2020
ALL_ACTIVATIONS = [
21+
"gelu",
2122
"hardshrink",
2223
"lisht",
2324
"mish",
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import pytest
17+
18+
import numpy as np
19+
import tensorflow as tf
20+
from deepray.activations import gelu
21+
from deepray.utils import test_utils
22+
23+
24+
@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64])
25+
def test_gelu(dtype):
26+
x = tf.constant([-2.0, -1.0, 0.0, 1.0, 2.0], dtype=dtype)
27+
expected_result = tf.constant([-0.04540229, -0.158808, 0.0, 0.841192, 1.9545977], dtype=dtype)
28+
test_utils.assert_allclose_according_to_type(gelu(x), expected_result)
29+
30+
expected_result = tf.constant([-0.04550028, -0.15865526, 0.0, 0.8413447, 1.9544997], dtype=dtype)
31+
test_utils.assert_allclose_according_to_type(gelu(x, False), expected_result)

deepray/layers/__init__.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
AdaptiveAveragePooling3D,
2323
AdaptiveMaxPooling3D,
2424
)
25-
from deepray.layers.crf import CRF
26-
from deepray.layers.embedding import Embedding
25+
2726
from deepray.layers.embedding_bag import EmbeddingBag
28-
from deepray.layers.esn import ESN
27+
from deepray.layers.gelu import GELU
2928
from deepray.layers.max_unpooling_2d import MaxUnpooling2D
3029
from deepray.layers.max_unpooling_2d_v2 import MaxUnpooling2DV2
3130
from deepray.layers.maxout import Maxout
3231
from deepray.layers.multihead_attention import MultiHeadAttention
33-
from deepray.layers.noisy_dense import NoisyDense
3432
from deepray.layers.normalizations import FilterResponseNormalization
3533
from deepray.layers.normalizations import GroupNormalization
3634
from deepray.layers.normalizations import InstanceNormalization
@@ -39,8 +37,11 @@
3937
from deepray.layers.polynomial import PolynomialCrossing
4038
from deepray.layers.snake import Snake
4139
from deepray.layers.sparsemax import Sparsemax
42-
from deepray.layers.spatial_pyramid_pooling import SpatialPyramidPooling2D
4340
from deepray.layers.spectral_normalization import SpectralNormalization
44-
from deepray.layers.stochastic_depth import StochasticDepth
41+
from deepray.layers.spatial_pyramid_pooling import SpatialPyramidPooling2D
4542
from deepray.layers.tlu import TLU
4643
from deepray.layers.wrappers import WeightNormalization
44+
from deepray.layers.esn import ESN
45+
from deepray.layers.stochastic_depth import StochasticDepth
46+
from deepray.layers.noisy_dense import NoisyDense
47+
from deepray.layers.crf import CRF

deepray/layers/dcn.py

-198
This file was deleted.

0 commit comments

Comments
 (0)