PyTorch torch.nn.ELU function
PyTorch torch.nn Reference Manual
torch.nn.ELU is the Exponential Linear Unit activation function in PyTorch.
It uses an exponential function for negative values, allowing non-zero outputs for negative inputs and producing smoother gradients.
Function definition
torch.nn.ELU(alpha=1.0, inplace=False)
Formula
ELU(x) = x, x > 0
ELU(x) = alpha * (e^x - 1), x <= 0
Usage examples
Example 1: Basic usage
Example
import
torch
import
torch.
nn
as
nn
elu
=
nn.
ELU
(
alpha
=
1.0
)
x
=
torch.
tensor
(
[
2.0
,
1.0
,
0.0
,
1.0
,
2.0
]
)
output
=
elu
(
x
)
(
"Input:"
,
x.
tolist
(
)
)
(
"Output:"
,
output.
tolist
(
)
)
Example 2: Comparing with ReLU
Example
import
torch
import
torch.
nn
as
nn
import
numpy
as
np
x
=
np.
array
(
[
3.0
,
2.0
,
1.0
,
0.0
,
1.0
,
2.0
]
)
x_t
=
torch.
tensor
(
x
)
(
"x ELU ReLU"
)
for
i
in
range
(
0
,
6
,
1
)
:
(
f
"{x[i]:6.1f} {nn.ELU()(x_t[i:i+1]).item():9.4f} {nn.ReLU()(x_t[i:i+1]).item():9.4f}"
)
Example 3: The alpha parameter
Example
import
torch
import
torch.
nn
as
nn
Different alpha values
x
=
torch.
tensor
(
[
1.0
]
)
for
alpha
in
[
0.5
,
1.0
,
1.5
,
2.0
]
:
out
=
nn.
ELU
(
alpha
=
alpha
)
(
x
)
(
f
"alpha={alpha}: {out.item():.4f}"
)
Use Cases
- Autoencoders
- Need negative outputs
- Smoother gradients
Tip: ELU is slower to compute than ReLU, but converges faster.