Sign in

Softplus

Softplus function

A smooth version of ReLU: it is close to zero for negative inputs, close to the input for positive ones, and always positive.

Overview

ReLU has a sharp corner at zero. Softplus rounds that corner. For very negative inputs it is nearly zero, for very positive inputs it nearly equals the input, and around zero it curves gently from one regime to the other. Its output is always strictly positive, which makes it a convenient way to force a quantity such as a standard deviation or a rate to be positive. Its slope is the sigmoid, so it never drops to exactly zero. The parameter β\beta sets how tight the bend is.

A short history

Charles Dugas, Yoshua Bengio and colleagues introduced softplus in 2000 in a paper on pricing financial options with neural networks, where they needed outputs that were positive and smooth. It is a smooth approximation of the rectifier, and its derivative is the logistic sigmoid. ReLU proved faster and at least as accurate for hidden layers, so softplus is now used mostly where an output must be strictly positive.

How it works

  1. Take the input xx and multiply it by β\beta.
  2. Compute ln⁡(1+eβx)\ln(1 + e^{\beta x}). The template uses logaddexp, which cannot overflow.
  3. Divide by β\beta so that the curve still follows xx for large inputs.
  4. During training, multiply the incoming gradient by σ(βx)\sigma(\beta x).

Watch out for

  • The output is never exactly zero, so activations are not sparse the way ReLU's are, and a head that should be able to output zero cannot.
  • At x=0x = 0 the output is ln⁡2/β\ln 2 / \beta, not 0. Every unit carries a positive offset that shifts the mean of the layer.
  • For large positive inputs the naive formula overflows even though the answer is just xx. Use a stable form such as logaddexp.
  • For very negative inputs the slope σ(βx)\sigma(\beta x) is tiny, so units can still learn very slowly there.

Use cases

When to use it

Output heads that must be positive, such as a predicted variance, scale or rate, and places where ReLU's corner or dead region is a problem.

In industry

Forecasting with uncertainty
Probabilistic forecasting models pass a raw output through softplus to produce a standard deviation or rate, which must be positive.
Finance
The original application was option pricing, where the network output had to be positive and smooth in its inputs.
Generative models
Variational autoencoders and normalising flows use softplus to turn unconstrained network outputs into positive scale parameters.

Choosing it: what must hold

  • A quantity that must be strictly positive, such as a variance, a rate or a scale.
  • A need for a smooth function with a gradient that is never exactly zero.
  • A stable implementation that switches to the identity for large inputs to avoid overflow.

When to choose something else

IfUse instead
You need a cheap hidden layer activationReLU
The positive quantity spans many orders of magnitudeAn exponential output, with care for large values
You want a smooth hidden activation in a deep networkSiLU or GELU

Math foundations

  1. The function

    softplus⁡β(x)=1β ln⁡ ⁣(1+eβx)\operatorname{softplus}_\beta(x) = \frac{1}{\beta}\,\ln\!\big(1 + e^{\beta x}\big)

    A smooth curve between the two straight pieces of ReLU. With β=1\beta = 1 it is the standard softplus.

  2. Its slope is the sigmoid

    ddxsoftplus⁡β(x)=eβx1+eβx=σ(βx)\frac{d}{dx}\operatorname{softplus}_\beta(x) = \frac{e^{\beta x}}{1 + e^{\beta x}} = \sigma(\beta x)

    The slope rises smoothly from 0 to 1 and is 0.5 at the origin. Softplus is therefore the integral of the sigmoid.

  3. How far it sits above ReLU

    max⁡(0,x)  <  softplus⁡β(x)  ≤  max⁡(0,x)+ln⁡2β\max(0, x) \;<\; \operatorname{softplus}_\beta(x) \;\le\; \max(0, x) + \frac{\ln 2}{\beta}

    Softplus is always above ReLU, and the gap is largest at x=0x = 0, where it equals ln⁡2/β\ln 2 / \beta. A larger β\beta shrinks the gap.

  4. The ReLU limit

    lim⁡β→∞softplus⁡β(x)=max⁡(0,x)\lim_{\beta \to \infty} \operatorname{softplus}_\beta(x) = \max(0, x)

    As β\beta grows the bend tightens and the curve converges to ReLU everywhere.

Symbols

xx
the input to the activation
β\beta
sharpness, beta in the config, default 1
σ\sigma
the logistic sigmoid
ln⁡\ln
the natural logarithm

Worked by hand

Compute softplus and its slope at x=0x = 0 and x=2x = 2 with β=1\beta = 1. Use e2=7.389e^{2} = 7.389.

softplus⁡(0)=ln⁡(1+1)=0.6931,σ(0)=0.5\operatorname{softplus}(0) = \ln(1 + 1) = 0.6931, \qquad \sigma(0) = 0.5
softplus⁡(2)=ln⁡(1+7.389)=ln⁡8.389=2.1269\operatorname{softplus}(2) = \ln(1 + 7.389) = \ln 8.389 = 2.1269
softplus⁡′(2)=7.3898.389=0.8808\operatorname{softplus}'(2) = \frac{7.389}{8.389} = 0.8808

At 0 the output is 0.6931 where ReLU gives 0. At 2 it is 2.1269, only 0.1269 above ReLU, and the slope is already 0.8808.

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 Softplus
from config import SoftplusConfig

act = Softplus(SoftplusConfig(beta=1.0))
y = act.forward(np.array([-2.0, 0.0, 2.0]))      # [0.1269, 0.6931, 2.1269]
dy = act.backward(np.array([-2.0, 0.0, 2.0]))    # sigmoid: [0.1192, 0.5, 0.8808]
Read the implementation (40 lines)
"""Softplus: f(x) = log(1 + exp(beta * x)) / beta.

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

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 Softplus:
    """f(x) = log(1 + exp(beta * x)) / beta, always positive."""

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

    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)
        # logaddexp(0, z) = log(exp(0) + exp(z)) is computed without overflow for any z
        return np.logaddexp(0.0, self.config.beta * x) / self.config.beta

    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) = sigmoid(beta * x).
        """
        x = np.asarray(x, dtype=float)
        local = _sigmoid(self.config.beta * x)
        return local if grad_output is None else local * np.asarray(grad_output, dtype=float)

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

Practice

Build Softplus(SoftplusConfig(beta=1.0)) and tabulate forward(x) minus max(0, x) on 201 points between -10 and 10. Confirm the largest gap is ln⁡2\ln 2 at zero. Repeat with beta=5.0 and compare the gap with ln⁡2/5\ln 2 / 5.

Check on learning

  1. A network must predict a standard deviation. Compare softplus with the exponential function as the final step. How does each respond to a large positive pre-activation?
    A way to think about it

    The exponential grows without limit in slope, so one bad step can produce a huge value. Softplus grows linearly. Think about which is easier to optimise.

  2. ReLU outputs exact zeros and softplus never does. In what situation is that a drawback of softplus, and in what situation an advantage?
    A way to think about it

    Exact zeros give sparse activations and cheap storage. Never-zero slope means no unit is ever fully dead. Decide which matters for the layer in question.

  3. Softplus at zero equals ln⁡2/β\ln 2 / \beta. How would you remove that offset, and what would the shifted function look like for negative inputs?
    A way to think about it

    Subtract the constant. The curve then passes through the origin and settles at a small negative floor, much like another activation in this family.

  4. The slope of softplus is the sigmoid. What does that tell you about the second derivative, and why might optimisers that use curvature prefer softplus to ReLU?
    A way to think about it

    Differentiate the sigmoid: the curvature is positive and smooth everywhere. ReLU's curvature is zero except for a spike at the corner.