SELU activationEasy

SELU activation

Background

SELU (Scaled Exponential Linear Unit) is a self-normalizing activation: with the right weight init (LeCun normal) it drives layer activations toward zero mean and unit variance automatically, removing the need for batch norm in fully-connected nets. It is an ELU scaled by a fixed factor with precisely chosen constants.

Problem statement

Implement selu(x):

SELU(x)=λ{xx>0α(ex1)x0\text{SELU}(x) = \lambda \begin{cases} x & x > 0 \\ \alpha\,(e^{x} - 1) & x \le 0 \end{cases}

with the fixed constants λ1.0507\lambda \approx 1.0507 and α1.6733\alpha \approx 1.6733.

Input

  • xnp.ndarray: input (any shape).

Output

Returns an np.ndarray of the same shape.

Examples

Example 1

Input:  x = [-1, 0, 1, 2]
Output: [-1.1113, 0.0, 1.0507, 2.1014]

Explanation: for x>0x>0, SELU =λx=\lambda x (so 11.05071\to1.0507, 22.10142\to2.1014); for x0x\le0, λα(ex1)\lambda\alpha(e^x-1) (so 11.05071.6733(e11)1.1113-1\to1.0507\cdot1.6733\cdot(e^{-1}-1)\approx-1.1113); and 000\to0.

Constraints

  • Use the exact constants λ=1.0507009873554805\lambda = 1.0507009873554805 and α=1.6732632423543772\alpha = 1.6732632423543772.
  • Positive branch: λx\lambda x; non-positive branch: λα(ex1)\lambda\alpha(e^x - 1).
  • Elementwise; tests compare with atol=1e-4.

Notes

  • The constants are derived (Klambauer et al., 2017) so the activation is a fixed point of mean/variance propagation — the "self-normalizing" property.
  • SELU pairs with LeCun-normal init and "alpha dropout"; outside that recipe its self-normalizing guarantee doesn't hold.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Reference values
  • Positive branch is lambda * x
  • selu(0) = 0
  • Negative branch saturates near -lambda*alpha