# GELU

**GELU**（高斯误差线性单元，Gaussian Error Linear Unit）是一种神经网络激活函数，定义为输入乘以标准高斯累积分布函数，即 \( \mathrm{GELU}(x) = x \cdot \Phi(x) \)。它由当时在加州大学伯克利分校任职的丹·亨德里克斯（Dan Hendrycks）与芝加哥丰田技术研究院的凯文·金佩尔（Kevin Gimpel）在2016年6月的一篇arXiv论文中提出，并成为BERT、GPT-2等Transformer模型中标准的前馈激活函数。<sup>[1](https://arxiv.org/abs/1606.08415)</sup><sup> • </sup><sup>[2](https://github.com/huggingface/transformers/blob/main/src/transformers/activations.py)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | \( \mathrm{GELU}(x) = x\Phi(x) = \frac{x}{2}\left(1 + \mathrm{erf}(x/\sqrt{2})\right) \)，其中 \( \Phi \) 为标准正态累积分布函数<sup>[1](https://arxiv.org/abs/1606.08415)</sup> |
| 提出者 | 丹·亨德里克斯与凯文·金佩尔，arXiv，2016年6月27日<sup>[1](https://arxiv.org/abs/1606.08415)</sup> |
| 行为特性 | 按输入的值对输入加权，而非像ReLU那样按符号进行门控；平滑、非单调，对大正输入近似线性<sup>[1](https://arxiv.org/abs/1606.08415)</sup><sup> • </sup><sup>[3](https://aiwiki.ai/wiki/gelu)</sup> |
| tanh近似 | \( 0.5x(1 + \tanh(\sqrt{2/\pi}(x + 0.044715x^3))) \)；相对精确GELU的最大误差为4.7e-4<sup>[4](https://arxiv.org/pdf/1606.08415)</sup><sup> • </sup><sup>[5](https://github.com/v-code01/geluapprox/blob/main/README.md)</sup> |
| sigmoid近似 | \( x\sigma(1.702x) \)；最大误差2.0e-2，约为tanh形式的43倍，但速度约为精确GELU的10倍<sup>[4](https://arxiv.org/pdf/1606.08415)</sup><sup> • </sup><sup>[5](https://github.com/v-code01/geluapprox/blob/main/README.md)</sup> |
| 框架支持 | PyTorch的`nn.GELU`，`approximate='none'`（精确）或`'tanh'`；TensorFlow与Hugging Face Transformers提供等价实现<sup>[6](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.activation.GELU.html)</sup><sup> • </sup><sup>[2](https://github.com/huggingface/transformers/blob/main/src/transformers/activations.py)</sup> |
| 采用情况 | BERT与GPT-2的前馈激活函数，二者均使用tanh近似<sup>[2](https://github.com/huggingface/transformers/blob/main/src/transformers/activations.py)</sup><sup> • </sup><sup>[5](https://github.com/v-code01/geluapprox/blob/main/README.md)</sup> |

## 定义与动机

该函数将每个输入 \( x \) 乘以 \( \Phi(x) \)，即标准正态随机变量不超过 \( x \) 的概率。作者将其推导为随机掩码的期望：将输入乘以 \( m \sim \mathrm{Bernoulli}(\Phi(x)) \)，于是输入越小越可能被丢弃，再取期望变换 \( \Phi(x) \cdot x + (1 - \Phi(x)) \cdot 0 = x\Phi(x) \)。他们选择高斯累积分布函数，是因为神经元输入往往服从正态分布，尤其是在使用批归一化时。这将GELU与随机正则化器联系起来：它是自适应Dropout的一种修改的期望，把ReLU的确定性零一乘法、dropout的随机置零以及zoneout循环神经网络正则化器的随机直通结合在一起。<sup>[4](https://arxiv.org/pdf/1606.08415)</sup>

所得曲线与ReLU（\( x \cdot \mathbf{1}_{x>0} \)）的差别在于一个具体方面：ReLU按符号对输入进行门控，而GELU按输入的值对输入加权。GELU平滑且无穷可微、非单调，对大正 \( x \) 近似线性；对大负输入则趋近于零，而不是精确截断为零。<sup>[1](https://arxiv.org/abs/1606.08415)</sup><sup> • </sup><sup>[3](https://aiwiki.ai/wiki/gelu)</sup>由于高斯累积分布函数通常用误差函数计算，精确形式为 \( \frac{x}{2}(1 + \mathrm{erf}(x/\sqrt{2})) \)。<sup>[4](https://arxiv.org/pdf/1606.08415)</sup>

## 近似

2016年的论文针对前馈速度值得牺牲精确性的场景，提出了两种更廉价的近似：<sup>[4](https://arxiv.org/pdf/1606.08415)</sup>

\[ \mathrm{GELU}(x) \approx 0.5x\left(1 + \tanh\left(\sqrt{2/\pi}\,(x + 0.044715x^3)\right)\right) \]

\[ \mathrm{GELU}(x) \approx x \cdot \sigma(1.702x) \]

两者不可互换。以f64精确erf为参照测得，tanh形式的最大绝对误差为4.7e-4，而sigmoid形式达到2.0e-2，约差43倍；以激活空间计，tanh约为0.05%的误差，sigmoid则为2%。sigmoid形式最廉价（一次指数运算），精确erf形式约慢9.5倍，tanh形式居中，约为sigmoid成本的4.3倍，因此在该基准测试中，tanh近似是速度—精度权衡上的有利选择。<sup>[5](https://github.com/v-code01/geluapprox/blob/main/README.md)</sup>

两种近似都在 \( |x| \approx 2 \) 到3的中段拐弯处最不准确，此处曲线发生弯折；而在两端尾部则近乎精确，此时GELU的行为近似于 \( x \) 或近似于0。误差是 \( x \) 的偶函数，在正负输入之间对称。浮点宽度并非限制因素：f32下的精确形式相对f64参照的误差为4.5e-7，比两种近似低数千倍，因此真正要紧的选择是用哪种近似，而不是浮点宽度。<sup>[5](https://github.com/v-code01/geluapprox/blob/main/README.md)</sup>

## 框架实现

PyTorch将该函数暴露为`torch.nn.GELU`，其`approximate`参数可取`'none'`（精确erf形式）或`'tanh'`（近似形式），默认为精确形式。<sup>[6](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.activation.GELU.html)</sup>Hugging Face Transformers库实现了Google BERT仓库所用的GELU，并指出其与OpenAI GPT的GELU相同。<sup>[2](https://github.com/huggingface/transformers/blob/main/src/transformers/activations.py)</sup>GPT-2及许多后续模型使用tanh近似。<sup>[5](https://github.com/v-code01/geluapprox/blob/main/README.md)</sup>

## 硬件考量

在Transformer推理中，激活函数与softmax、层归一化一样，属于计算密集的非线性算子，制约着在边缘设备上的部署。<sup>[7](https://openreview.net/pdf?id=QnuJR7qA3z)</sup>因此硬件工作会用更廉价的形式替代超越函数。一种FPGA实现采用定义域 \( [-4, 4] \) 上的切比雪夫展开，其定义域是依据激活输入的分布选定的；在该定义域之外，tanh项渐近于 \( \pm 1 \)，GELU退化为线性关系，对大正输入为 \( x \)，对大负输入为0，此时多项式近似没有益处。<sup>[8](https://www.mdpi.com/2079-9292/15/14/2981)</sup>另一种方法只对公式的某一段而非整个计算做分段近似，因为流行的基于tanh的近似对硬件不友好。<sup>[9](https://www.mdpi.com/2079-9292/14/9/1825)</sup>

## 实证表现

原论文在MNIST分类与自编码、推文[词性标注](https://www.edgepedia.org/zh/part-of-speech-tagging)、TIMIT帧识别以及[CIFAR-10](https://www.edgepedia.org/zh/cifar-10)/100分类上，将GELU与ReLU和ELU进行了对比评估，报告在所有考察的计算机视觉、自然语言处理和语音任务上均有性能提升。<sup>[1](https://arxiv.org/abs/1606.08415)</sup>在推文标注器上，五次运行的中位测试误差为GELU 12.57%、ReLU 12.67%、ELU 12.91%；在TIMIT上，以最低验证误差选出的中位测试误差为GELU 29.3%、ReLU 29.5%、ELU 29.6%。<sup>[4](https://arxiv.org/pdf/1606.08415)</sup>论文还发现，GELU网络对加噪MNIST输入的鲁棒性与ELU和ReLU相当或更优，并且在使用和不使用dropout时都取得最低的中位训练对数损失。<sup>[4](https://arxiv.org/pdf/1606.08415)</sup>

后来的比较表明，差距往往很小，且取决于具体设置。2023年一项针对残差卷积网络在CIFAR-10、CIFAR-100和STL-10上的数学分析与基准测试，将GELU置于二十种激活函数的广泛集合之中进行考察。<sup>[10](https://onlinelibrary.wiley.com/doi/10.1155/2023/4229924)</sup>一项针对表格型医疗数据的研究发现，在大样本量下激活函数的选择影响甚微：在包含253,680个样本的心脏病数据集上，ReLU、GELU、Swish、Mish和LeakyReLU之间的准确率跨度仅为0.09个百分点，而低数据量数据集则显示出更大的、依赖激活函数的分差。<sup>[11](https://www.frontiersin.org/journals/artificial-intelligence/articles/10.3389/frai.2026.1849571/full)</sup>由于GELU不同于ReLU，不会输出精确的零，语言模型的死神经元分析将激活值在整个数据集上保持在 \( -0.1 \) 到0.1之间的神经元定义为死亡；所考察的模型使用GELU（Phi-2）或SiLU（Llama、Qwen、Mistral）。<sup>[12](https://aclanthology.org/2025.ijcnlp-long.123.pdf)</sup>

## 相关函数

用不同的[累积分布函数](https://www.edgepedia.org/zh/cumulative-distribution-function)做同样的构造，可得到其他激活函数：使用逻辑斯谛分布累积分布函数 \( \sigma(x) \) 即得到Sigmoid线性单元（SiLU），\( x\sigma(x) \)。论文还指出，高斯的均值 \( \mu \) 与标准差 \( \sigma \) 可以设为可学习超参数，不过实验固定 \( \mu = 0 \)、\( \sigma = 1 \)，未引入新的超参数。<sup>[4](https://arxiv.org/pdf/1606.08415)</sup>GELU属于平滑自门控激活函数一族，该族还包括Swish和Mish，后来的激活函数论文常将它们用作基线。<sup>[13](https://www.mdpi.com/2079-9292/14/23/4661)</sup>

## References

1. [Dan Hendrycks, Kevin Gimpel (2016). Gaussian Error Linear Units (GELUs). arXiv:1606.08415.](https://arxiv.org/abs/1606.08415)
2. [GELU implementation in Hugging Face Transformers, src/transformers/activations.py](https://github.com/huggingface/transformers/blob/main/src/transformers/activations.py)
3. [GELU (Gaussian Error Linear Unit), AI Wiki](https://aiwiki.ai/wiki/gelu)
4. [arxiv.org](https://arxiv.org/pdf/1606.08415)
5. [geluapprox: benchmarking exact, tanh, and sigmoid GELU forms, GitHub](https://github.com/v-code01/geluapprox/blob/main/README.md)
6. [torch.nn.GELU, PyTorch documentation](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.activation.GELU.html)
7. [HARA: A Unified Framework for Hardware-Efficient Non-Linearity in Transformers (ICLR 2026 submission, OpenReview)](https://openreview.net/pdf?id=QnuJR7qA3z)
8. [High Accuracy Power Optimized FPGA Implementation for GELU Function: A Chebyshev Approach, Electronics (MDPI), 2025](https://www.mdpi.com/2079-9292/15/14/2981)
9. [High-Precision and Efficiency Hardware Implementation for GELU via Its Internal Symmetry, Electronics (MDPI), 2025](https://www.mdpi.com/2079-9292/14/9/1825)
10. [Mathematical Analysis and Performance Evaluation of the GELU Activation Function in Deep Learning, Journal of Mathematics (Wiley), 2023](https://onlinelibrary.wiley.com/doi/10.1155/2023/4229924)
11. [NeuroPlast: a learnable activation function evaluated under knowledge distillation for medical image classification, Frontiers in Artificial Intelligence, 2026](https://www.frontiersin.org/journals/artificial-intelligence/articles/10.3389/frai.2026.1849571/full)
12. [Dead neuron analysis across language models, IJCNLP-AACL 2025](https://aclanthology.org/2025.ijcnlp-long.123.pdf)
13. [TRex: A Smooth Nonlinear Activation Bridging Tanh and ReLU for Stable Deep Learning, Electronics (MDPI), 2025](https://www.mdpi.com/2079-9292/14/23/4661)

---
*Initially written Oct 15, 2026 · Edited: Nov 11, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Neural networks overview

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/gelu

English original: https://www.edgepedia.org/gelu
