GELU(Gaussian Error Linear Unit) 활성화 함수 소개
GELU는 딥러닝 분야에서, 특히 BERT, GPT 등의 대규모 언어 모델에서 널리 사용됩니다. 이 글에서는 GELU(Gaussian Error Linear Unit) 활성화 함수의 이론과 응용을 소개합니다. 확률론적 관점에서 GELU의 수학적 표현과 도함수를 유도하고, ReLU, ELU 등의 대표적인 활성화 함수와 비교하여 GELU의 특성을 분석합니다.
1. 개요
딥러닝의 발전 과정에서 활성화 함수는 핵심적인 역할을 해 왔습니다. 초기에 사용된 Sigmoid부터 오늘날 널리 쓰이는 ReLU까지, 어떤 활성화 함수를 선택하느냐는 신경망의 성능에 직접적인 영향을 미칩니다. GeLU (Gaussian Error Linear Unit)는 비교적 새로운 활성화 함수로, 여러 분야에서 뛰어난 성능을 보였으며 BERT, GPT 등의 대규모 언어 모델에서도 널리 사용됩니다.
확률론적 관점
확률론적 관점에서 GELU는 확률적 정규화 과정의 결정론적 근사로 해석할 수 있습니다. 익숙한 방법에 빗대어 보면, ReLU는 입력에 곱할 값이 0 또는 1로 확정되는 방식인 반면, Dropout은 확률적으로 입력에 0을 곱하는 방식입니다. 이와 비슷하게, 입력 x에 대한 GELU의 출력은 다음 확률 과정의 기댓값으로 볼 수 있습니다.
\begin{equation}\text{GELU}(x) = x \cdot \mathbb{E}[m], \text{ where } m \sim \text{Bernoulli}(\Phi(x))\end{equation}이러한 해석을 통해 GELU의 통계적 의미를 이론적으로 더 명확히 이해할 수 있습니다.
2. GELU의 수학적 표현
2.1 기본 정의
\begin{equation}\text{GELU}(x) = x \cdot \Phi(x)\end{equation}여기서 \Phi(x)는 표준 정규분포의 누적분포함수(CDF)입니다.
표준 정규분포의 확률밀도함수는 다음과 같습니다.
\begin{equation}f(x)=\frac1{\sqrt{2\pi}}\mathrm{~exp}{\left(-\frac{x^2}2\right)}\end{equation}따라서 누적분포함수 {P}\left(X\leq{x}\right)는 다음과 같습니다.
\begin{equation}\Phi(x)=F(x;0,1)=\frac1{\sqrt{2\pi}}\int_{-\infty}^x\exp\left(-\frac{t^2}2\right)dt.\end{equation}오차 함수 erf의 정의는 다음과 같습니다.
\begin{equation}\mathrm{erf}(x)=\frac1{\sqrt{\pi}}\int_{-x}^xe^{-t^2}\mathrm{~d}t=\frac2{\sqrt{\pi}}\int_0^xe^{-t^2}\mathrm{~d}t.\end{equation}
이 정의를 바탕으로 유도하면 다음과 같습니다.
\begin{equation}\begin{aligned}\Phi(x)&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{x}e^{-\frac{t^{2}}{2}}dt \\&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{0}e^{-\frac{t^{2}}{2}}dt\quad+\quad\frac{1}{\sqrt{2\pi}}\int_{0}^{x}e^{-\frac{t^{2}}{2}}dt \\&\overset{z=\frac{t}{\sqrt{2}}}{\Longrightarrow } \frac{1}{2}+\frac{1}{\sqrt{2\pi}}\int_{0}^{x}e^{-z^{2}}\cdot\sqrt{2}\cdot dz \\&= \frac{1}{2}\left[1+\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-z^{2}}\cdot dz \right] \\&=\frac{1}{2}\left[ 1+\operatorname{erf}(\frac{x}{\sqrt{2}}) \right]\end{aligned}\end{equation}
따라서 \Phi(x)는 다음과 같이 간단히 표현할 수 있습니다.
\begin{equation}\Phi(z)=\frac{1}{2}\left[1+\mathrm{erf}{\left(\frac{z}{\sqrt{2}}\right)}\right]\end{equation}
위 식을 바탕으로 GELU의 전체 형태를 다음과 같이 나타낼 수 있습니다.
\begin{equation}\begin{align*}\text{GELU}\left(x\right) &= x{P}\left(X\leq{x}\right) \\&= x\frac1{\sqrt{2\pi}}\int_{-\infty}^x\exp\left(-\frac{t^2}2\right)dt \\&= x\Phi\left(x\right) \\&= x \cdot \frac{1}{2}\left[1 + \text{erf}(x/\sqrt{2})\right],\end{align*}\end{equation}입력의 부호에 따라 입력을 통과시키거나 차단하는 ReLU(x\mathbf{1}_{x>0})와 달리, GELU는 입력값 x에 표준 정규분포에서 해당 값의 누적확률(즉, 백분위)을 가중치로 곱합니다. 따라서 GELU의 활성화 곡선은 ReLU보다 부드럽습니다.
2.2 근사 계산
GELU의 정확한 값을 계산하는 과정은 비교적 복잡하므로, 실제로는 다음 두 가지 근사 방법을 자주 사용합니다.
근사 방법 1(쌍곡탄젠트 기반, tanh 근사라고도 함):
\begin{equation}\text{GELU}(x) \approx 0.5x\left(1 + \tanh\left[\sqrt{2/\pi}(x + 0.044715x^3)\right]\right)\end{equation}근사 방법 2(Sigmoid 함수 기반, sigmoid 근사라고도 함):
\begin{equation}\text{GELU}(x) \approx x \cdot \sigma(1.702x)\end{equation}여기서 \sigma(x)는 sigmoid 함수입니다.
2.3 도함수 유도 과정
연쇄 법칙 적용
GELU의 도함수를 구하려면 연쇄 법칙을 사용해야 합니다. 먼저 GELU 함수를 다음과 같이 전개합니다.
\begin{equation}\text{GELU}(x) = \frac{x}{2} + \frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\end{equation}이 식을 미분하려면 곱의 미분 법칙을 적용해야 합니다.
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) = \frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\right) + \frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\right)\end{equation}단계별 유도
첫 번째 항의 미분은 간단합니다.
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\right) = \frac{1}{2}\end{equation}두 번째 항에는 곱의 미분 법칙을 적용합니다.
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\right) = \frac{1}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right) + \frac{x}{2}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\end{equation}오차 함수의 도함수
오차 함수의 도함수는 다음과 같습니다.
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}(x) = \frac{2}{\sqrt{\pi}}e^{-x^2}\end{equation}연쇄 법칙을 적용하면 다음과 같은 결과를 얻을 수 있습니다.
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}\left(\frac{x}{\sqrt{2}}\right) = \frac{2}{\sqrt{\pi}}e^{-x^2/2} \cdot \frac{1}{\sqrt{2}} = \sqrt{\frac{2}{\pi}}e^{-x^2/2}\end{equation}결과 종합
앞서 구한 항을 모두 합치면 다음과 같습니다.
\begin{equation}\begin{aligned}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) &= \frac{1}{2} + \frac{1}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right) + \frac{x}{2} \cdot \sqrt{\frac{2}{\pi}}e^{-x^2/2} \\&= \frac{1}{2}\left[1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right] + \frac{x}{\sqrt{2\pi}}e^{-x^2/2}\end{aligned}\end{equation}도함수의 또 다른 표현
표준 정규분포의 누적분포함수 \Phi(x)와 확률밀도함수 \phi(x)를 사용하면 GELU의 도함수를 더 간결하게 표현할 수 있습니다.
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) = \Phi(x) + x\phi(x)\end{equation}여기서 각 함수는 다음과 같습니다.
\begin{equation}\begin{aligned}\Phi(x) &= \frac{1}{2}\left[1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right] \\\phi(x) &= \frac{1}{\sqrt{2\pi}}e^{-x^2/2}\end{aligned}\end{equation}도함수의 성질 분석
GELU의 도함수에는 다음과 같은 중요한 성질이 있습니다.
- 모든 점에서 연속이고 미분 가능함
- x \to +\infty일 때, 도함수는 1에 가까워진다.
- x \to -\infty일 때, 도함수는 0에 가까워진다.
- x = 0에서 도함수의 값은 0.5이다.
3. GELU와 다른 활성화 함수의 비교
3.1 ReLU와의 비교

