Skip to content
class

ReLU

extends_InplaceActivation
ReLU(inplace: bool = False)

Rectified Linear Unit activation function.

Applies element-wise:

ReLU(x)=max⁡(0, x)\text{ReLU}(x) = \max(0,\, x)

The simplest non-linear activation — sets all negative values to zero. Commonly used in hidden layers of deep networks because it avoids the vanishing-gradient problem that affects sigmoid and tanh for large inputs, and is inexpensive to compute.

Parameters

inplacebool= False
If True, the input tensor itself receives the result. Default: False.

Notes

  • Input: (∗)(*) — any shape.
  • Output: (∗)(*) — same shape as input.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.ReLU()
>>> x = lucid.tensor([-1.0, 0.0, 1.0, 2.0])
>>> m(x)
tensor([0., 0., 1., 2.])
>>> # In place: the input itself holds the result
>>> x = lucid.tensor([-1.0, 2.0])
>>> nn.ReLU(inplace=True)(x) is x, x
(True, tensor([0., 2.]))

Used by 1

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.