# Rectifier (neural networks)

In the context of artificial neural networks, the rectifier or ReLU (rectified linear unit) activation function is an activation function defined as the non-negative part of its argument, i.e., the ramp function:
    $\operatorname {ReLU} (x)=x^{+}=\max(0,x)={\frac {x+|x|}{2}}={\begin{cases}x&{\text{if }}x>0,\\0&x\leq 0\end{cases}}$
where $x$ is the input to a neuron. This is analogous to half-wave rectification in electrical engineering.
ReLU is one of the most popular activation functions for artificial neural networks, and finds application in computer vision and speech recognition using deep neural nets and computational neuroscience.
It was first used by Alston Householder in 1941 as a mathematical abstraction of biological neural networks. It was introduced by Kunihiko Fukushima in 1969 in the context of visual feature extraction in hierarchical neural networks. It was later argued that it has strong biological motivations and mathematical justifications. In 2011, ReLU activation enabled training deep supervised neural networks without unsupervised pre-training, compared to the widely used activation functions prior to 2011, e.g., the logistic sigmoid (which is inspired by probability theory; see logistic regression) and its more practical counterpart, the hyperbolic tangent.

## Advantages

Advantages of ReLU include:
* Sparse activation: for example, in a randomly initialized network, only about 50% of hidden units are activated (i.e. have a non-zero output).
* Better gradient propagation: fewer vanishing gradient problems compared to sigmoidal activation functions that saturate in both directions.
* Efficiency: only requires comparison and addition.
* Scale-invariant (homogeneous):
    $\max(0,ax)=a\max(0,x){\text{ for }}a\geq 0$.

## Potential problems

Possible downsides can include:
* Non-differentiability at zero (however, it is differentiable anywhere else, and the value of the derivative at zero can be chosen to be 0 or 1 arbitrarily).
* Not zero-centered: ReLU outputs are always non-negative. This can make it harder for the network to learn during backpropagation, because gradient updates tend to push weights in one direction (positive or negative). Batch normalization can help address this.
* ReLU is unbounded.
* Dying ReLU: ReLU neurons can sometimes be pushed into states in which they become inactive for essentially all inputs. In this state, no gradients flow backward through the neuron, and so the neuron becomes stuck in a perpetually inactive state (it "dies"). This is a form of the vanishing gradient problem. In some cases, large numbers of neurons in a network can become stuck in dead states, effectively decreasing the model capacity and potentially even halting the learning process. This problem typically arises when the learning rate is set too high. It may be mitigated by using "leaky" ReLU instead, where a small positive slope is assigned for $x<0$. However, depending on the task, performance may be reduced.

## Variants
### Piecewise-linear variants

Leaky ReLU allows a small, positive gradient when the unit is inactive, helping to mitigate the vanishing gradient problem. This gradient is defined by a parameter $\alpha$, typically set to 0.01–0.3.
    $f(x)={\begin{cases}x&x>0,\\\alpha x&x\leq 0,\end{cases}}\qquad f'(x)={\begin{cases}1&x>0,\\\alpha &x\leq 0.\end{cases}}$
The same function can also be expressed without the piecewise notation as:
    $f(x)={\frac {1+\alpha }{2}}x+{\frac {1-\alpha }{2}}|x|$
Parametric ReLU (PReLU) takes this idea further by making $\alpha$ a learnable parameter along with the other network parameters.
Note that for $\alpha \leq 1$, this is equivalent to
    $f(x)=\max(x,\alpha x)$
and thus has a relation to "maxout" networks.
Concatenated ReLU (CReLU) preserves positive and negative phase information:
    $f(x)=[\operatorname {ReLU} (x),\operatorname {ReLU} (-x)].$

### Other non-linear variants
#### Gaussian-error linear unit (GELU)

GELU is a smooth approximation to the rectifier:
    $f(x)=x\Phi (x),$
    $f'(x)=x\Phi '(x)+\Phi (x)$
where $\Phi (x)=P(X\leqslant x)$ is the cumulative distribution function of the standard normal distribution.
This activation function is illustrated in the figure at the start of this article. It has a "bump" to the left of x < 0 and serves as the default activation for models such as BERT.