ReLU의 활성화 함수는 다음과 같다.
\begin{equation}\operatorname{ReLU}(x)=\begin{cases}x &\quad \text{ if } x>0\\0 &\quad \text{ if } x\leq0\end{cases}\end{equation}
GELU는 ReLU를 확률적으로 일반화한 것으로 볼 수 있다.
- x \gg 0일 때는 \Phi(x) \approx 1이므로 \operatorname{GELU}(x) \approx x가 되어 ReLU와 비슷하게 동작한다.
- x \ll 0일 때는 \Phi(x) \approx 0이므로 \operatorname{GELU}(x) \approx 0이 되어 ReLU와 비슷하게 동작한다.
3.2 ELU와의 비교

ELU의 활성화 함수는 다음과 같다.
\begin{equation}\operatorname{ELU}(x)=\begin{cases}x &\quad \text{ if } x \geq 0 \\\alpha(e^x-1) &\quad \text{ if } x < 0\end{cases}\end{equation}
- ELU는 xP(X \leq x), X \sim \operatorname{Cauchy}(0, 1) (코시 분포, Cauchy distribution)의 변형으로 볼 수 있으며, 형태가 GELU와 유사하다.
- GELU는 0 근처에서 부드럽게 전이하며, 이 특성은 ELU와 유사하다.

