PyTorch torch.nn.SiLU function
PyTorch torch.nn Reference Manual
torch.nn.SiLU is the Sigmoid Linear Unit activation function in PyTorch, also known as Swish.
It has self-gating properties, is smoother than ReLU, and performs better on some tasks.
Function definition
torch.nn.SiLU(inplace=False)
Mathematical principle
SiLU(x) = x * sigmoid(x)
Usage examples
Example 1: Basic usage
Example
import
torch
import
torch.
nn
as
nn
silu
=
nn.
SiLU
(
)
x
=
torch.
tensor
(
[
2.0
,
1.0
,
0.0
,
1.0
,
2.0
]
)
output
=
silu
(
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.
linspace
(
3
,
3
,
7
)
x_tensor
=
torch.
tensor
(
x
)
(
"x SiLU ReLU"
)
(
"-"
30
)
for
xi
in
x_tensor:
(
f
"{xi.item():5.1f} {nn.SiLU()(xi.unsqueeze(0)).item():9.4f} {nn.ReLU()(xi.unsqueeze(0)).item():9.4f}"
)
Example 3: Use in MobileNet
Example
import
torch
import
torch.
nn
as
nn
MobileNetV3 uses SiLU
model
=
nn.
Sequential
(
nn.
Conv2d
(
3
,
32
,
3
,
stride
=
2
,
padding
=
1
)
,
nn.
BatchNorm2d
(
32
)
,
nn.
SiLU
(
)
,
nn.
Conv2d
(
32
,
64
,
3
,
padding
=
1
)
,
nn.
BatchNorm2d
(
64
)
,
nn.
SiLU
(
)
)
x
=
torch.
randn
(
1
,
3
,
224
,
224
)
output
=
model
(
x
)
(
"Input:"
,
x.
shape
,
"-> Output:"
,
output.
shape
)
Use Cases
- MobileNet: MobileNetV3
- EfficientNet
- Smooth activation: Requires gating properties
Note: SiLU is more computationally expensive than ReLU, but generally performs better.