Sign in

SELU

Scaled Exponential Linear Unit

A scaled ELU with two fixed constants chosen so that activations drift toward mean 0 and variance 1 from layer to layer.

Overview

Deep networks train poorly when activations grow or shrink as they pass through the layers. The usual fix is a normalisation layer. SELU takes a different route: it is an ELU multiplied by a constant slightly above 1, with a specific negative floor. The two constants were solved for so that, when the inputs to a layer have mean 0 and variance 1 and the weights are initialised to match, the outputs have mean 0 and variance 1 as well. Small deviations are pulled back toward that point, so a plain stack of dense layers normalises itself.

A short history

Günter Klambauer, Thomas Unterthiner, Andreas Mayr and Sepp Hochreiter introduced SELU in the 2017 paper 'Self-Normalizing Neural Networks'. They derived two fixed constants for which the mean and variance of activations are drawn toward 0 and 1 as signals pass through layers. This let plain feed-forward networks be trained many layers deep without batch normalisation.

How it works

  1. Take the input xx that a layer has computed for one unit.
  2. If x>0x > 0, return λx\lambda x.
  3. If x≤0x \le 0, return λα(ex−1)\lambda \alpha (e^{x} - 1), which approaches −λα≈−1.7581-\lambda\alpha \approx -1.7581.
  4. During training, multiply the incoming gradient by λ\lambda on the positive side and by λαex\lambda \alpha e^{x} on the negative side.
  5. Pair it with weights of mean 0 and variance 1/n1/n, where nn is the number of inputs to the unit, so the self-normalising property holds.

Watch out for

  • The self-normalising guarantee assumes weights initialised with mean 0 and variance 1/n1/n (LeCun normal). Other initialisations break it, and activations drift.
  • Ordinary dropout shifts the mean and variance and undoes the effect. The matching technique is alpha dropout, which the template does not include.
  • The property was derived for plain dense layers. With convolutions, skip connections or attention it is not guaranteed.
  • The constants alpha and scale are configurable in the template, but changing either one gives a scaled ELU without the fixed point.

Use cases

When to use it

Deep fully connected networks, such as tabular models, where you want stable activations without batch normalisation.

In industry

Drug discovery
The original paper applied self-normalising networks to predicting the toxicity of chemical compounds from molecular descriptors.
Tabular prediction
Deep fully connected networks on rows of numeric features use SELU when batch normalisation is awkward, for example with very small batches.
Scientific computing
Researchers fitting deep fully connected surrogate models to simulation outputs use SELU to keep signals stable across many layers.

Choosing it: what must hold

  • A plain stack of fully connected layers, with no batch or layer normalisation in between.
  • LeCun normal weight initialisation and inputs standardised to mean 0 and variance 1.
  • Alpha dropout in place of ordinary dropout, since ordinary dropout breaks the self-normalising property.

When to choose something else

IfUse instead
The network has convolutions, skip connections or normalisation layersReLU or ELU with normalisation
You cannot standardise the inputs or control the initialisationELU
The model is a transformerGELU

