Sign in

SiLU (Swish)

Sigmoid Linear Unit (Swish)

An activation that multiplies each input by its own sigmoid, giving a smooth ReLU-like curve with a small negative dip.

Overview

SiLU, also called Swish, gates each input by a factor the input itself decides. The factor is the sigmoid of the input, a number between 0 and 1. A large positive input has a factor near 1 and passes almost unchanged. A large negative input has a factor near 0 and is almost removed. Near zero the factor is about one half. The result looks like ReLU with the corner rounded off and a shallow dip below zero. The parameter β\beta controls how sharp the rounding is.

A short history

Dan Hendrycks and Kevin Gimpel mentioned the sigmoid linear unit in their 2016 GELU paper. Stefan Elfwing, Eiji Uchibe and Kenji Doya studied it for reinforcement learning in 2017. In the same year Prajit Ramachandran, Barret Zoph and Quoc Le found the same function through an automated search over activations and named it Swish. Their results showed it could replace ReLU in deep image models with a small gain in accuracy.

How it works

  1. Take the input xx that a layer has computed for one unit.
  2. Compute the gate s=σ(βx)s = \sigma(\beta x), a number between 0 and 1.
  3. Multiply: the output is x⋅sx \cdot s.
  4. During training, multiply the incoming gradient by s+βx s (1−s)s + \beta x\,s\,(1 - s).

Watch out for

  • The output dips to about −0.28-0.28 near x=−1.28x = -1.28 when β=1\beta = 1, so the function is not monotonic and outputs are not non-negative.
  • It costs one exponential per element, more than ReLU. Mobile models sometimes use a piecewise-linear stand-in called hard swish for that reason.
  • In the template β\beta is a fixed setting. The original Swish paper also tried learning it, which this small version does not do.

Use cases

When to use it

Modern convolutional networks such as EfficientNet, diffusion models, and the gate inside SwiGLU feed-forward blocks, where a smooth activation trains better than ReLU.

In industry

Computer vision
The EfficientNet family of image classifiers uses Swish throughout, and later YOLO detectors use SiLU in their convolution blocks.
Image generation
The U-Net denoisers in diffusion models use SiLU in their residual blocks and time embedding layers.
Language models
SiLU is the gating function inside the SwiGLU feed-forward blocks of several recent large language models.

Choosing it: what must hold

  • A deep network with normalisation layers, where a smooth activation helps optimisation.
  • Compute for a sigmoid per value, which costs more than ReLU.
  • On mobile hardware, support for the function or a willingness to use the piecewise hard swish approximation.

When to choose something else

IfUse instead
Latency or low precision arithmetic is the constraintReLU, or ReLU6 on mobile
The model is a transformer with a plain feed-forward blockGELU
You are building a gated feed-forward blockSwiGLU

Math foundations

  1. The sigmoid gate

    σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

    Maps any number to the range 0 to 1. Here it decides what share of the input to keep.

  2. The activation

    SiLU⁡β(x)=x σ(βx)\operatorname{SiLU}_\beta(x) = x\,\sigma(\beta x)

    The input scaled by its own gate. With β=1\beta = 1 this is SiLU. With general β\beta it is Swish.

  3. Its slope

    ddxSiLU⁡β(x)=s+βx s (1−s),s=σ(βx)\frac{d}{dx}\operatorname{SiLU}_\beta(x) = s + \beta x\,s\,(1 - s), \qquad s = \sigma(\beta x)

    From the product rule, since the derivative of the sigmoid is s(1−s)s(1 - s). At x=0x = 0 the slope is exactly 0.5.

  4. The two limits

    lim⁡β→∞x σ(βx)=max⁡(0,x),lim⁡β→0x σ(βx)=x2\lim_{\beta \to \infty} x\,\sigma(\beta x) = \max(0, x), \qquad \lim_{\beta \to 0} x\,\sigma(\beta x) = \frac{x}{2}

    A very large β\beta makes the gate a hard switch and recovers ReLU. A very small β\beta fixes the gate at one half and gives a linear function. SiLU sits between.

Symbols

xx
the input to the activation (one unit's pre-activation)
β\beta
sharpness of the gate, beta in the config, default 1
σ\sigma
the logistic sigmoid
ss
the gate value, sigmoid of beta times x

Worked by hand

Compute SiLU and its slope at x=1x = 1 with β=1\beta = 1. Use σ(1)=0.7311\sigma(1) = 0.7311.

SiLU⁡(1)=1×0.7311=0.7311\operatorname{SiLU}(1) = 1 \times 0.7311 = 0.7311
s (1−s)=0.7311×0.2689=0.1966s\,(1 - s) = 0.7311 \times 0.2689 = 0.1966
SiLU⁡′(1)=0.7311+1×0.1966=0.9277\operatorname{SiLU}'(1) = 0.7311 + 1 \times 0.1966 = 0.9277

The output is 0.7311 and the slope is 0.9277. ReLU would give 1 and 1, and GELU gives 0.8413, so SiLU is the gentlest of the three at this input.

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 SiLU
from config import SiLUConfig

act = SiLU(SiLUConfig(beta=1.0))
y = act.forward(np.array([-1.0, 0.0, 1.0]))      # [-0.2689, 0.0, 0.7311]
dy = act.backward(np.array([-1.0, 0.0, 1.0]))    # [0.0723, 0.5, 0.9277]
Read the implementation (40 lines)
"""SiLU / Swish: f(x) = x * sigmoid(beta * x).

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 SiLUConfig
except ImportError:  # run from the template's own folder
    from config import SiLUConfig

def _sigmoid(x: np.ndarray) -> np.ndarray:
    """Logistic function 1 / (1 + exp(-x)), written so that large |x| cannot overflow."""
    return np.exp(-np.logaddexp(0.0, -x))


class SiLU:
    """f(x) = x * sigmoid(beta * x)."""

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

    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)
        return x * _sigmoid(self.config.beta * x)

    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).

        With s = sigmoid(beta * x): f'(x) = s + beta * x * s * (1 - s).
        """
        x = np.asarray(x, dtype=float)
        s = _sigmoid(self.config.beta * x)
        local = s + self.config.beta * x * s * (1.0 - s)
        return local if grad_output is None else local * np.asarray(grad_output, dtype=float)

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

Practice

Build SiLU(SiLUConfig(beta=1.0)) and find the minimum of forward on 2001 points between -5 and 5. Then repeat with beta of 0.1 and 10 and compare each curve with x/2x/2 and with ReLU.

Check on learning

  1. As β\beta grows the curve approaches ReLU. What is lost and what is gained along the way?
    A way to think about it

    Track two things as β\beta rises: the width of the smooth region near zero and the depth of the negative dip. Both shrink toward ReLU's corner.

  2. GELU is well approximated by x σ(1.702 x)x\,\sigma(1.702\,x). What does that tell you about choosing between GELU and SiLU?
    A way to think about it

    That approximation is Swish with a particular β\beta. The two are members of one family with slightly different sharpness, so expect small differences.

  3. ReLU has zero slope for all negative inputs. SiLU has a small negative slope for inputs below about −1.28-1.28. Which is more likely to leave a unit stuck?
    A way to think about it

    A unit is stuck when it gets no gradient. Compare exactly zero with small and nonzero, and consider that SiLU's slope tends to zero only far out in the tail.

  4. Why might a quantised mobile model replace SiLU with a piecewise-linear imitation?
    A way to think about it

    Think about what an exponential costs in integer arithmetic, and how well straight segments can follow a curve whose interesting part is a narrow band.