Transformer block › MLP
Approximate GELU
×no parametersgelu(…), applied to conv1d(x, 'c_fc', n_state)
A smooth feature-wise activation, computed through its tanh approximation.
- in
- [B batch, T positions, 3072 d_ff]
- out
- [B batch, T positions, 3072 d_ff]
Explanation
The function is near zero for strongly negative inputs, dips to about −0.17 near −0.75, and approaches the identity for large positive ones.
y = x. Function evaluated from −4 to 4.Codemodel.py 25–26
def gelu(x):
return 0.5*x*(1+tf.tanh(np.sqrt(2/np.pi)*(x+0.044715*tf.pow(x, 3))))Sources1
Direct evidence
The released graph defines the tanh approximation used by GPT-2.
openai · openai/gpt-2 / src/model.py
Pinned revision 9b63575ef427 ↗ · lines 25–26
Read code excerpt
def gelu(x):
return 0.5*x*(1+tf.tanh(np.sqrt(2/np.pi)*(x+0.044715*tf.pow(x, 3))))