#### SiLU

The SiLU (sigmoid linear unit) or swish function is another smooth approximation which uses the sigmoid function, first introduced in the GELU paper:
    $f(x)=x\cdot \operatorname {sigmoid} (x),$
    $f'(x)=x\cdot \operatorname {sigmoid} '(x)+\operatorname {sigmoid} (x)$

#### Softplus

A smooth approximation to the rectifier is the analytic function
    $f(x)=\ln(1+e^{x}),\qquad f'(x)={\frac {e^{x}}{1+e^{x}}}={\frac {1}{1+e^{-x}}}$
which is called the softplus or SmoothReLU function. For large negative $x$ it is roughly $\ln 1$, so just above 0, while for large positive $x$ it is roughly $\ln(e^{x})$, so just above $x$.
This function can be approximated as:
    $\ln \left(1+e^{x}\right)\approx {\begin{cases}\ln 2,&x=0,\\[6pt]{\frac {x}{1-e^{-x/\ln 2}}},&x\neq 0\end{cases}}$
By making the change of variables $x=y\ln(2)$, this is equivalent to
    $\log _{2}(1+2^{y})\approx {\begin{cases}1,&y=0,\\[6pt]{\frac {y}{1-e^{-y}}},&y\neq 0\end{cases}}$
A sharpness parameter $k$ may be included:
    $f(x)={\frac {\ln(1+e^{kx})}{k}},\qquad f'(x)={\frac {e^{kx}}{1+e^{kx}}}={\frac {1}{1+e^{-kx}}}$
The derivative of softplus is the logistic function.
The logistic sigmoid function is a smooth approximation of the derivative of the rectifier, the Heaviside step function.
The multivariable generalization of single-variable softplus is the LogSumExp with the first argument set to zero:
    $\operatorname {LSE_{0}} ^{+}(x_{1},\dots ,x_{n}):=\operatorname {LSE} (0,x_{1},\dots ,x_{n})=\ln(1+e^{x_{1}}+\cdots +e^{x_{n}})$
The LogSumExp function is
    $\operatorname {LSE} (x_{1},\dots ,x_{n})=\ln(e^{x_{1}}+\cdots +e^{x_{n}})$
and its gradient is the softmax; the softmax with the first argument set to zero is the multivariable generalization of the logistic function. Both LogSumExp and softmax are used in machine learning.

#### ELU

Exponential linear units try to make the mean activations closer to zero, which speeds up learning. It has been shown that ELUs can obtain higher classification accuracy than ReLUs.
    $f(x)={\begin{cases}x&x>0,\\\alpha \left(e^{x}-1\right)&x\leq 0\end{cases}}\qquad f'(x)={\begin{cases}1&x>0,\\\alpha e^{x}&x\leq 0\end{cases}}$
In these formulas, $\alpha$ is a hyperparameter to be tuned with the constraint $\alpha \geq 0$.
Given the same interpretation of $\alpha$, ELU can be viewed as a smoothed version of a shifted ReLU (SReLU), which has the form $f(x)=\max(-\alpha ,x)$.

#### Mish

The mish function can also be used as a smooth approximation of the rectifier. It is defined as
    $f(x)=x\tanh {\big\operatorname {softplus} (x){\big )},$
where $\tanh(x)$ is the hyperbolic tangent, and $\operatorname {softplus} (x)$ is the softplus function.
Mish is non-monotonic and self-gated. It was inspired by Swish, itself a variant of ReLU.

#### Squareplus

Squareplus is the function
    $f(x)={\frac {x+{\sqrt {x^{2}+b}}}{2}}$
where $b\geq 0$ is a hyperparameter that determines the "size" of the curved region near $x=0$. (For example, letting $b=0$ yields ReLU, and letting $b=4$ yields the metallic mean function.) Squareplus shares many properties with softplus: It is monotonic, strictly positive, approaches 0 as $x\to -\infty$, approaches the identity as $x\to +\infty$, and is $C^{\infty }$ smooth. However, squareplus can be computed using only algebraic functions, making it well-suited for settings where computational resources or instruction sets are limited. Additionally, squareplus requires no special consideration to ensure numerical stability when $x$ is large.