Math foundations

  1. The activation

    SELU⁡(x)=λ{x,x>0α (ex−1),x≤0\operatorname{SELU}(x) = \lambda \begin{cases} x, & x > 0 \\ \alpha\,(e^{x} - 1), & x \le 0 \end{cases}

    An ELU with floor −α-\alpha, multiplied by λ\lambda. The scale above 1 lets the unit increase variance when it is too low, which ELU alone cannot do.

  2. The two constants

    α≈1.6733,λ≈1.0507\alpha \approx 1.6733, \qquad \lambda \approx 1.0507

    Solved for by requiring that an input with mean 0 and variance 1 gives an output with mean 0 and variance 1. They are the template's defaults for alpha and scale.

  3. Its slope

    ddxSELU⁡(x)=λ{1,x>0α ex,x≤0\frac{d}{dx}\operatorname{SELU}(x) = \lambda \begin{cases} 1, & x > 0 \\ \alpha\,e^{x}, & x \le 0 \end{cases}

    The factor backpropagation multiplies by. On the positive side it is slightly above 1. Just left of zero it is λα≈1.7581\lambda\alpha \approx 1.7581, so the slope jumps at the origin.

  4. The condition it is built to keep

    E[z]=0,    Var⁡[z]=1  ⟹  E[SELU⁡(z)]=0,    Var⁡[SELU⁡(z)]=1\mathbb{E}[z] = 0,\;\; \operatorname{Var}[z] = 1 \;\Longrightarrow\; \mathbb{E}[\operatorname{SELU}(z)] = 0,\;\; \operatorname{Var}[\operatorname{SELU}(z)] = 1

    For a pre-activation zz that is standard normal, the output has the same mean and variance. With correctly scaled weights the next layer's pre-activation is again close to standard normal, so the pattern repeats.

Symbols

x,zx, z
the input to the activation (one unit's pre-activation)
α\alpha
the negative-side constant, alpha in the config, about 1.6733
λ\lambda
the output scale, scale in the config, about 1.0507
E,Var⁡\mathbb{E}, \operatorname{Var}
mean and variance over the inputs a unit sees

Worked by hand

Compute SELU at x=1x = 1 and x=−1x = -1 with the default constants. Use e−1=0.3679e^{-1} = 0.3679 and λα=1.7581\lambda\alpha = 1.7581.

SELU⁡(1)=1.0507×1=1.0507\operatorname{SELU}(1) = 1.0507 \times 1 = 1.0507
SELU⁡(−1)=1.7581×(0.3679−1)=−1.1113\operatorname{SELU}(-1) = 1.7581 \times (0.3679 - 1) = -1.1113
SELU⁡′(−1)=1.7581×0.3679=0.6468\operatorname{SELU}'(-1) = 1.7581 \times 0.3679 = 0.6468

The positive input is stretched a little and the negative input maps to −1.1113-1.1113. Negative outputs are larger in size than ELU's −0.6321-0.6321, which is what balances the mean.

Visual example

Loading the example.

Try this

  • Move across the curve and read the output and the slope together.
  • Find where the slope is close to zero: a unit there has stopped learning.

Code sample

import numpy as np
from impl import SELU
from config import SELUConfig

act = SELU(SELUConfig())
y = act.forward(np.array([-1.0, 0.0, 2.0]))      # [-1.1113, 0.0, 2.1014]
dy = act.backward(np.array([-1.0, 0.0, 2.0]))    # gradient with respect to the input
Read the implementation (36 lines)
"""SELU: scaled exponential linear unit, f(x) = scale * (x if x > 0 else alpha * (exp(x) - 1)).

NumPy only. `forward` computes the activation, `backward` the gradient with respect to the input.
"""
from __future__ import annotations

import numpy as np

try:
    from .config import SELUConfig
except ImportError:  # run from the template's own folder
    from config import SELUConfig

class SELU:
    """f(x) = scale * x for x > 0, scale * alpha * (exp(x) - 1) for x <= 0."""

    def __init__(self, config: SELUConfig | None = None) -> None:
        self.config = config or SELUConfig()

    def forward(self, x: np.ndarray) -> np.ndarray:
        """Apply the activation element by element. x: any shape. Returns the same shape."""
        x = np.asarray(x, dtype=float)
        negative = self.config.alpha * np.expm1(np.minimum(x, 0.0))  # the minimum keeps exp from overflowing
        return self.config.scale * np.where(x > 0, x, negative)

    def backward(self, x: np.ndarray, grad_output: np.ndarray | None = None) -> np.ndarray:
        """d f / d x at x, times `grad_output` when given (the chain rule step of backpropagation).

        f'(x) = scale for x > 0 and scale * alpha * exp(x) for x <= 0.
        """
        x = np.asarray(x, dtype=float)
        negative = self.config.alpha * np.exp(np.minimum(x, 0.0))
        local = self.config.scale * np.where(x > 0, 1.0, negative)
        return local if grad_output is None else local * np.asarray(grad_output, dtype=float)

    __call__ = forward
Repository
gitlab.com/designsbyduhart/solomon-templates/activation-selu
Needs
Python 3.10 and NumPy
Tests
8, run with python3 -m unittest -v

Practice

Build SELU(SELUConfig()), draw 100000 standard normal inputs and report the mean and variance of forward. Both should be close to 0 and 1. Repeat with inputs of standard deviation 2 and see how far they move, then with scale=1.0.

Check on learning

  1. The scale λ\lambda is above 1. Why does a self-normalising activation need a slope greater than 1 somewhere?
    A way to think about it

    Think about a layer whose variance is too low. To pull it back up toward 1, some region must stretch its inputs. A function with slope at most 1 everywhere can only shrink.

  2. You add standard dropout to a SELU network and training becomes unstable. Why, and what does alpha dropout do differently?
    A way to think about it

    Dropout sets units to 0. Zero is not the resting value of SELU, whose negative floor is about −1.7581-1.7581. Consider what value a dropped unit should take to keep the mean and variance.

  3. The fixed point assumes each pre-activation is a sum of many independent inputs. Which common architecture choices violate that?
    A way to think about it

    List what adds correlated or unscaled terms: skip connections, shared convolution filters, attention weights. Ask whether the sum still has variance 1.

  4. How would you check on a real network whether self-normalisation is working?
    A way to think about it

    Record the mean and variance of activations at each layer for a batch. A working network keeps them near 0 and 1 at every depth.