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 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
- Take the input and multiply it by .
- Compute . The template uses
logaddexp, which cannot overflow. - Divide by so that the curve still follows for large inputs.
- During training, multiply the incoming gradient by .
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 the output is , 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 . Use a stable form such as
logaddexp. - For very negative inputs the slope 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
| If | Use instead |
|---|---|
| You need a cheap hidden layer activation | ReLU |
| The positive quantity spans many orders of magnitude | An exponential output, with care for large values |
| You want a smooth hidden activation in a deep network | SiLU or GELU |
Math foundations
The function
A smooth curve between the two straight pieces of ReLU. With it is the standard softplus.
Its slope is the sigmoid
The slope rises smoothly from 0 to 1 and is 0.5 at the origin. Softplus is therefore the integral of the sigmoid.
How far it sits above ReLU
Softplus is always above ReLU, and the gap is largest at , where it equals . A larger shrinks the gap.
The ReLU limit
As grows the bend tightens and the curve converges to ReLU everywhere.
Symbols
- the input to the activation
- sharpness,
betain the config, default 1 - the logistic sigmoid
- the natural logarithm
Worked by hand
Compute softplus and its slope at and with . Use .
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 at zero. Repeat with beta=5.0 and compare the gap with .
Check on learning
- 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.
- 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.
- Softplus at zero equals . 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.
- 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.