Warning: This project is deprecated. TensorFlow Addons has stopped development,
The project will only be providing minimal maintenance releases until May 2024. See the full
announcement here or on
github.
tfa.activations.gelu
Stay organized with collections
Save and categorize content based on your preferences.
Gaussian Error Linear Unit.
tfa.activations.gelu(
x: tfa.types.TensorLike
,
approximate: bool = True
) -> tf.Tensor
Computes gaussian error linear:
\[
\mathrm{gelu}(x) = x \Phi(x),
\]
where
\[
\Phi(x) = \frac{1}{2} \left[ 1 + \mathrm{erf}(\frac{x}{\sqrt{2} }) \right]$
\]
when approximate
is False
; or
\[
\Phi(x) = \frac{x}{2} \left[ 1 + \tanh(\sqrt{\frac{2}{\pi} } \cdot (x + 0.044715 \cdot x^3)) \right]
\]
when approximate
is True
.
See Gaussian Error Linear Units (GELUs)
and BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.
Consider using tf.nn.gelu
instead.
Note that the default of approximate
changed to False
in tf.nn.gelu
.
Usage:
x = tf.constant([-1.0, 0.0, 1.0])
tfa.activations.gelu(x, approximate=False)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15865529, 0. , 0.8413447 ], dtype=float32)>
tfa.activations.gelu(x, approximate=True)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15880796, 0. , 0.841192 ], dtype=float32)>
Args |
x
|
A Tensor . Must be one of the following types:
float16 , float32 , float64 .
|
approximate
|
bool, whether to enable approximation.
|
Returns |
A Tensor . Has the same type as x .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-07-12 UTC.
[null,null,["Last updated 2023-07-12 UTC."],[],[],null,["# tfa.activations.gelu\n\n| **Note:** `tensorflow-addons` is deprecated, use [`tf.keras.activations.gelu`](https://www.tensorflow.org/api_docs/python/tf/keras/activations/gelu) instead.\n\n|--------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/addons/blob/v0.20.0/tensorflow_addons/activations/gelu.py#L22-L74) |\n\nGaussian Error Linear Unit. \n\n tfa.activations.gelu(\n x: ../../tfa/types/TensorLike,\n approximate: bool = True\n ) -\u003e tf.Tensor\n\nComputes gaussian error linear:\n\n\\\\\\[\n\\\\mathrm{gelu}(x) = x \\\\Phi(x),\n\\\\\\]\n\nwhere\n\n\\\\\\[\n\\\\Phi(x) = \\\\frac{1}{2} \\\\left\\[ 1 + \\\\mathrm{erf}(\\\\frac{x}{\\\\sqrt{2} }) \\\\right\\]$\n\\\\\\]\n\nwhen `approximate` is `False`; or\n\n\\\\\\[\n\\\\Phi(x) = \\\\frac{x}{2} \\\\left\\[ 1 + \\\\tanh(\\\\sqrt{\\\\frac{2}{\\\\pi} } \\\\cdot (x + 0.044715 \\\\cdot x\\^3)) \\\\right\\]\n\\\\\\]\n\nwhen `approximate` is `True`.\n\nSee [Gaussian Error Linear Units (GELUs)](https://arxiv.org/abs/1606.08415)\nand [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805).\n\nConsider using [`tf.nn.gelu`](https://www.tensorflow.org/api_docs/python/tf/nn/gelu) instead.\nNote that the default of `approximate` changed to `False` in [`tf.nn.gelu`](https://www.tensorflow.org/api_docs/python/tf/nn/gelu).\n\n#### Usage:\n\n x = tf.constant([-1.0, 0.0, 1.0])\n tfa.activations.gelu(x, approximate=False)\n \u003ctf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15865529, 0. , 0.8413447 ], dtype=float32)\u003e\n tfa.activations.gelu(x, approximate=True)\n \u003ctf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15880796, 0. , 0.841192 ], dtype=float32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|----------------------------------------------------------------------------------|\n| `x` | A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`. |\n| `approximate` | bool, whether to enable approximation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor`. Has the same type as `x`. ||\n\n\u003cbr /\u003e"]]