Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.nn.SiLU function

PyTorch torch.nn.SiLU function

PyTorch torch.nn Reference Manual 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

)

print

(

"Input:"

,

x.

tolist

(

)

)

print

(

"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

)

print

(

"x       SiLU      ReLU"

)

print

(

"-"

30

)

for

xi

in

x_tensor:

   

print

(

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

)

print

(

"Input:"

,

x.

shape

,

"-> Output:"

,

output.

shape

)


Use Cases

Note: SiLU is more computationally expensive than ReLU, but generally performs better.


PyTorch torch.nn Reference Manual PyTorch torch.nn Reference Manual