Skip to content
DeepForgeby svx
All problems
HardDeep Learningdl-042

GELU (Tanh Approximation)

Problem

Apply the tanh approximation of GELU element-wise: 0.5 * x * (1 + tanh(sqrt(2 / pi) * (x + 0.044715 * x^3))).

The signature is gelu(x). Return a list of the same length.

Hint

The cubic term inside tanh is what distinguishes this approximation from plain tanh gating.

Test cases

case 1
in: [[0]]
expected: [0]
case 2
in: [[1,-1]]
expected: [0.8411919906,-0.1588080094]
case 3
in: [[2,-2,0.5]]
expected: [1.9545976941,-0.0454023059,0.3457140098]
case 4
in: [[100,-100]]
expected: [100,0]
Starter codePython
import math
def gelu(x):
    # Your code here
    pass

4 test cases · run your code against them in the editor