TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement GELU Activation (Gaussian Error Linear Unit)

Activation Functions
Medium

Compute the exact Gaussian Error Linear Unit for every input value:

GELU⁡(x)=x2(1+erf⁡(x2))\operatorname{GELU}(x) = \frac{x}{2}\left(1 + \operatorname{erf}\left(\frac{x}{\sqrt{2}}\right)\right)GELU(x)=2x​(1+erf(2​x​))

Here, xxx is an input value and erf⁡\operatorname{erf}erf is the Gaussian error function. Apply the formula elementwise and return a NumPy array with the same shape as the input.

Loading visualization...

Examples

Input: x = [-1.0, 0.0, 1.0]

Output: [-0.158655, 0.0, 0.841345]

Explanation: The exact GELU formula scales negative values toward zero while retaining most of a positive value.

Input: x = [[-2.0, -1.0], [0.0, 1.0]]

Output: [[-0.0455, -0.158655], [0.0, 0.841345]]

Hint 1

Use np.asarray(x, dtype=float) before applying the formula.

Hint 2

Use np.vectorize(math.erf) to apply the scalar error function elementwise.

Requirements

  • Apply the exact error-function formula elementwise
  • Preserve the input shape
  • Return a NumPy array of floating-point values

Constraints

  • x is a nonempty finite numeric list of any depth
  • x contains at most 10610^6106 values
  • Use NumPy and the Python standard library only
Try Similar Problems
Relu ActivationSigmoid NumpyTanh ActivationSoftmax FunctionSwish Activation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Implement GELU Activation (Gaussian Error Linear Unit)

Activation Functions
Medium

Compute the exact Gaussian Error Linear Unit for every input value:

GELU⁡(x)=x2(1+erf⁡(x2))\operatorname{GELU}(x) = \frac{x}{2}\left(1 + \operatorname{erf}\left(\frac{x}{\sqrt{2}}\right)\right)GELU(x)=2x​(1+erf(2​x​))

Here, xxx is an input value and erf⁡\operatorname{erf}erf is the Gaussian error function. Apply the formula elementwise and return a NumPy array with the same shape as the input.

Loading visualization...

Examples

Input: x = [-1.0, 0.0, 1.0]

Output: [-0.158655, 0.0, 0.841345]

Explanation: The exact GELU formula scales negative values toward zero while retaining most of a positive value.

Input: x = [[-2.0, -1.0], [0.0, 1.0]]

Output: [[-0.0455, -0.158655], [0.0, 0.841345]]

Hint 1

Use np.asarray(x, dtype=float) before applying the formula.

Hint 2

Use np.vectorize(math.erf) to apply the scalar error function elementwise.

Requirements

  • Apply the exact error-function formula elementwise
  • Preserve the input shape
  • Return a NumPy array of floating-point values

Constraints

  • x is a nonempty finite numeric list of any depth
  • x contains at most 10610^6106 values
  • Use NumPy and the Python standard library only
Try Similar Problems
Relu ActivationSigmoid NumpyTanh ActivationSoftmax FunctionSwish Activation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.