Problems
Loading...
1 / 1

Implement GELU Activation (Gaussian Error Linear Unit)

Activation Functions
Medium

Implement the Gaussian Error Linear Unit (GELU) activation function. It smoothly combines the behavior of linear and nonlinear activations by weighting each input x by the probability that a Gaussian random variable is less than x.

Mathematical Definition

GELU(x)=12x(1+erf(x2))\text{GELU}(x) = \frac{1}{2} x \left(1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right)
Loading visualization...

Examples

Input:

x = [-1.0, 0.0, 1.0]

Output:

[-0.158655, 0.0, 0.841345]

Input:

x = [[-2., -1.],[0., 1.]]

Output:

[[-0.045500, -0.158655],

[ 0.000000, 0.841345]]

Hint 1

Convert input to NumPy array first.

Hint 2

Use np.vectorize(math.erf) to make the error function work with arrays.

Requirements

  • Must handle scalars, lists, and NumPy arrays
  • Must be fully vectorized (no loops)
  • Return a NumPy array of floats

Constraints

  • Input up to size 1e6
  • Time limit: 200 ms; Memory: 64 MB
Try Similar Problems